Merge branch 'bug27968_squashed' into maint-0.3.5

This commit is contained in:
Nick Mathewson 2018-11-02 13:14:32 -04:00
commit 996f24fcd3
2 changed files with 47 additions and 17 deletions

3
changes/bug27968 Normal file
View File

@ -0,0 +1,3 @@
o Minor bugfixes (testing):
- Avoid hangs and race conditions in test_rebind.py.
Fixes bug 27968; bugfix on 0.3.5.1-alpha.

View File

@ -1,25 +1,42 @@
from __future__ import print_function from __future__ import print_function
import sys import errno
import subprocess
import socket
import os import os
import time
import random import random
import socket
import subprocess
import sys
import time
LOG_TIMEOUT = 60.0
LOG_WAIT = 0.1
LOG_CHECK_LIMIT = LOG_TIMEOUT / LOG_WAIT
def fail(msg):
print('FAIL')
sys.exit(msg)
def try_connecting_to_socksport(): def try_connecting_to_socksport():
socks_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socks_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if socks_socket.connect_ex(('127.0.0.1', socks_port)): if socks_socket.connect_ex(('127.0.0.1', socks_port)):
tor_process.terminate() tor_process.terminate()
print('FAIL') fail('Cannot connect to SOCKSPort')
sys.exit('Cannot connect to SOCKSPort')
socks_socket.close() socks_socket.close()
def wait_for_log(s): def wait_for_log(s):
while True: log_checked = 0
while log_checked < LOG_CHECK_LIMIT:
l = tor_process.stdout.readline() l = tor_process.stdout.readline()
if s in l.decode('utf8'): l = l.decode('utf8')
if s in l:
return return
print('Tor logged: "{}", waiting for "{}"'.format(l.strip(), s))
# readline() returns a blank string when there is no output
# avoid busy-waiting
if len(s) == 0:
time.sleep(LOG_WAIT)
log_checked += 1
fail('Could not find "{}" in logs after {} seconds'.format(s, LOG_TIMEOUT))
def pick_random_port(): def pick_random_port():
port = 0 port = 0
@ -33,13 +50,16 @@ def pick_random_port():
else: else:
break break
if port == 0:
fail('Could not find a random free port between 10000 and 60000')
return port return port
if sys.hexversion < 0x02070000: if sys.hexversion < 0x02070000:
sys.exit("ERROR: unsupported Python version (should be >= 2.7)") fail("ERROR: unsupported Python version (should be >= 2.7)")
if sys.hexversion > 0x03000000 and sys.hexversion < 0x03010000: if sys.hexversion > 0x03000000 and sys.hexversion < 0x03010000:
sys.exit("ERROR: unsupported Python3 version (should be >= 3.1)") fail("ERROR: unsupported Python3 version (should be >= 3.1)")
control_port = pick_random_port() control_port = pick_random_port()
socks_port = pick_random_port() socks_port = pick_random_port()
@ -48,7 +68,7 @@ assert control_port != 0
assert socks_port != 0 assert socks_port != 0
if not os.path.exists(sys.argv[1]): if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: cannot find tor at %s' % sys.argv[1]) fail('ERROR: cannot find tor at %s' % sys.argv[1])
tor_path = sys.argv[1] tor_path = sys.argv[1]
@ -60,10 +80,10 @@ tor_process = subprocess.Popen([tor_path,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
if tor_process == None: if tor_process == None:
sys.exit('ERROR: running tor failed') fail('ERROR: running tor failed')
if len(sys.argv) < 2: if len(sys.argv) < 2:
sys.exit('Usage: %s <path-to-tor>' % sys.argv[0]) fail('Usage: %s <path-to-tor>' % sys.argv[0])
wait_for_log('Opened Control listener on') wait_for_log('Opened Control listener on')
@ -72,8 +92,7 @@ try_connecting_to_socksport()
control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if control_socket.connect_ex(('127.0.0.1', control_port)): if control_socket.connect_ex(('127.0.0.1', control_port)):
tor_process.terminate() tor_process.terminate()
print('FAIL') fail('Cannot connect to ControlPort')
sys.exit('Cannot connect to ControlPort')
control_socket.sendall('AUTHENTICATE \r\n'.encode('utf8')) control_socket.sendall('AUTHENTICATE \r\n'.encode('utf8'))
control_socket.sendall('SETCONF SOCKSPort=0.0.0.0:{}\r\n'.format(socks_port).encode('utf8')) control_socket.sendall('SETCONF SOCKSPort=0.0.0.0:{}\r\n'.format(socks_port).encode('utf8'))
@ -88,6 +107,14 @@ try_connecting_to_socksport()
control_socket.sendall('SIGNAL HALT\r\n'.encode('utf8')) control_socket.sendall('SIGNAL HALT\r\n'.encode('utf8'))
time.sleep(0.1) wait_for_log('exiting cleanly')
print('OK') print('OK')
try:
tor_process.terminate() tor_process.terminate()
except OSError as e:
if e.errno == errno.ESRCH: # errno 3: No such process
# assume tor has already exited due to SIGNAL HALT
print("Tor has already exited")
else:
raise