<div dir="ltr">Hello,<br><div class="gmail_quote"><div dir="ltr"><div><br></div><div>My PySide app seems to ignore the TERM signal sent during shutdown on Linux when it is automatically started by putting an entry in ~/.config/autostart.</div>
<div><br>
</div><div>I came to the conclusion that the issue is with PySide/Qt through the following experiment.</div><div><br></div><div>First, I created a simple Python script that catches the TERM signal and prints a confirmation then exits. Here is the code.</div>
<div><br></div><div><font face="courier new, monospace">import signal</font></div><div><font face="courier new, monospace">import sys</font></div><div><font face="courier new, monospace">import time</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">def on_quit(*args):</font></div><div><font face="courier new, monospace"> print 'here', args</font></div><div><font face="courier new, monospace"> sys.exit(0)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">for i in [x for x in dir(signal) if x.startswith('SIG')]:</font></div><div><font face="courier new, monospace"> try:</font></div>
<div><font face="courier new, monospace"> signum = getattr(signal, i)</font></div><div><font face="courier new, monospace"> if i == 'SIGCHILD' or i == 'SIGCHLD':</font></div><div><font face="courier new, monospace"> continue</font></div>
<div><font face="courier new, monospace"> signal.signal(signum, on_quit)</font></div><div><font face="courier new, monospace"> except Exception, m:</font></div><div><font face="courier new, monospace"> print 'Skipping %s' % i</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">time.sleep(1000000)</font></div><div><br></div><div>The for loop just registers the on_quit method on all possible signals.</div>
<div><br></div><div>Calling this in a terminal and sending it the TERM signal by killing it, I can confirm that it is being caught and processed by the script.</div><div><br></div><div>I then tried to run this script at startup by including an entry in ~/.config/autostart. The entry is similar to the following:</div>
<div><br></div><div><font face="courier new, monospace">[Desktop Entry]</font></div><div><font face="courier new, monospace">Version=1.0</font></div><div><font face="courier new, monospace">Type=Application</font></div><div>
<font face="courier new, monospace">Name=Test</font></div><div><font face="courier new, monospace">GenericName=Test</font></div><div><font face="courier new, monospace">Comment=Test</font></div><div><font face="courier new, monospace">Exec=strace -T -f -tt -o run_test.strace run_test</font></div>
<div><font face="courier new, monospace">Terminal=false</font></div><div><font face="courier new, monospace">StartupNotify=false</font></div><div><br></div><div>I used strace to trace the system calls of the script to confirm in another way that it is catching the TERM signal. The options to strace tells it to include the time spent on the system call, to follow forks, to print the time of day with microseconds, and to send the output to run_test.strace, in that order.</div>
<div><br></div><div>run_test is just a bash script that calls the Python script; it is included because that is how my original PySide app is being called. The code for it is:</div><div><br></div><div><font face="courier new, monospace">#!/bin/bash</font></div>
<div><font face="courier new, monospace">python test.py &>out.test</font></div><div><br></div><div>I proceeded to test it by adding the desktop entry to ~/.config/autostart, restarting the computer, then shutting it down. run_test.strace contained the line "--- SIGTERM (Terminated) @ 0 (0) ---" which confirms that SIGTERM was sent and caught. out.test also contains the expected output which further confirms it.</div>
<div><br></div><div>I then tried to test a simple PySide application with signal handling. The code is as follows.</div><div><br></div><div><font face="courier new, monospace">import signal</font></div><div><font face="courier new, monospace">import sys</font></div>
<div><font face="courier new, monospace">import time</font></div><div><font face="courier new, monospace">from PySide.QtGui import QApplication</font></div><div><font face="courier new, monospace">from PySide.QtCore import QTimer</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">app = QApplication([])</font></div><div><font face="courier new, monospace">timer = QTimer()</font></div><div><font face="courier new, monospace">timer.start(500)</font></div>
<div><font face="courier new, monospace">timer.timeout.connect(lambda: None)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">def on_quit(*args):</font></div>
<div>
<font face="courier new, monospace"> print 'here', args</font></div><div><font face="courier new, monospace"> sys.exit(0)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">for i in [x for x in dir(signal) if x.startswith('SIG')]:</font></div>
<div><font face="courier new, monospace"> try:</font></div><div><font face="courier new, monospace"> signum = getattr(signal, i)</font></div><div><font face="courier new, monospace"> if i == 'SIGCHILD' or i == 'SIGCHLD':</font></div>
<div><font face="courier new, monospace"> continue</font></div><div><font face="courier new, monospace"> signal.signal(signum, on_quit)</font></div><div><font face="courier new, monospace"> except Exception, m:</font></div>
<div><font face="courier new, monospace"> print 'Skipping %s' % i</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">app.exec_()</font></div><div><br>
</div><div>The QTimer is essential to allow the signal handler to execute.</div><div><br></div><div>Running the above script in a terminal and sending it the TERM signal, the output still confirms that the TERM signal is being caught and processed. Running it through strace also shows the line "--- SIGTERM (Terminated) @ 0 (0) ---".</div>
<div><br></div><div>When running it during startup, however, the output shows that the on_quit method is not called nor is there an indication in the strace output that it was sent the TERM signal.</div><div><br></div><div>
Could anyone help? I can provide the strace outputs if needed. System is Ubuntu 12.04 32bit.</div></div>
</div><br></div>