This repository was archived by the owner on Nov 23, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 178
Fix remove_signal_handler to not to crash after PyOS_FiniInterrupts #456
Open
1st1
wants to merge
2
commits into
python:master
Choose a base branch
from
1st1:signals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
"""Selector event loop for Unix with signal handling.""" | ||
|
||
import atexit | ||
import errno | ||
import os | ||
import signal | ||
|
@@ -9,6 +10,7 @@ | |
import sys | ||
import threading | ||
import warnings | ||
import weakref | ||
|
||
|
||
from . import base_events | ||
|
@@ -39,6 +41,12 @@ def _sighandler_noop(signum, frame): | |
pass | ||
|
||
|
||
def _loop_atexit_callback(loop_ref): | ||
loop = loop_ref() | ||
if loop is not None: | ||
loop._interpreter_shutting_down = True | ||
|
||
|
||
class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): | ||
"""Unix event loop. | ||
|
||
|
@@ -47,6 +55,15 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): | |
|
||
def __init__(self, selector=None): | ||
super().__init__(selector) | ||
|
||
# atexit callbacks are fired before PyOS_FiniInterrupts, which | ||
# allows us to workaround bugs in remove_signal_handler. | ||
atexit.register(_loop_atexit_callback, weakref.ref(self)) | ||
# When _interpreter_shutting_down is True, PyOS_FiniInterrupts | ||
# has already been called and signalmodule's internal state was | ||
# cleaned up. | ||
self._interpreter_shutting_down = False | ||
|
||
self._signal_handlers = {} | ||
|
||
def _socketpair(self): | ||
|
@@ -124,6 +141,11 @@ def remove_signal_handler(self, sig): | |
|
||
Return True if a signal handler was removed, False if not. | ||
""" | ||
if self._interpreter_shutting_down: | ||
# The interpreter is being shutdown. `PyOS_FiniInterrupts` | ||
# was already called and it has restored all signals already. | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should return a bool. |
||
|
||
self._check_signal(sig) | ||
try: | ||
del self._signal_handlers[sig] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of just
return
, we can emit a warning that the loop wasn't properly closed. @gvanrossum what do you think about that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only in debug mode. But frankly I don't understand this code.