Skip to content

Report WS_WANT_WRITE only while output is queued - #1404

Open
ejohnstown wants to merge 2 commits into
wolfSSL:masterfrom
ejohnstown:sftp-stale-want-write
Open

ejohnstown wants to merge 2 commits into
wolfSSL:masterfrom
ejohnstown:sftp-stale-want-write

Conversation

@ejohnstown

@ejohnstown ejohnstown commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

A non-blocking SFTP server or client could leave wolfSSH_get_error() at
WS_WANT_WRITE with nothing queued, so a caller waited on a socket that had
nothing to send.

  • A flush that empties the output buffer clears a stale WS_WANT_WRITE, and the
    worker returns WS_SUCCESS once its own flush sends a reply that blocked
  • SFTP sends continue past the packet-size cap
  • A server reply or client request held by the window or a rekey reports
    WS_WINDOW_FULL or WS_REKEYING

Issue: #1401

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #1404

Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 5 of 5 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Max

@ejohnstown

Copy link
Copy Markdown
Contributor Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟣 src/wolfsftp.c — pre-existing: Non-blocking SFTP clients still get wolfSSH_get_error() == WS_WANT_WRITE with nothing queued when the peer window or a rekey holds a request. The remap to WS_WINDOW_FULL/WS_REKEYING was added only to the server path in wolfSSH_SFTP_read (~2027). wolfSSH_SFTP_buffer_send_finish still writes WS_WANT_WRITE whenever idx < sz (685-687). Fix: do the window/rekey mapping inside wolfSSH_SFTP_buffer_send_finish, so the server site and all 6 client sites are covered. Same pattern at 7 sites (wolfsftp.c:2024, 8452, 8639, 8777, 9001, 9289, 9851).

    Why this was flagged

    Take a non-blocking client calling wolfSSH_SFTP_SetSTAT, Open, Close, Remove or a similar request whose packet is bigger than the peer's remaining channel window. Another trigger is a flush that starts a rekey. The new loop in wolfSSH_SFTP_buffer_send (wolfsftp.c:651-655) stops with idx < sz and nothing in the output buffer. wolfSSH_SFTP_buffer_send_finish then sets ssh->error = WS_WANT_WRITE (wolfsftp.c:686), and the client sites return WS_FATAL_ERROR (e.g. wolfsftp.c:8452-8455, 8639-8642). The caller then waits for the socket to become writable. It is always writable, so the caller either busy-loops or never runs the worker that would receive the WINDOW_ADJUST or finish the KEX. Issue #1401 describes exactly this on the server side. The PR fixes it only in wolfSSH_SFTP_read (wolfsftp.c:2026-2035), so client paths behave as they do on the base branch.

    Verification: pre-existing. The client sites return WS_FATAL_ERROR with that error left in place: src/wolfsftp.c:8452-8455 (SetSTAT), 8639-8642 (Open), 9289-9292 (MKDIR). Only the server path in wolfSSH_SFTP_read (src/wolfsftp.c:2024-2036) remaps the error to WS_REKEYING or WS_WINDOW_FULL. So on the client paths, wolfSSH_get_error() still reports WS_WANT_WRITE while nothing is queued.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 00:11
@ejohnstown

Copy link
Copy Markdown
Contributor Author

Re the pre-existing client finding: done. wolfSSH_SFTP_buffer_send_finish() now reports WS_REKEYING or WS_WINDOW_FULL when a rekey or the peer's window stopped the send with nothing queued, so the server reply and every client request share it, and the server site in wolfSSH_SFTP_read() no longer remaps on its own. A new regress test holds Open, SendReadPacket, SendWritePacket, SetSTAT and MKDIR with an 8-byte window and with a rekey after the first packet, and checks the reported error and that nothing is queued. Rename is not in it: it sends a STAT first, so the test does not reach its own request.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Bundled SFTP client loops treat WS_WINDOW_FULL—and WS_REKEYING for MKDIR—as terminal errors, abandoning retained requests instead of processing peer messages and resuming the operation.

1 open issue
What changed in this PR

Addresses #1401 by aligning non-blocking write-wait reporting with queued output across wolfSSH’s transport, worker, and SFTP paths.

Changes:

  • Clears stale WS_WANT_WRITE after completed flushes and returns worker success when its flush resolves a blocked reply.
  • Continues SFTP sends across packet-size limits and distinguishes window exhaustion from rekey waits.
  • Adds regression coverage for flush completion and stalled SFTP requests and replies.
File Description
wolfssh/​wolfsftp.h Declares an internal SFTP send-completion test hook.
tests/​regress.c Tests stale write status, packet limits, window stalls, and rekeying.
src/​wolfsftp.c Sends across packet limits and reports specific flow-control statuses.
src/​ssh.c Returns success after flushing a previously blocked reply.
src/​internal.c Clears stale WS_WANT_WRITE when output finishes flushing.

Review effort: Balanced

Comment thread src/wolfsftp.c Resolved
A non-blocking SFTP server could leave wolfSSH_get_error() at
WS_WANT_WRITE with nothing queued, so a caller waited on a socket that
had nothing to send.

- internal: a flush that empties the output buffer clears a stale
  WS_WANT_WRITE, as GetInputData() does for WS_WANT_READ
- ssh: wolfSSH_worker() returns WS_SUCCESS once its own flush sends the
  reply DoReceive() blocked on
- sftp: wolfSSH_SFTP_buffer_send() keeps sending past a packet-size
  cap until the buffer is done, the socket blocks or the window empties
- sftp: the server reports a reply held back by the window or a rekey
  as WS_WINDOW_FULL or WS_REKEYING
- regress: the stale flush error, the worker flush, the packet cap and
  a server reply held by the window or a rekey

Issue: wolfSSL#1401

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #1404

Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 1 of 3 in-scope changed file(s) opened by the reviewer; not opened: examples/sftpclient/sftpclient.c, tests/regress.c

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

A client request stopped by the peer's window or a rekey, with nothing
queued, now reports WS_WINDOW_FULL or WS_REKEYING instead of
WS_WANT_WRITE. wolfSSH_SFTP_buffer_send_finish() makes the call, so
the server reply and every client request share it.

- sftp: Open, Rename, Put's open and CHMOD keep their state on those
  codes, so a half-sent request is resumed, not sent again
- sftpclient: mkdir, creat, chmod, get, put and rename retry on them
- regress: each held request resumes once the window opens, goes out
  once and clears its state

Issue: wolfSSL#1401
@ejohnstown
ejohnstown force-pushed the sftp-stale-want-write branch from f48585b to 9280ce1 Compare December 2, 2026 23:24
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

Successfully merging this pull request may close these issues.

4 participants