Skip to content

tool_doswin: fix stdin data truncation - #22682

Closed
jay wants to merge 5 commits into
curl:masterfrom
jay:fix_stdin_windows
Closed

jay wants to merge 5 commits into
curl:masterfrom
jay:fix_stdin_windows

Conversation

@jay

@jay jay commented Aug 26, 2026

Copy link
Copy Markdown
Member
  • Don't do premature write shutdown (SHUT_WR) on the socket used to read the stdin data (aka socket_r).

  • Don't do premature read shutdown (SHUT_RD) on the socket used to write the stdin data (aka socket_w).

  • Remove broken setsockopt call for SO_DONTLINGER on socket_r.

  • Treat ReadFile receiving 0 bytes as valid (it seems this is allowed). (removed because mingw uses it to signal eof)

  • Wait until all data sent from socket_w is acknowledged by socket_r.

Prior to this change a partial shutdown of the write side of the read socket that the main thread reads stdin data from (socket_r) happened before reading the data from the socket. The partial shutdown caused a FIN to be sent which lingered for ~2 minutes (typical OS configured time), and after that time the connection was terminated.

Theory of operation to read stdin on windows is a connection is established to relay stdin from a dedicated thread (write to socket_w) to the main thread (read from socket_r) so reading stdin is non-blocking. Data truncation would occur if this process was not completed by the OS FIN reply wait time.

Ref: #22383 (comment)

Closes #xxxx


/cc @11soda11 @denandz

- Don't do premature write shutdown (SHUT_WR) on the socket used to read
  the stdin data (aka socket_r).

- Remove broken setsockopt call for SO_DONTLINGER on socket_r.

- Treat ReadFile receiving 0 bytes as valid (it seems this is allowed).

Prior to this change a partial shutdown of the write side of the read
socket that the main thread reads stdin data from (socket_r) happened
before reading the data from the socket. The partial shutdown caused a
FIN to be sent which lingered for ~2 minutes (typical OS configured
time), and after that time the connection was terminated.

Theory of operation to read stdin on windows is a connection is
established to relay stdin from a dedicated thread (write to socket_w)
to the main thread (read from socket_r) so reading stdin is
non-blocking. Data truncation would occur if this process was not
completed by the linger time.

Ref: curl#22383 (comment)

Closes #xxxx
@jay jay added cmdline tool Windows Windows-specific labels Aug 26, 2026
- wait for all data to be received by the main thread
@jay
jay force-pushed the fix_stdin_windows branch from 828e3bb to 7428143 Compare August 26, 2026 18:43
Prior to this change treating 0 bytes as valid and not eof caused test
1498 (upload arbitrary data from stdin) to hang in mingw CI jobs.
@jay
jay force-pushed the fix_stdin_windows branch from 25f14da to 45bddb6 Compare August 28, 2026 07:18
jay added 2 commits August 30, 2026 01:05
we need to call recv on this socket and wait for shutdown.
the wait time on shutdown and recv is not controllable by LINGER wait
time which is exclusively used by closesocket
@jay
jay marked this pull request as ready for review August 30, 2026 07:45
@jay

jay commented Aug 31, 2026

Copy link
Copy Markdown
Member Author

Can someone take a look at this so I can land it before the release?

Copilot AI 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.

🟢 Approval recommended

Pull request overview

Fixes Windows asynchronous stdin relay truncation by correcting socket shutdown sequencing.

Changes:

  • Removes premature half-shutdowns and invalid SO_DONTLINGER usage.
  • Sends FIN after stdin EOF and waits for the receiving socket to close.
  • Documents observed Windows pipe EOF behavior.
File summaries
File Description
src/tool_doswin.c Corrects Windows stdin relay socket lifecycle and EOF handling.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@jay jay closed this in c303355 Sep 1, 2026
@jay
jay deleted the fix_stdin_windows branch September 1, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cmdline tool Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

2 participants