Opened 5 years ago

Closed 5 months ago

#6235 closed defect (invalid)

Unable to release control connections on OSX

Reported by: atagar Owned by:
Priority: High Milestone: Tor: unspecified
Component: Core Tor/Tor Version:
Severity: Normal Keywords: tor-client
Cc: Actual Points:
Parent ID: Points:
Reviewer: Sponsor:

Description

Hi Nick. I've spent this whole weekend trying to diagnose an OSX specific integ error with stem without much success...
https://trac.torproject.org/5917

I've finally narrowed the issue down to the following (note that you don't need stem at all, I'm just using it to start and stop the tor process)...

import os
import socket
import signal

import stem.process

tor_process = stem.process.launch_tor_with_config(
  tor_cmd = "../tor/src/or/tor",
  config = {'ControlPort': '7777'},
)

for i in xrange(5000):
  # connect to control socket
  control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  control_socket.connect(('127.0.0.1', 7777))
  
  # disconnect
  control_socket.shutdown(socket.SHUT_RDWR)
  control_socket.close()

try:
  tor_process.kill()
except AttributeError:
  # python 2.5 or earlier
  os.kill(tor_process.pid, signal.SIGTERM)

On Linux this works perfectly. However, on a mac (I'm running on macppc.torproject.org) the control socket goes into CLOSE_WAIT status, and eventually we're unable to make further connections...

74-95-122-145-Connecticut:stem atagar$ lsof -wnP | grep "127.0.0.1:7777"
Python  15052 atagar    5u    IPv4 0x23f966c        0t0      TCP 127.0.0.1:59076->127.0.0.1:7777 (SYN_SENT)
tor     15055 atagar    5u    IPv4 0x618f270        0t0      TCP 127.0.0.1:7777 (LISTEN)
tor     15055 atagar   13u    IPv4 0x616f270        0t0      TCP 127.0.0.1:7777->127.0.0.1:58947 (CLOSE_WAIT)
74-95-122-145-Connecticut:stem atagar$ python gloom.py 
Traceback (most recent call last):
  File "gloom.py", line 15, in <module>
    control_socket.connect(('127.0.0.1', 7777))
  File "<string>", line 1, in connect
socket.error: (60, 'Operation timed out')

At this point I'm completely out of ideas. Is this a Tor bug with releasing connections? An inability with python to actually shut down a socket? Am I simply doing this wrong?

This issue has been encountered by Karsten, Sathyanarayanan, and me interdependently (all on macs), and at present prevents Stem's integ tests from running there. Feel free to lower the priority or reassign back to me if I'm simply being stupid. :)

Child Tickets

Change History (7)

comment:1 Changed 5 years ago by nickm_mobile

Istr that osx has some issues with garbage-collecting sockets. Will investigate once once i am near an osx host. If you want to investigate more, my next suggestion would be to write a trivial echo server that closes connections once the client has closed them, and see whether that has the same issue on osx or not.

comment:2 Changed 5 years ago by nickm

Milestone: Tor: unspecified

Oh! Also, Try setting reuseaddr on your sockets.

comment:3 Changed 5 years ago by atagar

Oh! Also, Try setting reuseaddr on your sockets.

I had while trying to fix the stem tests, and just tried it with this script too. No luck...

  # connect to control socket
  control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  control_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  control_socket.connect(('127.0.0.1', 7777))

comment:4 Changed 5 years ago by nickm

Keywords: tor-client added

comment:5 Changed 5 years ago by nickm

Component: Tor ClientTor

comment:6 Changed 5 months ago by nickm

Severity: Normal
Status: newneeds_information

ug, 5 years on and no good answer. Did osx ever get better here?

comment:7 Changed 5 months ago by atagar

Resolution: invalid
Status: needs_informationclosed

Hi Nick. Yeah, after this much time the trail's pretty cold. I haven't a clue if this is a concern or not. Gonna go ahead and resolve.

Note: See TracTickets for help on using tickets.