Skip to content

tool_doswin: don't use TerminateThread in stdin relay - #22383

Closed
11soda11 wants to merge 1 commit into
curl:masterfrom
11soda11:test-branch
Closed

11soda11 wants to merge 1 commit into
curl:masterfrom
11soda11:test-branch

Conversation

@11soda11

@11soda11 11soda11 commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

It is a dangerous function. Remove usage by only starting the thread after setup completed successfully.

Ref: https://learn.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminatethread
Ref: #21467 (comment)
Ref: #18996 (comment)
Ref: #18451
Follow-up to 9a26633 #17572

@github-actions github-actions Bot added cmdline tool Windows Windows-specific labels Jul 24, 2026
Comment thread src/tool_doswin.c Resolved
Comment thread src/tool_doswin.c Resolved
Comment thread src/tool_doswin.c Resolved
@11soda11
11soda11 requested a review from vszakats July 25, 2026 16:11
@github-actions github-actions Bot added the tests label Jul 25, 2026
@11soda11

11soda11 commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor Author

I added a test because i noticed there wasn't one for async stdin, only for normal stdin upload.

the failing test seems to be unrelated

@11soda11
11soda11 force-pushed the test-branch branch 2 times, most recently from 7a477fb to 481e91f Compare July 26, 2026 00:55
Comment thread src/tool_doswin.c Resolved
Comment thread src/tool_doswin.c Resolved
Comment thread src/tool_doswin.c Resolved
Comment thread src/tool_doswin.c Resolved
Comment thread tests/http/test_07_upload.py Resolved
@11soda11
11soda11 requested a review from vszakats July 26, 2026 11:40
@11soda11
11soda11 force-pushed the test-branch branch 3 times, most recently from d68d1b2 to 971b83e Compare July 27, 2026 00:58
Comment thread tests/data/test1498 Resolved
@github-actions github-actions Bot added the CI Continuous Integration label Jul 27, 2026
Comment thread src/tool_doswin.c Resolved
@11soda11

Copy link
Copy Markdown
Contributor Author

both tests are still failing in the torture test because of the socket fd which the main thread passes to the background thread. I don't see a solution other than making valgrind ignore it somehow because the socket needs to be alive throughout the whole program lifecycle.

@vszakats

Copy link
Copy Markdown
Member

both tests are still failing in the torture test because of the socket fd which the main thread passes to the background thread. I don't see a solution other than making valgrind ignore it somehow because the socket needs to be alive throughout the whole program lifecycle.

Is there perhaps a way to wait for that thread before exit,
and/or just close handles? or do this via atexit()/signal_handler()?

@11soda11
11soda11 force-pushed the test-branch branch 3 times, most recently from b4f3e7e to f89318e Compare July 28, 2026 11:53
@11soda11

Copy link
Copy Markdown
Contributor Author

Is there perhaps a way to wait for that thread before exit, and/or just close handles? or do this via atexit()/signal_handler()?

waiting for the thread is not really an option because it is inside a blocking read call most of the time and the fd needs to be open until exit. atexit seems to work tho

@11soda11
11soda11 requested a review from vszakats July 28, 2026 12:38
@11soda11

Copy link
Copy Markdown
Contributor Author

Did you test this? I'm seeing some strange behavior with this. Sometimes it sends nothing and sometimes it sends the data but not the 0 chunk size (to signal no more data) at the end.

I had only tested it by typing into stdin directly tbh. After testing like you did, it always sent the data for me but not the concluding 0. This was due to the background thread not closing the socket/connection after reading 0 bytes from stdin. This is fixed now.

However, I wasn't able to reproduce the case where it doesn't send the data at all. Could you test it again with the new commits and tell me if it still happens pls?

@denandz

denandz commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Ill have some time this week to do some testing as well. There's a more detailed use-case in https://github.com/denandz/httpstream2tcp

@jay

jay commented Aug 11, 2026

Copy link
Copy Markdown
Member

Did you test this? I'm seeing some strange behavior with this. Sometimes it sends nothing and sometimes it sends the data but not the 0 chunk size (to signal no more data) at the end.

I had only tested it by typing into stdin directly tbh. After testing like you did, it always sent the data for me but not the concluding 0. This was due to the background thread not closing the socket/connection after reading 0 bytes from stdin. This is fixed now.

I can no longer reproduce either problem with your updated branch. The no content problem involved a debug break which was interactive, as I showed earlier. My guess is it was a timing dependent problem and had nothing to do with the debugger.

Comment thread src/tool_doswin.c Resolved

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@11soda11

Copy link
Copy Markdown
Contributor Author

I can no longer reproduce either problem with your updated branch. The no content problem involved a debug break which was interactive, as I showed earlier. My guess is it was a timing dependent problem and had nothing to do with the debugger.

I don't really have a debugger setup on windows, so I tried to test it with curl_wait_ms() to simulate a delay with no luck.

But I guess it is fixed now if you can't reproduce it either.

Comment thread src/tool_doswin.c Resolved
@jay

jay commented Aug 18, 2026

Copy link
Copy Markdown
Member

Viktor before this is landed I'd like more time to hear from @denandz to make sure everything is ok with his setup which may be more complicated than our tests.

@vszakats

Copy link
Copy Markdown
Member

Viktor before this is landed I'd like more time to hear from @denandz to make sure everything is ok with his setup which may be more complicated than our tests.

Sounds good to me. Short of his blessing, I'd be happy to land this on the next cycle, if no one else is reporting issues.

Do we know specifics about @denandz setup?

@jay

jay commented Aug 18, 2026

Copy link
Copy Markdown
Member

Do we know specifics about @denandz setup?

Yes:

Ill have some time this week to do some testing as well. There's a more detailed use-case in https://github.com/denandz/httpstream2tcp

@denandz

denandz commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Thank you for your patience, team. Busy week on my end.

I've successfully tested these changes with httpstream2tcp and OpenSSH. No issues on my end, LGTM and blessings given! 🙏

@11soda11

11soda11 commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor Author

sry for the noise. I had committed a faulty change, which I now reverted by resetting to the previous commit, so nothing has changed in total.

However, there is a flaky netbsd test which is failing now.

I don't have anything more to add to this pr, its done from my side.

@jay

jay commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

I've squashed your commits and rebased on upstream/master to prepare for landing this. I've force-pushed the proposed final commit to your branch. Since there was dual author information I used the one with your real name (also the name on your github profile). Please amend the commit meta if necessary. After it passes CI I will take a final look.

for example git commit --amend --author="YOU MUST <CHANGE@THIS>"

While reviewing this PR I found two issues related to this stdin upload area but separate from this PR so I will address them separately. The first is how curl can ignore truncated data on stdin (this is how I saw 0 content being uploaded in the debugger), filed as #22682. The second is CPU usage issue like if stdin is used like to type to peer and back and forth and it's waiting a lot, filed as #22640.

@11soda11

11soda11 commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Please amend the commit meta if necessary

done. Thank you for your work

@jay jay closed this in 67221a7 Aug 25, 2026
@jay

jay commented Aug 25, 2026

Copy link
Copy Markdown
Member

Thanks

@jay jay removed the needs-info label Aug 25, 2026
jay added a commit to jay/curl that referenced this pull request Aug 26, 2026
- 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 added a commit that referenced this pull request Sep 1, 2026
- 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.

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

Note this FIN wait time is not the same as SO_LINGER wait time, the
latter only used by closesocket.

Ref: #22383 (comment)

Closes #22682
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Continuous Integration cmdline tool tests Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

6 participants