dabeaz / curio

Good Curio!
Other
4.04k stars 243 forks source link

Investigate Ctrl-C behavior on Windows #204

Closed dabeaz closed 7 years ago

dabeaz commented 7 years ago

This was referenced in a different issue, but there is some strange difference between SignalEvent and SignalQueue on Windows that allows SignalQueues to respond immediately to Ctrl-C whereas SignalEvent can be delayed. This happens even if signal queuing is enabled (which turns on a signal file descriptor and other machinery used by queues). Need to investigate further because the differences in handling are not immediately obvious.

dabeaz commented 7 years ago

Have resolved this. Signals can write to a monitored file descriptor, but any associated signal handlers get delayed. SignalEvent objects have been changed to operate off of the file descriptor.

njsmith commented 7 years ago

Huh, I read the patch and I can't figure out why it made a difference (assuming that set_wakeup_fd had been called, which from your original post here it sounds like you're saying didn't make any difference...)

dabeaz commented 7 years ago

The setting of the event was taking place in the signal handler function. However, this function isn't triggered by the main thread until the underlying select() call returns (even though a byte is still written to the wakeup_fd). Patch moves the event setting to the thread that's reading the wakeup fd.

njsmith commented 7 years ago

Right, but writing a byte to the wakeup_fd should cause the underlying select() call to return, and then the signal handler function should be called :-)

On further thought I bet you were running into this bug: https://bugs.python.org/issue30038

dabeaz commented 7 years ago

It's not anything related to that bug. Signal handling in Curio is not handled in-kernel and is not processed by the kernel select loop. A wakeup_fd is used, but it's read by a completely separate thread that dispatches signal notifications via queues and events to Curio tasks. This is part of a different effort to make the Curio kernel even smaller and to carry out more functionality in "user space".

njsmith commented 7 years ago

Oh, huh, I indeed missed that! Interesting, thanks!