-
Notifications
You must be signed in to change notification settings - Fork 5k
node:net: reset the stream state when connect() reuses a half-closed socket #38980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
edad317
44e6a05
33e982c
5314d9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2134,8 +2134,15 @@ | |
|
|
||
| if (!this._handle) { | ||
| this._handle = newDetachedSocket(typeof this[bunTlsSymbol] === "function"); | ||
| initSocketHandle(this); | ||
| } | ||
| // Unlike node (where connecting a handle that is still connected fails with | ||
| // EISCONN), a handle whose previous connection is still open or half-closed is | ||
| // reused: doConnect closes that connection and connects the same handle again. | ||
| // The stream state has to be re-initialized for it as well, otherwise a | ||
| // connect() issued after end() but before the previous connection finished | ||
| // closing keeps its ended/finished state and the first write on the new | ||
| // connection fails with ERR_STREAM_WRITE_AFTER_END. | ||
| initSocketHandle(this); | ||
|
Check warning on line 2145 in src/js/node/net.ts
|
||
|
claude[bot] marked this conversation as resolved.
Comment on lines
+2136
to
+2137
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The hostname variant of this fix has a race window: Extended reasoning...What the gap is
The code path that re-corrupts the reset stateDuring that DNS window the old (still-attached) connection can deliver its FIN or close to
The Why the existing tests don't cover itEvery new test in this PR — the rewritten "should allow reconnecting after end()" and all four "connect() while the previous connection is half-closed" cases — connects to Step-by-step trace
Why this is a nit, not a blocker
Per REVIEW.md's "Cover the variant matrix, not just the repro" this is worth noting — the hostname variant is an untested gap — but not worth blocking a PR that fixes the flaky test it set out to fix and strictly improves every case. |
||
|
|
||
| if (!pipe) { | ||
| lookupAndConnect(this, options); | ||
|
|
@@ -4261,10 +4268,13 @@ | |
| return arr; | ||
| } | ||
|
|
||
| // Called when creating new Socket, or when re-using a closed Socket | ||
| // Called before every connect(): on a new Socket, and whenever a Socket is | ||
| // re-used for another connection (after a close, or while the previous | ||
| // connection is still being torn down). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trimmed to one line in 33e982c. |
||
| function initSocketHandle(self) { | ||
| self._undestroy(); | ||
| self._sockname = null; | ||
| self._peername = null; | ||
| self[kclosed] = false; | ||
| self[kended] = false; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trimmed to one line in 33e982c / 5314d9e.