Skip to content

Commit 3bf1f7c

Browse files
committed
Move signal handler attachment back to main body. Signals cannot be
attached from threads in Python. While it is impossible to guarantee that any piece of Python code is not called from a thread (one can always import this class from the run method of a thread), this reduces the likelihood of that happening. This commit also adds a clear error message stating that psij has to be imported from the main thread.
1 parent bc2bde5 commit 3bf1f7c

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/psij/executors/local.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818
logger = logging.getLogger(__name__)
1919

2020

21+
if threading.current_thread() != threading.main_thread():
22+
raise ImportError('The psij module must be imported from the main thread.')
23+
24+
2125
def _handle_sigchld(signum: int, frame: Optional[FrameType]) -> None:
2226
_ProcessReaper.get_instance()._handle_sigchld()
2327

2428

29+
signal.signal(signal.SIGCHLD, _handle_sigchld)
30+
31+
2532
_REAPER_SLEEP_TIME = 0.2
2633

2734

0 commit comments

Comments
 (0)