Information
ID: 117
PHID: PHID-TASK-yravw3g3eyjzmpbt2xdc
Author: Patrick
Status at Migration Time: resolved
Priority at Migration Time: Normal
Description
Problem, when you start something like
/usr/lib/msgcollector/msgdispatcher_dispatch_x info "test title" "test msg" 1 ""
from terminal, you cannot terminate using ctrl + c. This makes it difficult for calling scripts to terminate them (in case those would receive a signal themselves) and then that process could either hang or stay open. Not critical, but an issue we can fix.
To find them.
grep -r \#\!/usr/bin/python *
What do do… I suggest this.
on sigterm → “Signal sigterm received! Exiting.” → exit 143
on sigint (ctrl + c) → “Signal sigint received! Exiting.” → exit 130
Ideally we have some small code snippet that we can easily reuse and copy and paste into all the scripts.
Comments
Patrick
2015-01-26 22:25:32 UTC
Ideally, we could set up one global signal trap per script. In most cases we don’t need any cleanup code. Or? By “global signal trap”, I mean just to set it up once at the very top. Not below in every function and loop and so forth.
I failed to setup a working global trap for the Qt script. Maybe you have any ideas, @troubadour ?
troubadour
2015-01-28 00:07:31 UTC
We could put this at the top of every script.
import signal
import os, sys
import time
f_name = os.path.basename(__file__) + '.pid'
f = open(f_name, 'w')
f.write(str(os.getpid()) + '\n')
f.close()
def receive_signal(signum, signal):
#print signum
if signum == 15: # SIGTERM
sys.exit(143)
elif signum == 2: # SIGINT
sys.exit(130)
signal.signal(signal.SIGTERM, receive_signal)
signal.signal(signal.SIGINT, receive_signal)
# for testing
while True:
print 'Waiting...'
time.sleep(5)
A bash script (!!) for testing (implies that both scripts are in the same directory).
pid=$(cat kill_signal.pid)
kill -SIGINT $pid # replace with any signal
But that is may be overkill for the graphical scripts (Qt). A more typical use is with daemons.
troubadour
2015-01-28 09:35:52 UTC
The above does not work once the Qt event handler loop is running. There is a hack for SIGINT (Ctrl+C), when the script is run from command line. Two lines at the very beginning of the script.
# -*- coding: utf-8 -*-
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
Don’t know if it allows custom traps.
Tested with generic_gui_message
, msgdispatcher_dispatch_x
and whonix-setup-wizard
Patrick
2015-01-29 08:09:58 UTC
Let’s find some small code easy solution for the simple non-QT command line tools and then worry about the QT tools later?
Tried your above code. It works when using the sleep testing code. But it doesn’t work when connection really hangs (such as tor bootstrap).
Test case, theoretical, bad firewall rules. Practical simulation, on the gateway: Put exit 0
below iptables -t mangle -X
in /usr/bin/whonix_firewall
, then reload it, sudo whonix_firewall.
.
On the workstation, see the following test script.
#/bin/bash
set -x
/usr/lib/anon-shared-helper-scripts/tor_bootstrap_check.py 127.0.0.1 9151 0 &
pid="$!"
sleep 1
kill -sigterm "$pid"
wait "$pid"
true "exit code: $?"
Output:
++ pid=30266
++ sleep 1
++ /usr/lib/anon-shared-helper-scripts/tor_bootstrap_check.py 127.0.0.1 9151 0
++ kill -sigterm 30266
++ wait 30266
(The sleep 1
is used to interrupt python during the connection phase, not very early during initialization where sigterm does still function. The wait
indicates, that the process is still running, even though signal sigterm was sent.)
hqi
2015-09-03 00:15:17 UTC
I tried this command:
/usr/lib/msgcollector/msgdispatcher_dispatch_x
and found the output is like this:
user@host:~$ /usr/lib/msgcollector/msgdispatcher_dispatch_x
Traceback (most recent call last):
File “/usr/lib/msgcollector/msgdispatcher_dispatch_x”, line 91, in
if str(sys.argv[1]) == “info”:
IndexError: list index out of range
So I added some code between ‘#—’ to avoid it:
if name == “main ”:
import sys
#-------------------------------------------------
if len(sys.argv) == 1:
sys.exit(“‘msgdispatcher_dispatch_x’. There is no option”)
#-------------------------------------------------
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
Patrick
2015-09-03 11:51:35 UTC
hqi
2015-09-03 14:45:58 UTC
Patrick
2015-09-03 14:53:29 UTC
hqi
2015-09-03 23:15:58 UTC
Patrick
2015-09-03 23:17:16 UTC
joysn1980
2017-01-17 11:37:13 UTC
msgcollector/usr/lib/msgcollector/msgdispatcher_dispatch_x at master · joysn/msgcollector · GitHub
Is this what is required here?
Error message for incorrect # of arguments
‘msgdispatcher_dispatch_x’. Invalid number of options
msgdispatcher_dispatch_x requires 4 manadatory and 1 optional arguments
1) Message Type = info|warning|error
2) Title of the message box
3) Message in the message box
4) Position of the message (Value 0 or more)
5) Icon to be used (optional)
Working commands
/usr/lib/msgcollector/msgdispatcher_dispatch_x “info” “test title” “test msg” 0
usr/lib/msgcollector/msgdispatcher_dispatch_x “info” “test title” “test msg” 0 “/path/to/optional/icon”
If there are lesser arguments, we will see the error.
Patrick
2017-01-17 11:55:21 UTC
No. However, that’s good to have.
What is missing here… When you run:
/usr/lib/msgcollector/msgdispatcher_dispatch_x info "test title" "test msg" 1 ""
And then press ctrl + c
or ctrl + d
, nothing happens. The sigterm / sigint signal is ignored. The wanted behavior is to exit.
on sigterm → “Signal sigterm received! Exiting.” → exit 143
on sigint (ctrl + c) → “Signal sigint received! Exiting.” → exit 130
joysn1980
2017-01-17 13:09:17 UTC
Patrick
2017-01-17 15:08:39 UTC
joysn1980
2017-01-17 15:19:09 UTC
Is this task about doing this for all python scripts?
There 4 more in this directory
br_add:#!/usr/bin/python
generic_gui_message:#!/usr/bin/python
striphtml:#!/usr/bin/python
tb_updater_gui:#!/usr/bin/python
Are all these standalone python scripts? Are all these needs to be modified to have same changes - ctrl+c ?
Patrick
2017-01-17 15:36:54 UTC
Patrick
2017-01-17 15:41:50 UTC
A few more projects are involved, where this is TODO. Btw there are all our components where #python is involved.
Some of them (perhaps #sdwdate and #control-port-filter-python ) might already have proper signal handling.
joysn1980
2017-01-18 08:07:20 UTC
joysn1980
2017-01-18 08:16:57 UTC
joysn1980
2017-01-18 08:44:34 UTC
From guimessages - 1 change required and it done
GitHub - joysn/python-guimessages: Translatable GUI Messages - python library
/usr/share/pyshared/guimessages/guimessage.py - changes done
/usr/share/pyshared/guimessages/translations.py - no change required
From anon-shared-helper-scripts - no change required
/usr/lib/anon-shared-helper-scripts/tor_circuit_established_check.py - no change required
/usr/lib/anon-shared-helper-scripts/tor_circuit_established_check.py - no change required
/usr/lib/anon-shared-helper-scripts/tor_circuit_established_check.py - no change required
joysn1980
2017-01-18 09:28:17 UTC
So we are done with
msgcollector - to be merged
sdwdate-gui - to be merged
python-guimessages - to be merged
No changes required for
sdwdate
tor-controlport-filter
anon-shared-helper-scripts
Let me know if there any places which still requires the changes, I can make them
joysn1980
2017-01-18 09:28:42 UTC
Patrick
2017-01-18 11:41:43 UTC