diff --git a/CHANGES.rst b/CHANGES.rst index 0ab9f54f8..9f0a451d9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -22,7 +22,11 @@ This list is detailed and covers changes in each pre-release version. Unreleased ---------- -Nothing yet. +- Fix: a signal handler was being set from multiple threads, causing an error: + ``ValueError: signal only works in main thread``. This is now fixed, closing + `issue 1312`_. + +.. _issue 1312: https://github.com/nedbat/coveragepy/issues/1312 .. _changes_63: diff --git a/coverage/control.py b/coverage/control.py index 0f62198a6..9dfa59fb1 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -11,6 +11,7 @@ import platform import signal import sys +import threading import time import warnings @@ -528,8 +529,10 @@ def _init_for_start(self): # It's useful to write debug info after initing for start. self._should_write_debug = True + # Register our clean-up handlers. atexit.register(self._atexit) - if not env.WINDOWS: + is_main = (threading.current_thread() == threading.main_thread()) + if is_main and not env.WINDOWS: # The Python docs seem to imply that SIGTERM works uniformly even # on Windows, but that's not my experience, and this agrees: # https://stackoverflow.com/questions/35772001/x/35792192#35792192