Skip to content

Conversation

@andrewryno
Copy link
Member

@andrewryno andrewryno commented Nov 10, 2025

This fixes a data race in the forwarder which can be hit by reading/writing data during a call to (ngrok.Endpoint).CloseWithContext() and (ngrok.Agent).Disconnect().

I've been unsuccessfully trying to get a small test to demonstrate it since I ran into it in a rather complex setup for an internal test. I'll see if I can get one working. However, my internal tests are now working perfectly fine with this fix and I'm no longer getting any races.

The underlying problem is that when a muxado stream is closed it does internal writes, but the io.Copy still might be doing reads which races. This closes writes but waits for all reads to finish from io.Copy before entirely closing the connection. There was also some additional Close calls that are unnecessary since join can handle them all.

Investigated/written with the help of Amp, but it took a long time to get there lol.

@andrewryno andrewryno force-pushed the ryno/fix-race-closing-muxado-streams branch from 9c2fb30 to 7e98b95 Compare November 10, 2025 23:26
@andrewryno andrewryno force-pushed the ryno/fix-race-closing-muxado-streams branch from 7e98b95 to 93a182a Compare November 10, 2025 23:29
Copy link
Contributor

@euank euank left a comment

Choose a reason for hiding this comment

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

Half closed tcp connections are a mess, go does not make dealing with them easy, and I think this PR is doing something that is right on some technical level, but will cause some unfortunate regressions in practice...

@andrewryno andrewryno force-pushed the ryno/fix-race-closing-muxado-streams branch from 93a182a to 1d8a191 Compare November 11, 2025 19:02
Copy link
Contributor

@bmpngrok bmpngrok left a comment

Choose a reason for hiding this comment

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

After @andrewryno's update, I think (unless I'm missing something) this covers both Ryno's needs and Euan's concerns, so I'm going to go ahead and approve

Copy link
Contributor

@euank euank left a comment

Choose a reason for hiding this comment

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

lgtm, though I'd also suggest editing the PR title to match the new commit message.
Thanks!

@andrewryno andrewryno changed the title Wait for io.Copy to complete before closing connections Don't close connections twice after join completes Nov 14, 2025
@andrewryno andrewryno merged commit b3f8178 into main Nov 14, 2025
2 checks passed
@andrewryno andrewryno deleted the ryno/fix-race-closing-muxado-streams branch November 14, 2025 18:05
@jonstacks jonstacks mentioned this pull request Dec 5, 2025
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