onionshare icon indicating copy to clipboard operation
onionshare copied to clipboard

Kill tor and its subprocesses

Open micahflee opened this issue 7 years ago • 6 comments

When using Bundled Tor, obfs4proxy, if you switch to using no bridges, it crashes like this:

[Jan 17 2018 18:32:02] SettingsDialog.save_clicked: Connected to Tor
[Jan 17 2018 18:32:02] SettingsDialog.save_clicked: rebooting the Onion
[Jan 17 2018 18:32:02] Onion.cleanup
Traceback (most recent call last):
  File "onionshare_gui\settings_dialog.py", line 628, in save_clicked
    self.onion.cleanup()
  File "onionshare\onion.py", line 511, in cleanup
    self.tor_data_directory.cleanup()
  File "tempfile.py", line 811, in cleanup
  File "shutil.py", line 494, in rmtree
  File "shutil.py", line 389, in _rmtree_unsafe
  File "shutil.py", line 387, in _rmtree_unsafe
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\MICAHL~1\\AppData\\Local\\Temp\\tmpv011cdr9\\lock'

This is because it can't delete the temporary tor data dir, and I'm pretty sure it's because obfs4proxy.exe is running and has an open file in that dir, and it doesn't get killed when tor.exe gets killed.

So I think the solution is to kill tor's children before killing the tor process (brutal, I know).

micahflee avatar Jan 18 '18 02:01 micahflee

Interesting reading https://stackoverflow.com/questions/23434842/python-how-to-kill-child-processes-when-parent-dies

mig5 avatar Jan 18 '18 02:01 mig5

Oh interesting. I was just trying to kill subprocesses like this https://stackoverflow.com/questions/1230669/subprocess-deleting-child-processes-in-windows but failed.

So for the moment I'm going with a simpler approach, and just catching the PermissionError exception and then ignoring it, because the temp dir will eventually empty itself, and it shouldn't contain any secrets like onion keys or anything in it.

I figure I can spend more time troubleshooting this one in a later release.

micahflee avatar Jan 18 '18 02:01 micahflee

:+1: I still can't even get obfs4 to get past 20% on Windows, frustrating because the obfs4 in Tor Browser on the same VM Just Works somehow... and it's the same (well, copy of) obfs4proxy.exe!

mig5 avatar Jan 18 '18 02:01 mig5

My obfs4proxy is stuck at 76% in Windows right now. I picked 6 random bridges from https://bridges.torproject.org/. Haven't succeeded in connecting yet...

micahflee avatar Jan 18 '18 02:01 micahflee

Another situation where Tor subprocess stays open is when OnionShare CLI client on Linux is closed by closing terminal by clicking the X button. This leaves orphaned child process running in the background. The only solution currently is to remember to close the application every time with ^C first.

I'm currently handling this in my own project by running following at start of the program.

try:
    pids = subprocess.check_output("ps aux |grep '[t]orrc' | awk '{print $2}' 2>/dev/null", shell=True).split(b'\n')
    for pid in pids:
        subprocess.Popen("kill {}".format(int(pid)), shell=True).wait()
except ValueError:
    pass

which seems to work (as long as readline (if needed) is imported after the command: otherwise check_output returns list with empty string and fails to find the pids). If this is an acceptable solution, Onion() object's self.tor_torrc value should probably be unique to onion share that the check_output greps. Hopefully a more robust alternative is found.

maqp avatar Jan 22 '18 04:01 maqp

Hi, all. Is this still an issue/ bug? It's 7 years since the last update.

chrisdebian avatar May 09 '25 17:05 chrisdebian