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

atagar at torproject.org atagar at torproject.org
Sun Dec 7 00:56:30 UTC 2014


commit 2ee4e95ab0f2640674c1074ce26e2f09ce9bdf26
Author: Damian Johnson <atagar at torproject.org>
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))





More information about the tor-commits mailing list