Skip to content

userspace socket: treat a closed peer handle as writable - #43976

Merged
botengyao merged 13 commits into
envoyproxy:mainfrom
pianiststickman:io-handle-write
Mar 31, 2026
Merged

botengyao merged 13 commits into
envoyproxy:mainfrom
pianiststickman:io-handle-write

Conversation

@pianiststickman

@pianiststickman pianiststickman commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message: userspace socket: treat a closed peer handle as writable
Additional Description:
If one side of an internal listener connection closes, subsequent writes from the other side may result in data waiting to be flushed on a half-close. For connections managed by the HCM, this is covered by the delayed close timer, but TcpProxy does not implement a delayed close timer and therefore the connection leak in #38148 can persist.

The fix here is treat a closed peer handle as writable in order to get the connection to attempt a write and error out. (This is analogous to Linux signalling EPOLLOUT along with EPOLLHUP when a peer closes for reads; without it, an epoll consumer on the other side may continue to write-block a socket indefinitely, which is exactly what's happening here but in userspace.)
Risk Level: low; subsequent calls to write() are guarded against peer being nullptr, and the error when calling write() is the point.
Testing: new CI and integration test that reproduces the issue
Docs Changes: n/a
Release Notes: n/a
Fixes #38148

@repokitteh-read-only

Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #43976 was opened by pianiststickman.

see: more, trace.

@pianiststickman pianiststickman changed the title userspace socket: trigger Write event when peer handle is closed userspace socket: treat a closed peer handle as writable Mar 17, 2026
@pianiststickman
pianiststickman marked this pull request as ready for review March 19, 2026 01:38
@paul-r-gall

Copy link
Copy Markdown
Contributor

/assign @botengyao

@wbpcode

wbpcode commented Mar 25, 2026

Copy link
Copy Markdown
Member

gently ping @botengyao

@botengyao botengyao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for addressing this! could you also add a bug fix changelog for this?

/wait

@botengyao

Copy link
Copy Markdown
Member

/retest

@botengyao botengyao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, thanks!

@botengyao
botengyao merged commit f50d90f into envoyproxy:main Mar 31, 2026
29 checks passed
TAOXUY pushed a commit to TAOXUY/envoy that referenced this pull request Apr 1, 2026
…43976)

Commit Message: userspace socket: treat a closed peer handle as writable
Additional Description:
If one side of an internal listener connection closes, subsequent writes
from the other side may result in data waiting to be flushed on a
half-close. For connections managed by the HCM, this is covered by the
delayed close timer, but TcpProxy does not implement a delayed close
timer and therefore the connection leak in envoyproxy#38148 can persist.

The fix here is treat a closed peer handle as writable in order to get
the connection to attempt a write and error out. (This is analogous to
Linux signalling `EPOLLOUT` along with `EPOLLHUP` when a peer closes for
reads; without it, an epoll consumer on the other side may continue to
write-block a socket indefinitely, which is exactly what's happening
here but in userspace.)
Risk Level: low; subsequent calls to `write()` are guarded against peer
being nullptr, and the error when calling `write()` is the point.
Testing: new CI and integration test that reproduces the issue
Docs Changes: n/a
Release Notes: n/a
Fixes envoyproxy#38148

---------

Signed-off-by: Eugene Chan <[email protected]>
Signed-off-by: pianiststickman <[email protected]>
Signed-off-by: Xuyang Tao <[email protected]>
citrus7 pushed a commit to citrus7/envoy that referenced this pull request Apr 1, 2026
…43976)

Commit Message: userspace socket: treat a closed peer handle as writable
Additional Description:
If one side of an internal listener connection closes, subsequent writes
from the other side may result in data waiting to be flushed on a
half-close. For connections managed by the HCM, this is covered by the
delayed close timer, but TcpProxy does not implement a delayed close
timer and therefore the connection leak in envoyproxy#38148 can persist.

The fix here is treat a closed peer handle as writable in order to get
the connection to attempt a write and error out. (This is analogous to
Linux signalling `EPOLLOUT` along with `EPOLLHUP` when a peer closes for
reads; without it, an epoll consumer on the other side may continue to
write-block a socket indefinitely, which is exactly what's happening
here but in userspace.)
Risk Level: low; subsequent calls to `write()` are guarded against peer
being nullptr, and the error when calling `write()` is the point.
Testing: new CI and integration test that reproduces the issue
Docs Changes: n/a
Release Notes: n/a
Fixes envoyproxy#38148

---------

Signed-off-by: Eugene Chan <[email protected]>
Signed-off-by: pianiststickman <[email protected]>
Signed-off-by: Jonathan Wu <[email protected]>
nshipilov pushed a commit to nshipilov/envoy that referenced this pull request Apr 13, 2026
…43976)

Commit Message: userspace socket: treat a closed peer handle as writable
Additional Description:
If one side of an internal listener connection closes, subsequent writes
from the other side may result in data waiting to be flushed on a
half-close. For connections managed by the HCM, this is covered by the
delayed close timer, but TcpProxy does not implement a delayed close
timer and therefore the connection leak in envoyproxy#38148 can persist.

The fix here is treat a closed peer handle as writable in order to get
the connection to attempt a write and error out. (This is analogous to
Linux signalling `EPOLLOUT` along with `EPOLLHUP` when a peer closes for
reads; without it, an epoll consumer on the other side may continue to
write-block a socket indefinitely, which is exactly what's happening
here but in userspace.)
Risk Level: low; subsequent calls to `write()` are guarded against peer
being nullptr, and the error when calling `write()` is the point.
Testing: new CI and integration test that reproduces the issue
Docs Changes: n/a
Release Notes: n/a
Fixes envoyproxy#38148

---------

Signed-off-by: Eugene Chan <[email protected]>
Signed-off-by: pianiststickman <[email protected]>
Signed-off-by: Nick Shipilov <[email protected]>
krinkinmu pushed a commit to grnmeira/envoy that referenced this pull request Apr 20, 2026
…43976)

Commit Message: userspace socket: treat a closed peer handle as writable
Additional Description:
If one side of an internal listener connection closes, subsequent writes
from the other side may result in data waiting to be flushed on a
half-close. For connections managed by the HCM, this is covered by the
delayed close timer, but TcpProxy does not implement a delayed close
timer and therefore the connection leak in envoyproxy#38148 can persist.

The fix here is treat a closed peer handle as writable in order to get
the connection to attempt a write and error out. (This is analogous to
Linux signalling `EPOLLOUT` along with `EPOLLHUP` when a peer closes for
reads; without it, an epoll consumer on the other side may continue to
write-block a socket indefinitely, which is exactly what's happening
here but in userspace.)
Risk Level: low; subsequent calls to `write()` are guarded against peer
being nullptr, and the error when calling `write()` is the point.
Testing: new CI and integration test that reproduces the issue
Docs Changes: n/a
Release Notes: n/a
Fixes envoyproxy#38148

---------

Signed-off-by: Eugene Chan <[email protected]>
Signed-off-by: pianiststickman <[email protected]>
yanavlasov pushed a commit that referenced this pull request Oct 21, 2026
…48409)

An internal connection can enter `CloseAfterFlush` while its peer's
receive buffer is full, leaving only Write enabled and no callback
scheduled. If the peer then closes, the EOF notification cannot wake
that writer, so the connection can remain stuck retaining its pending
output.

Activate an enabled Write event after clearing the peer state. The
pending flush then observes the write error and closes. This complements
#43976, which makes a closed peer write-unblocked when readiness is
checked but does not schedule a callback for a writer that was already
blocked.

Risk Level: Medium
Runtime Guard: N/A

Docs Changes: N/A
Release Notes: Added a bug-fix fragment describing retained-buffer
cleanup.

AI assistance: Codex assisted with investigation, implementation, tests,
and review.

Signed-off-by: Harry Bagdi <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…nvoyproxy#48409)

An internal connection can enter `CloseAfterFlush` while its peer's
receive buffer is full, leaving only Write enabled and no callback
scheduled. If the peer then closes, the EOF notification cannot wake
that writer, so the connection can remain stuck retaining its pending
output.

Activate an enabled Write event after clearing the peer state. The
pending flush then observes the write error and closes. This complements
envoyproxy#43976, which makes a closed peer write-unblocked when readiness is
checked but does not schedule a callback for a writer that was already
blocked.

Risk Level: Medium
Runtime Guard: N/A

Docs Changes: N/A
Release Notes: Added a bug-fix fragment describing retained-buffer
cleanup.

AI assistance: Codex assisted with investigation, implementation, tests,
and review.

Signed-off-by: Harry Bagdi <[email protected]>
(cherry picked from commit 30a4db3)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…nvoyproxy#48409)

An internal connection can enter `CloseAfterFlush` while its peer's
receive buffer is full, leaving only Write enabled and no callback
scheduled. If the peer then closes, the EOF notification cannot wake
that writer, so the connection can remain stuck retaining its pending
output.

Activate an enabled Write event after clearing the peer state. The
pending flush then observes the write error and closes. This complements
envoyproxy#43976, which makes a closed peer write-unblocked when readiness is
checked but does not schedule a callback for a writer that was already
blocked.

Risk Level: Medium
Runtime Guard: N/A

Docs Changes: N/A
Release Notes: Added a bug-fix fragment describing retained-buffer
cleanup.

AI assistance: Codex assisted with investigation, implementation, tests,
and review.

Signed-off-by: Harry Bagdi <[email protected]>

Backport note (release/v1.39): `onPeerDestroy()` is inline in
`io_handle_impl.h` on this branch, so the wakeup goes there. The
connection test goes in `connection_compatbility_test.cc` (renamed by
envoyproxy#47867, not on this branch) and is not parameterized on
`enable_send_rst_on_user_space_socket`, which this branch does not have.

(cherry picked from commit 30a4db3)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit that referenced this pull request Nov 17, 2026
…48409)

An internal connection can enter `CloseAfterFlush` while its peer's
receive buffer is full, leaving only Write enabled and no callback
scheduled. If the peer then closes, the EOF notification cannot wake
that writer, so the connection can remain stuck retaining its pending
output.

Activate an enabled Write event after clearing the peer state. The
pending flush then observes the write error and closes. This complements
#43976, which makes a closed peer write-unblocked when readiness is
checked but does not schedule a callback for a writer that was already
blocked.

Risk Level: Medium
Runtime Guard: N/A

Docs Changes: N/A
Release Notes: Added a bug-fix fragment describing retained-buffer
cleanup.

AI assistance: Codex assisted with investigation, implementation, tests,
and review.

Signed-off-by: Harry Bagdi <[email protected]>
(cherry picked from commit 30a4db3)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…nvoyproxy#48409)

An internal connection can enter `CloseAfterFlush` while its peer's
receive buffer is full, leaving only Write enabled and no callback
scheduled. If the peer then closes, the EOF notification cannot wake
that writer, so the connection can remain stuck retaining its pending
output.

Activate an enabled Write event after clearing the peer state. The
pending flush then observes the write error and closes. This complements
envoyproxy#43976, which makes a closed peer write-unblocked when readiness is
checked but does not schedule a callback for a writer that was already
blocked.

Risk Level: Medium
Runtime Guard: N/A

Docs Changes: N/A
Release Notes: Added a bug-fix fragment describing retained-buffer
cleanup.

AI assistance: Codex assisted with investigation, implementation, tests,
and review.

Signed-off-by: Harry Bagdi <[email protected]>

Backport note (release/v1.39): `onPeerDestroy()` is inline in
`io_handle_impl.h` on this branch, so the wakeup goes there. The
connection test goes in `connection_compatbility_test.cc` (renamed by
envoyproxy#47867, not on this branch) and is not parameterized on
`enable_send_rst_on_user_space_socket`, which this branch does not have.

(cherry picked from commit 30a4db3)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…nvoyproxy#48409)

An internal connection can enter `CloseAfterFlush` while its peer's
receive buffer is full, leaving only Write enabled and no callback
scheduled. If the peer then closes, the EOF notification cannot wake
that writer, so the connection can remain stuck retaining its pending
output.

Activate an enabled Write event after clearing the peer state. The
pending flush then observes the write error and closes. This complements
envoyproxy#43976, which makes a closed peer write-unblocked when readiness is
checked but does not schedule a callback for a writer that was already
blocked.

Risk Level: Medium
Runtime Guard: N/A

Docs Changes: N/A
Release Notes: Added a bug-fix fragment describing retained-buffer
cleanup.

AI assistance: Codex assisted with investigation, implementation, tests,
and review.

Signed-off-by: Harry Bagdi <[email protected]>

Backport note (release/v1.38): `onPeerDestroy()` is inline in
`io_handle_impl.h` on this branch, so the wakeup goes there. The
connection test goes in `connection_compatbility_test.cc` (renamed by
envoyproxy#47867, not on this branch) and is not parameterized on
`enable_send_rst_on_user_space_socket`, which this branch does not have.

(cherry picked from commit 30a4db3)
Signed-off-by: Ryan Northey <[email protected]>
phlax pushed a commit that referenced this pull request Nov 17, 2026
…48409)

An internal connection can enter `CloseAfterFlush` while its peer's
receive buffer is full, leaving only Write enabled and no callback
scheduled. If the peer then closes, the EOF notification cannot wake
that writer, so the connection can remain stuck retaining its pending
output.

Activate an enabled Write event after clearing the peer state. The
pending flush then observes the write error and closes. This complements
#43976, which makes a closed peer write-unblocked when readiness is
checked but does not schedule a callback for a writer that was already
blocked.

Risk Level: Medium
Runtime Guard: N/A

Docs Changes: N/A
Release Notes: Added a bug-fix fragment describing retained-buffer
cleanup.

AI assistance: Codex assisted with investigation, implementation, tests,
and review.

Signed-off-by: Harry Bagdi <[email protected]>

Backport note (release/v1.38): `onPeerDestroy()` is inline in
`io_handle_impl.h` on this branch, so the wakeup goes there. The
connection test goes in `connection_compatbility_test.cc` (renamed by
#47867, not on this branch) and is not parameterized on
`enable_send_rst_on_user_space_socket`, which this branch does not have.

(cherry picked from commit 30a4db3)
Signed-off-by: Ryan Northey <[email protected]>
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.

Envoy internal listener's active connections are not released with TcpProxy case

4 participants