Skip to content

Commit 97d6c6e

Browse files
committed
Avoid errors about signal handlers when running Robot on thread.
Python doesn't allow non-main threads to register signal handlers. Robot already ignored errors registering handlers for SIGINT and SIGTERM if not run on the main thread. When registering the old handlers back after execution, these errors were shown on the console. This is fixed by checking are we on the main thread both before registering our own handlers before execution and also when resetting old handlers afterwards. Fixes robotframework#1848.
1 parent eb2f8ef commit 97d6c6e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/robot/running/signalhandler.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,28 @@ def start(self):
5454
self.__enter__()
5555

5656
def __enter__(self):
57-
if signal:
57+
if self._can_register_signal:
5858
self._orig_sigint = signal.getsignal(signal.SIGINT)
5959
self._orig_sigterm = signal.getsignal(signal.SIGTERM)
6060
for signum in signal.SIGINT, signal.SIGTERM:
6161
self._register_signal_handler(signum)
6262
return self
6363

6464
def __exit__(self, *exc_info):
65-
if signal:
65+
if self._can_register_signal:
6666
signal.signal(signal.SIGINT, self._orig_sigint)
6767
signal.signal(signal.SIGTERM, self._orig_sigterm)
6868

69+
@property
70+
def _can_register_signal(self):
71+
return signal and currentThread().getName() == 'MainThread'
72+
6973
def _register_signal_handler(self, signum):
7074
try:
7175
signal.signal(signum, self)
7276
except (ValueError, IllegalArgumentException), err:
73-
# ValueError occurs e.g. if Robot doesn't run on main thread.
74-
# IllegalArgumentException is http://bugs.jython.org/issue1729
75-
if currentThread().getName() == 'MainThread':
76-
self._warn_about_registeration_error(signum, err)
77+
# IllegalArgumentException due to http://bugs.jython.org/issue1729
78+
self._warn_about_registeration_error(signum, err)
7779

7880
def _warn_about_registeration_error(self, signum, err):
7981
name, ctrlc = {signal.SIGINT: ('INT', 'or with Ctrl-C '),

utest/api/test_run_and_rebot.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import sys
3+
import threading
34
import tempfile
45
import signal
56
from os.path import abspath, dirname, join, exists, curdir
@@ -181,6 +182,15 @@ def test_original_signal_handlers_are_restored(self):
181182
signal.signal(signal.SIGINT, orig_sigint)
182183
signal.signal(signal.SIGTERM, orig_sigterm)
183184

185+
def test_dont_register_signal_handlers_then_run_on_thread(self):
186+
stream = StringIO()
187+
thread = threading.Thread(target=run_without_outputs, args=(self.data,),
188+
kwargs=dict(stdout=stream, stderr=stream))
189+
thread.start()
190+
thread.join()
191+
output = stream.getvalue()
192+
assert_true('ERROR' not in output.upper(), 'Errors:\n%s' % output)
193+
184194

185195
class TestRelativeImportsFromPythonpath(RunningTestCase):
186196
_data = join(abspath(dirname(__file__)), 'import_test.robot')

0 commit comments

Comments
 (0)