fix(pty): re-arm mio readiness after raw socket WouldBlock on Windows - #79
Open
srstack wants to merge 1 commit into
Open
fix(pty): re-arm mio readiness after raw socket WouldBlock on Windows#79srstack wants to merge 1 commit into
srstack wants to merge 1 commit into
Conversation
mio's Windows backend is edge-triggered: after delivering an event it clears the socket's interest bits, and only re-registers them when a read or write through mio::net::TcpStream hits WouldBlock. All SSH channel I/O in this crate bypasses mio (libssh2 owns the raw socket), so after the first READABLE event the interest was never re-armed and the session never signalled readable again: the terminal displayed the initial login banner and then went permanently deaf, while writes kept working because they never needed an event. Peek one byte through the mio socket whenever libssh2 reports WouldBlock (kernel buffer drained). The peek hits WouldBlock without consuming data, which is mio's documented signal to re-register the socket interest. Unix is unaffected: mio is level-triggered there and the helper is a no-op.
There was a problem hiding this comment.
Pull request overview
Fixes a Windows-specific edge-triggered mio readiness issue in otty-pty’s SSH backend where libssh2 performs out-of-band raw-socket I/O, causing mio interests to be cleared after the first event and the session to stop receiving further output.
Changes:
- Adds a Windows-only helper (
rearm_io_events) that callsmio::net::TcpStream::peekto trigger mio’s internal re-registration behavior afterWouldBlock. - Calls this helper from the SSH
Session::readandSession::writeWouldBlockpaths to keep readiness armed.
Suppressed comments (1)
otty-pty/src/ssh.rs:163
- Same race as the read path: after a libssh2
WouldBlock,rearm_io_events()can observepeek() == Ok(_)(socket became ready again) and returnOk(())without necessarily re-arming mio. ReturningOk(0)immediately can then leave pending output stalled until a writable event that might not be delivered. Retrying the write once after re-arming makes progress when the socket becomes writable in-between.
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
self.rearm_io_events()?;
Ok(0)
},
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+144
to
+147
| Err(e) if e.kind() == io::ErrorKind::WouldBlock => { | ||
| self.rearm_io_events()?; | ||
| Ok(0) | ||
| }, |
Comment on lines
+70
to
+78
| /// Re-arm mio's edge-triggered readiness after a raw-socket WouldBlock. | ||
| /// | ||
| /// All channel I/O bypasses mio (libssh2 owns the socket), so mio's | ||
| /// Windows backend never observes the WouldBlock it uses as the signal | ||
| /// to re-register interest, leaving the session permanently deaf after | ||
| /// the first event. Peeking one byte through the mio socket hits | ||
| /// WouldBlock once the kernel buffer is drained, which triggers mio's | ||
| /// internal re-registration without consuming any data. | ||
| #[cfg(windows)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
On Windows, an SSH session displays the initial login banner/motd and then goes permanently deaf: the shell prompt never appears, and no further output arrives, while keyboard input keeps being sent successfully.
Root cause
mio's Windows backend is edge-triggered by design (
src/sys/windows/selector.rs):After delivering an event, mio clears the socket's interest bits. The interest is only re-armed when a read/write through
mio::net::TcpStreamhitsWouldBlock(seeIoSourceState::do_io, which re-registers onWouldBlock).But all SSH channel I/O in
otty-ptybypasses mio: libssh2 reads and writes the raw socket directly. So mio never observes aWouldBlock, thePOLL_RECEIVEinterest is lost after the first event, and noREADABLEis ever delivered again. Writes keep working because they never needed an event.Trace evidence from a Windows run (OTTY_LOG_FILE diagnostics): the runtime loop polls fine (waker tokens fire), one
Token(0) readableevent delivers the motd (599 bytes), then zero socket events for seconds while the server provably sends data (prompt + echoes). The same session parameters against the same server work on Linux, where mio is level-triggered.This affects any Rust project combining mio-registered sockets with out-of-band raw-socket I/O (like libssh2) on Windows.
Fix
When libssh2 reports
WouldBlock(kernel buffer drained), peek one byte through the mio socket:The peek hits
WouldBlockwithout consuming data, which is mio's documented signal (TcpStream::peek: "Need to re-register if peek returns WouldBlock to ensure the socket will receive more events once it is ready again") to re-register the full interest set. Applied on both the read and write WouldBlock paths; on unix the helper is a no-op since mio is level-triggered there.Verification
ls, full-screen programs) works normally#[cfg(not(windows))]no-op)cargo clippy -p otty-pty --all-targets --all-features -- -D warnings,cargo test -p otty-pty --all-featuresgreen