Skip to content

Commit f3b0a06

Browse files
mauvilsaawaelchlitchatoncarmocca
authored
Fix SignalConnector._has_already_handler check for callable type (#10483)
Co-authored-by: Adrian Wälchli <[email protected]> Co-authored-by: thomas chaton <[email protected]> Co-authored-by: Carlos Mocholi <[email protected]>
1 parent 25473ac commit f3b0a06

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
199199
- Fixed an issue with `SignalConnector` not restoring the default signal handlers on teardown when running on SLURM or with fault-tolerant training enabled ([#10611](https://github.com/PyTorchLightning/pytorch-lightning/pull/10611))
200200

201201

202+
- Fixed `SignalConnector._has_already_handler` check for callable type ([#10483](https://github.com/PyTorchLightning/pytorch-lightning/pull/10483))
203+
204+
202205
- Disabled batch_size extraction for torchmetric instances because they accumulate the metrics internally ([#10815](https://github.com/PyTorchLightning/pytorch-lightning/pull/10815))
203206

204207

pytorch_lightning/trainer/connectors/signal_connector.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import threading
66
from signal import Signals
77
from subprocess import call
8-
from types import FrameType, FunctionType
8+
from types import FrameType
99
from typing import Any, Callable, Dict, List, Set, Union
1010

1111
import pytorch_lightning as pl
@@ -138,10 +138,7 @@ def _is_on_windows() -> bool:
138138

139139
@staticmethod
140140
def _has_already_handler(signum: Signals) -> bool:
141-
try:
142-
return isinstance(signal.getsignal(signum), FunctionType)
143-
except AttributeError:
144-
return False
141+
return signal.getsignal(signum) is not signal.SIG_DFL
145142

146143
@staticmethod
147144
def _register_signal(signum: Signals, handlers: HandlersCompose) -> None:

tests/trainer/connectors/test_signal_connector.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,27 @@ def test_signal_connector_in_thread():
109109
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
110110
for future in concurrent.futures.as_completed([executor.submit(_registering_signals)]):
111111
assert future.exception() is None
112+
113+
114+
def signal_handler():
115+
pass
116+
117+
118+
class SignalHandlers:
119+
def signal_handler(self):
120+
pass
121+
122+
123+
@pytest.mark.parametrize(
124+
["handler", "expected_return"],
125+
[
126+
(signal.Handlers.SIG_IGN, True),
127+
(signal.Handlers.SIG_DFL, False),
128+
(signal_handler, True),
129+
(SignalHandlers().signal_handler, True),
130+
],
131+
)
132+
def test_has_already_handler(handler, expected_return):
133+
"""Test that the SignalConnector detects whether a signal handler is already attached."""
134+
with mock.patch("pytorch_lightning.trainer.connectors.signal_connector.signal.getsignal", return_value=handler):
135+
assert SignalConnector._has_already_handler(signal.SIGTERM) is expected_return

0 commit comments

Comments
 (0)