pytest-timeout icon indicating copy to clipboard operation
pytest-timeout copied to clipboard

signals should be blocked in thread when using thread timeout method

Open pytest-timeout-bot opened this issue 7 years ago • 1 comments

Original report by Christopher Hunt (Bitbucket: chrahunt, GitHub: chrahunt).


Currently in Python it's only possible to change your signal disposition on a thread-specific basis using signal.pthread_sigmask. When a test depends on certain signals being blocked but we use the thread method for pytest-timeout, the signal is still received by the process via the internal thread maintained by pytest-timeout and it is then propagated to the main thread by the Python runtime.

The fix is to surround the thread start with something like

old_mask = signal.pthread_sigmask(signal.SIG_SETMASK, range(1, signal.NSIG))
t.start()
signal.pthread_sigmask(signal.SIG_SETMASK, old_mask)

This works because the signal mask is inherited by spawned threads, and also avoids a potential race condition if we were to set the signal mask inside the thread target function itself.

Currently I work around this issue by patching threading.Thread.start to do the same as above.

pytest-timeout-bot avatar Jan 20 '19 01:01 pytest-timeout-bot

Original comment by Floris Bruynooghe (Bitbucket: flub, GitHub: flub).


Great catch! Could you do a PR?

pytest-timeout-bot avatar Oct 24 '19 13:10 pytest-timeout-bot