[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Unable to call socket.gethostbyname() when disconnected



commit 2ee4e95ab0f2640674c1074ce26e2f09ce9bdf26
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Dec 6 16:45:24 2014 -0800

    Unable to call socket.gethostbyname() when disconnected
    
    A Gentoo ticket notes an interesting stacktrace...
    
      https://bugs.gentoo.org/show_bug.cgi?id=527072
    
      ======================================================================
      ERROR: test_all_private_policy
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/tmp/portage/net-libs/stem-1.2.2_p20141018/work/stem-1.2.2_p20141018/test/unit/exit_policy/policy.py", line 115, in test_all_private_policy
          private_policy = get_config_policy('reject private:%s' % port)
        File "/tmp/portage/net-libs/stem-1.2.2_p20141018/work/stem-1.2.2_p20141018/stem/exit_policy.py", line 141, in get_config_policy
          ip_address = socket.gethostbyname(socket.gethostname())
      gaierror: [Errno -3] Temporary failure in name resolution
      ======================================================================
    
    I wasn't able to repro this by simply disconnecting, but oh well. Easy enough
    to suppress the exception.
---
 stem/exit_policy.py |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index 0b7bea7..9bc8ccb 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -136,11 +136,17 @@ def get_config_policy(rules, ip_address = None):
     if 'private' in rule:
       acceptance = rule.split(' ', 1)[0]
       port = rule.split(':', 1)[1]
+      addresses = list(PRIVATE_ADDRESSES)
 
-      if ip_address is None:
-        ip_address = socket.gethostbyname(socket.gethostname())
+      if ip_address:
+        addresses.append(ip_address)
+      else:
+        try:
+          addresses.append(socket.gethostbyname(socket.gethostname()))
+        except:
+          pass  # we might not have a network connection
 
-      for private_addr in PRIVATE_ADDRESSES + (ip_address,):
+      for private_addr in addresses:
         result.append(ExitPolicyRule("%s %s:%s" % (acceptance, private_addr, port)))
     else:
       result.append(ExitPolicyRule(rule))



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits