Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SignalConnector._has_already_handler check for callable type #10483

Merged
merged 17 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `CombinedLoader` and `max_size_cycle` didn't receive a `DistributedSampler` ([#10374](https://github.com/PyTorchLightning/pytorch-lightning/issues/10374))


- Fixed `SignalConnector._has_already_handler` check for callable type ([#10483](https://github.com/PyTorchLightning/pytorch-lightning/pull/10483))


-


Expand Down
4 changes: 2 additions & 2 deletions pytorch_lightning/trainer/connectors/signal_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from signal import Signals
from subprocess import call
from types import FrameType, FunctionType
from types import FrameType
from typing import Callable, List, Union

import pytorch_lightning as pl
Expand Down Expand Up @@ -104,6 +104,6 @@ def _is_on_windows(self) -> bool:

def _has_already_handler(self, signum: Signals) -> bool:
try:
return isinstance(signal.getsignal(signum), FunctionType)
return callable(signal.getsignal(signum))
carmocca marked this conversation as resolved.
Show resolved Hide resolved
except AttributeError:
return False
26 changes: 26 additions & 0 deletions tests/trainer/connectors/test_signal_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pytest

from pytorch_lightning import Trainer
from pytorch_lightning.trainer.connectors.signal_connector import SignalConnector
from pytorch_lightning.utilities.exceptions import ExitGracefullyException
from tests.helpers import BoringModel
from tests.helpers.runif import RunIf
Expand Down Expand Up @@ -57,3 +58,28 @@ def training_step(self, batch, batch_idx):
else:
trainer.fit(model)
assert trainer._terminate_gracefully == (False if register_handler else terminate_gracefully)


def signal_handler():
pass


class SignalHandlers:
def signal_handler(self):
pass


@pytest.mark.parametrize(
["handler", "expected_return"],
[
(signal.Handlers.SIG_DFL, False),
(signal_handler, True),
(SignalHandlers().signal_handler, True),
],
)
def test_has_already_handler(handler, expected_return):
"""Test that the SignalConnector detects whether a signal handler is already attached."""
trainer = Trainer()
connector = SignalConnector(trainer)
with mock.patch("signal.getsignal", return_value=handler):
assert connector._has_already_handler(signal.SIGTERM) is expected_return