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

Ctrl+C does not interrupt Socket.recv or Socket.send #49

Closed
codypiersall opened this issue Oct 26, 2019 · 2 comments
Closed

Ctrl+C does not interrupt Socket.recv or Socket.send #49

codypiersall opened this issue Oct 26, 2019 · 2 comments

Comments

@codypiersall
Copy link
Owner

The normal behavior of Python applications is to be able to hit Ctrl+C to interrupt the main thread. However, pynng breaks this expectation for Socket.recv() and Socket.send(). I'm not confident that I've found the underlying issue in nng yet, but I believe (and could be wrong, I haven't attempted a patch yet) that this is related to the use of some pthread_cond_wait, rather than actually restarting the recv() system calls.

@codypiersall
Copy link
Owner Author

Okay, it turns out that this cannot be solved at the Python level in any reasonable way I can think of. I was attempting to register a signal handler for SIGINT to examine the stack frame and close any pynng sockets, but since the handler will only run in Python's main thread that was a nonstarter; the whole reason we send SIGINT is because the main thread is in a blocking call!

We could do something painful like send all calls to recv() and send() to a thread pool, but it seems needlessly inefficient; the cure is worse than the disease.

The good news is that the async API doesn't have this problem! i.e. the following code does the right thing, raising a KeyboardInterrupt exception when SIGINT is received:

import pynng
import trio
s = pynng.Pair0()
trio.run(s.arecv)

The situation for Windows is worse than POSIX, because at least on POSIX if upstream changes things will get better. On Windows, to get nice behavior we'll have to have hacks no matter what, it seems; see this PR that does... something... to make Ctrl+C work on Windows. I haven't looked at it closely yet and... it's time for bed. :-)

@codypiersall
Copy link
Owner Author

Closing this for the newer one #106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant