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

Allow interrupting input() on Windows, as part of effort to make pdb interruptible #498

Merged
merged 10 commits into from
May 20, 2020
13 changes: 10 additions & 3 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ def _input_request(self, prompt, ident, parent, password=False):
# Flush output before making the request.
sys.stderr.flush()
sys.stdout.flush()

# flush the stdin socket, to purge stale replies
while True:
try:
Expand All @@ -885,14 +886,20 @@ def _input_request(self, prompt, ident, parent, password=False):
# Await a response.
while True:
try:
ident, reply = self.session.recv(self.stdin_socket, 0)
except Exception:
ident, reply = self.session.recv(self.stdin_socket)
except Exception as e:
self.log.warning("Invalid Message:", exc_info=True)
except KeyboardInterrupt:
# re-raise KeyboardInterrupt, to truncate traceback
raise KeyboardInterrupt("Interrupted by user") from None
else:
break
# Use polling with sleep so KeyboardInterrupts can get through;
# doing a blocking recv() means stdin reads are uninterruptible
# on Windows:
if (ident, reply) == (None, None):
time.sleep(0.001)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wary of spin loops like this; since it is a socket we can use select().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately select() is also uninterruptible by KeyboardInterrupt on Windows. I decided to try using it anyway, because that way at least the loop will be more responsive while needing to wake up less. But sadly as far as I can tell this needs a loop of some sort if it's going to be uninterruptible 😞

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shucks, thanks for looking into it. I'm not sure what happened on the last travis build, I kicked it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried matplotlib 3.2.1 on my Linux machine and can't reproduce those issues. But on newer version of Python.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPython 7.14.0 was released 4 days ago, that might be it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using same version of matplotlib as Travis, though. With updated ipython still can't reproduce... Can you try rerunning tests on master? At least that way can tell if it's in any way related to this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and that failed in the same way

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one thing to try is pinning the ipython version to 7.13.0

else:
break
try:
value = py3compat.unicode_to_str(reply['content']['value'])
except:
Expand Down
17 changes: 17 additions & 0 deletions ipykernel/tests/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,20 @@ def test_shutdown():
else:
break
assert not km.is_alive()


def test_interrupt_during_input():
"""The kernel exits after being interrupted, while waiting in input().

input() appears to have issues other functions don't, and it needs to be
interruptible in order for pdb to be interruptible.
"""
with new_kernel() as kc:
km = kc.parent
msg_id = kc.execute("input()")
time.sleep(1) # Make sure it's actually waiting for input.
km.interrupt_kernel()
# If we failed to interrupt interrupt, this will timeout:
reply = kc.get_shell_msg(timeout=TIMEOUT)
from .test_message_spec import validate_message
validate_message(reply, 'execute_reply', msg_id)