Information
ID: 629
PHID: PHID-TASK-srp7d6ckduruuyel7ciq
Author: Patrick
Status at Migration Time: open
Priority at Migration Time: Normal
Description
When sdwdate is inside the threads loop in function get_time_from_servers
from file remote_times.py
, signal sigterm does not lead to sdwdate terminating.
#!/usr/bin/python3 -u
# Copyright (C) 2015 troubadour <trobador@riseup.net>
# Copyright (C) 2015 - 2023 ENCRYPTED SUPPORT LP <adrelanos@whonix.org>
# See the file COPYING for copying conditions.
# Example:
# sudo -u sdwdate python3 /usr/lib/python3/dist-packages/sdwdate/remote_times.py "http://www.dds6qkxpwdeubwucdiaord2xgbbeyds25rbsgr73tbfpqpt4a6vjwsyd.onion/a http://www.dds6qkxpwdeubwucdiaord2xgbbeyds25rbsgr73tbfpqpt4a6vjwsyd.onion/b http://www.dds6qkxpwdeubwucdiaord2xgbbeyds25rbsgr73tbfpqpt4a6vjwsyd.onion/c" "127.0.0.1" "9050"
import sys
sys.dont_write_bytecode = True
import os
import signal
import shlex
import time
import subprocess
from subprocess import Popen, PIPE
import concurrent.futures
This file has been truncated. show original
signal_sigterm_handler
is triggered, but sys.exit(143)
is without effect.
Apparently that is to be expected.
python, python-2.6
TODO:
Shut terminate all eventually running threads and exit.
How to reproduce?
#!/bin/bash
set -x
cd ~/Whonix/packages/sdwdate
sudo make install
sudo -u sdwdate sdwdate &
sdwdate_pid="$!"
sleep 1
sudo kill -sigterm "$sdwdate_pid"
wait "$sdwdate_pid"
true $?
The signal must send during.
Requested urls [‘x.onion ’, ‘y.onion ’, ‘z.onion ’]
I.e. signal must send during gevent.wait().
Comments
Patrick
2017-02-13 14:24:47 UTC
Patrick
2017-03-01 21:10:46 UTC
marmarek
2017-03-01 22:04:48 UTC
Where the other thread is created? For me it looks like get_time_from_servers
is running in the main thread, so simply not catching SystemExit
should be enough. At the point where it was raised, you already called sys.exit
(which is how SystemExit
is raised), so exit_handler
was already called.
If I’m mistaken about this, and it’s running in non-main thread, you may want to use threading — Thread-based parallelism — Python 3.12.2 documentation instead, and check it in main loop. Something like:
exit_event = threading.Event()
...
if exit_event.is_set():
sys.exit(143)
And from the other thread:
exit_event.set()
marmarek
2017-03-01 22:09:42 UTC
Also, Python >= 3.4 comes with great API for concurrent execution - asyncio - much more powerful version of asyncore. In this case it could be used to avoid threads at all. But I’m still learning how to use it… You may want to read/watch this: FOSDEM 2017 - Asynchronous programming with Coroutines in Python if you want quick intro.