Skip to content

Commit

Permalink
more care taken with wakeup_read_fd on non-main threads
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasballinger committed Oct 19, 2021
1 parent a6019a4 commit 307aa87
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions curtsies/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
self.sigint_event = sigint_event
self.disable_terminal_start_stop = disable_terminal_start_stop
self.sigints: List[events.SigIntEvent] = []
self.wakeup_read_fd: Optional[int] = None

self.readers: List[int] = []
self.queued_interrupting_events: List[Union[events.Event, str]] = []
Expand Down Expand Up @@ -110,12 +111,14 @@ def __enter__(self) -> "Input":
self.orig_sigint_handler = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, self.sigint_handler)

self.wakeup_read_fd, wfd = os.pipe()
os.set_blocking(wfd, False)
if sys.version_info[0] == 3 and 5 <= sys.version_info[1] < 7:
signal.set_wakeup_fd(wfd)
elif sys.version_info[0] == 3 and 7 <= sys.version_info[1]:
signal.set_wakeup_fd(wfd, warn_on_full_buffer=False)
# Non-main threads don't receive signals
if threading.current_thread() is threading.main_thread():
self.wakeup_read_fd, wfd = os.pipe()
os.set_blocking(wfd, False)
if sys.version_info[0] == 3 and 5 <= sys.version_info[1] < 7:
signal.set_wakeup_fd(wfd)
elif sys.version_info[0] == 3 and 7 <= sys.version_info[1]:
signal.set_wakeup_fd(wfd, warn_on_full_buffer=False)

return self

Expand Down Expand Up @@ -168,10 +171,8 @@ def _wait_for_read_ready_or_timeout(
while True:
try:
(rs, _, _) = select.select(
[
self.in_stream.fileno(),
self.wakeup_read_fd,
]
[self.in_stream.fileno()]
+ ([] if self.wakeup_read_fd is None else [self.wakeup_read_fd])
+ self.readers,
[],
[],
Expand Down

0 comments on commit 307aa87

Please sign in to comment.