Dispatch interrupts to StdIn and ReadConsole #60
Merged
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.
Branched from #58
Addresses posit-dev/positron#535.
The fix is in two parts:
In
ReadConsole()
: Detect interrupts signaled during a user-prompt request, e.g. duringreadline()
. In case of interrupt, quit the current Rust context and callonintr()
to cause a longjump out ofreadline()
. The longjump is called from a plain-old-frame function that doesn't have any destructors on the stack.The behaviour in that case is unspecified at the moment but it is also safe with the current implementation of rustc, and it should be made formally safe in a future version of rust: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md. If that's a concern we could move our method to a
.c
file, but the implementation proposed in this PR seems safe enough.Dispatch an interrupt message from Control to StdIn so that the latter doesn't wait for an input reply that is never coming. It was easiest to implement this with a simple channel, but this did add some complications inside StdIn because of the need to pump the channel continuously. This could be simplified in the future if we implement a pub/sub mechanism.
I noticed another race condition between interrupts and exec-request messages, now documented in comments. To fix it, we could manage the Shell and Control sockets on the common message event thread implemented in #58. The Control messages would need to be handled in a blocking way to ensure subscribers are notified before the next incoming message is processed.