Skip to content

network: wake blocked internal socket writers when the peer closes - #48409

Merged
yanavlasov merged 1 commit into
envoyproxy:mainfrom
hbagdi:codex/internal-socket-peer-close-wakeup
Oct 21, 2026
Merged

yanavlasov merged 1 commit into
envoyproxy:mainfrom
hbagdi:codex/internal-socket-peer-close-wakeup

Conversation

@hbagdi

@hbagdi hbagdi commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message: network: wake blocked internal socket writers when the peer closes

Additional Description:
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.

@hbagdi
hbagdi requested a review from kyessenov as a code owner October 16, 2026 17:58
@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/runtime-guard-changes: FYI only for changes made to (source/common/runtime/runtime_features.cc).

🐱

Caused by: #48409 was opened by hbagdi.

see: more, trace.

Wake an enabled Write event after detaching a user-space socket peer so
connections waiting in CloseAfterFlush can observe the write error and
release their pending output. Add regressions for peer destruction after
a blocked flush, enabled-event handling, and pending-buffer release.

Signed-off-by: Harry Bagdi <[email protected]>
@hbagdi
hbagdi force-pushed the codex/internal-socket-peer-close-wakeup branch from a7b1327 to 03d8f5f Compare October 16, 2026 19:21
@hbagdi

hbagdi commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@kyessenov a gentle ping for a review!

@yanavlasov
yanavlasov merged commit 30a4db3 into envoyproxy:main Oct 21, 2026
27 checks passed
@damluji-pgt

Copy link
Copy Markdown

/backport

This fixes a hang we hit in production with Istio ambient, and we'd like to ask for a backport to release/v1.40 and release/v1.39 (and older supported branches if they are affected).

Istio's gateways reach backends through an internal listener that runs tcp_proxy in CONNECT tunneling mode, sharing one HTTP/2 connection per worker per backend pod. When a client that is downloading a large response disconnects, the router closes its internal connection to that listener, but the tcp_proxy side only sees end_stream=true and stays blocked on the write it could not flush. Its CONNECT stream keeps its flow-control window until the tcp_proxy idle timeout (1 h). Once a couple of these fill the shared connection window, every request on that HTTP/2 connection hangs (istio/istio#62641).

Standalone reproduction with plain Envoy (one worker): HTTP listener → internal listener with tcp_proxy CONNECT tunneling → HTTP/2 → a CONNECT terminator → go-httpbin. Four clients stall a 64 MiB download, then disconnect; 20 quick requests with a 3 s timeout before and after.

Envoy 4 clients stalled after they disconnect
v1.39.6 0/20 0/20, 24 tunnel streams still active
dev-24a950b6 (includes this PR) 0/20 20/20, 0 tunnel streams

Istio 1.31 builds on release/v1.39 and Istio 1.32 on release/v1.40, so a backport would let the fix reach Istio patch releases instead of waiting for Istio 1.33.

envoy.yaml (run with --concurrency 1; backend is go-httpbin)
bootstrap_extensions:
  - name: envoy.bootstrap.internal_listener
    typed_config:
      "@type": type.googleapis.com/envoy.extensions.bootstrap.internal_listener.v3.InternalListener
admin:
  address: {socket_address: {address: 0.0.0.0, port_value: 9901}}
static_resources:
  listeners:
    - name: ingress
      address: {socket_address: {address: 0.0.0.0, port_value: 10000}}
      filter_chains:
        - filters:
            - name: envoy.filters.network.http_connection_manager
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
                stat_prefix: ingress
                codec_type: AUTO
                route_config:
                  virtual_hosts:
                    - name: all
                      domains: ["*"]
                      routes:
                        - match: {prefix: "/"}
                          route: {cluster: to_tunnel, timeout: 0s}
                http_filters:
                  - name: envoy.filters.http.router
                    typed_config: {"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router}
    - name: connect_originate
      internal_listener: {}
      filter_chains:
        - filters:
            - name: envoy.filters.network.tcp_proxy
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
                stat_prefix: connect_originate
                cluster: tunnel
                tunneling_config: {hostname: "backend:8080"}
    - name: connect_terminate
      address: {socket_address: {address: 127.0.0.1, port_value: 15008}}
      filter_chains:
        - filters:
            - name: envoy.filters.network.http_connection_manager
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
                stat_prefix: terminate
                codec_type: HTTP2
                http2_protocol_options: {allow_connect: true}
                upgrade_configs: [{upgrade_type: CONNECT}]
                route_config:
                  virtual_hosts:
                    - name: all
                      domains: ["*"]
                      routes:
                        - match: {connect_matcher: {}}
                          route:
                            cluster: backend
                            timeout: 0s
                            upgrade_configs: [{upgrade_type: CONNECT, connect_config: {}}]
                http_filters:
                  - name: envoy.filters.http.router
                    typed_config: {"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router}
  clusters:
    - name: to_tunnel
      load_assignment:
        cluster_name: to_tunnel
        endpoints:
          - lb_endpoints:
              - endpoint: {address: {envoy_internal_address: {server_listener_name: connect_originate}}}
      transport_socket:
        name: envoy.transport_sockets.internal_upstream
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.transport_sockets.internal_upstream.v3.InternalUpstreamTransport
          transport_socket:
            name: envoy.transport_sockets.raw_buffer
            typed_config: {"@type": type.googleapis.com/envoy.extensions.transport_sockets.raw_buffer.v3.RawBuffer}
    - name: tunnel
      typed_extension_protocol_options:
        envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
          "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
          explicit_http_config: {http2_protocol_options: {allow_connect: true}}
      load_assignment:
        cluster_name: tunnel
        endpoints:
          - lb_endpoints:
              - endpoint: {address: {socket_address: {address: 127.0.0.1, port_value: 15008}}}
    - name: backend
      type: STRICT_DNS
      load_assignment:
        cluster_name: backend
        endpoints:
          - lb_endpoints:
              - endpoint: {address: {socket_address: {address: backend, port_value: 8080}}}

The investigation and this reproduction were done with Claude Code; the results above come from running it.

@repokitteh-read-only repokitteh-read-only Bot added the backport/review Request to backport to stable releases label Nov 5, 2026
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 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
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.37): `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
`enable_send_rst_on_user_space_socket`, which this branch does not have. The flag
that the peer's close sets is `write_shutdown_` here (renamed `sent_eof_` later).

(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
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.37): `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
`enable_send_rst_on_user_space_socket`, which this branch does not have. The flag
that the peer's close sets is `write_shutdown_` here (renamed `sent_eof_` later). The tests
use `isWritable()`, which `canReceiveData()` was called on this branch.

(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
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.37): `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
`enable_send_rst_on_user_space_socket`, which this branch does not have. The flag
that the peer's close sets is `write_shutdown_` here (renamed `sent_eof_` later). The tests
use `isWritable()`, which `canReceiveData()` was called on this branch.
`close()` tells the peer the stream has ended before the peer is told of the destruction
here, so the two existing tests that expect one wakeup of the peer on close now expect two.

(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 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
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.37): `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
`enable_send_rst_on_user_space_socket`, which this branch does not have. The flag
that the peer's close sets is `write_shutdown_` here (renamed `sent_eof_` later). The tests
use `isWritable()`, which `canReceiveData()` was called on this branch.
`close()` tells the peer the stream has ended before the peer is told of the destruction
here, so the two existing tests that expect one wakeup of the peer on close now expect two.

(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 18, 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
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.37): `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
`enable_send_rst_on_user_space_socket`, which this branch does not have. The flag
that the peer's close sets is `write_shutdown_` here (renamed `sent_eof_` later). The tests
use `isWritable()`, which `canReceiveData()` was called on this branch.
`close()` tells the peer the stream has ended before the peer is told of the destruction
here, so the two existing tests that expect one wakeup of the peer on close now expect two.

(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

backport/review Request to backport to stable releases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants