Skip to content

test: Close the mcp_router backend connections before destroying them - #48970

Merged
phlax merged 2 commits into
envoyproxy:mainfrom
phlax:test-mcp-router-close-backends
Nov 3, 2026
Merged

phlax merged 2 commits into
envoyproxy:mainfrom
phlax:test-mcp-router-close-backends

Conversation

@phlax

@phlax phlax commented Nov 3, 2026

Copy link
Copy Markdown
Member

The mcp_router fixture keeps its fake backend connections in its own members rather than fake_upstream_connection_, so
cleanupUpstreamAndDownstream() never closes them and they are destroyed with the fixture while the fake upstream thread may still be draining the response write. That frees the HTTP/1 server codec under the write, and the codec's flood-tracking sentinel fragment calls back into it when the write buffer drains:

codec_impl.cc:1067: runtime error: member call on address ... which
does not point to an object of type 'ServerConnectionImpl'

(asan, HeaderForwardingDisallowedWinsOverForwardAll/1, main runs 41674010049 and 41675082565).

Close both backend connections and wait for the disconnect in TearDown before the standard cleanup, as cleanupUpstreamAndDownstream() does for fake_upstream_connection_.

The mcp_router fixture keeps its fake backend connections in its own
members rather than fake_upstream_connection_, so
cleanupUpstreamAndDownstream() never closes them and they are destroyed
with the fixture while the fake upstream thread may still be draining
the response write. That frees the HTTP/1 server codec under the write,
and the codec's flood-tracking sentinel fragment calls back into it
when the write buffer drains:

  codec_impl.cc:1067: runtime error: member call on address ... which
  does not point to an object of type 'ServerConnectionImpl'

(asan, HeaderForwardingDisallowedWinsOverForwardAll/1, main runs
41674010049 and 41675082565).

Close both backend connections and wait for the disconnect in TearDown
before the standard cleanup, as cleanupUpstreamAndDownstream() does for
fake_upstream_connection_.

Signed-off-by: Ryan Northey <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified in the backend-connection teardown cleanup change.

No open issues 🎉

What changed in this PR

This PR addresses a teardown use-after-free in the MCP router integration tests by closing fixture-owned backend connections before they are destroyed.

Changes:

  • Close both backend connections and wait for disconnection before standard cleanup.
  • Document why explicit cleanup is needed to prevent callbacks into a freed HTTP/1 codec.
File Description
test/​extensions/​filters/​http/​mcp_router/​mcp_router_integration_test.cc Update TearDown() to close and await disconnection of both backend connections.

Review effort: Balanced

Comment on lines +275 to +276
ASSERT_TRUE((*connection)->close());
ASSERT_TRUE((*connection)->waitForDisconnect());

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.

If Envoy has already closed this backend connection (for example after an error or reset), close() returns right away (fake_upstream.cc:509), and so does waitForDisconnect(). Neither waits for work the test queued earlier with postToConnectionThread (for example from a final encodeData), and that work holds a raw pointer to the connection. The connection is then freed with the fixture, before the fake upstreams stop, so the same use-after-free can still happen in a different order. cleanupUpstreamAndDownstream() avoids this by calling waitForNoPost() before reset() (http_integration.cc:545). Could we do the same here? Using RELEASE_ASSERT, as that function does, would also keep a failure here from skipping the rest of cleanup.

Suggested change
ASSERT_TRUE((*connection)->close());
ASSERT_TRUE((*connection)->waitForDisconnect());
auto result = (*connection)->close();
RELEASE_ASSERT(result, result.message());
result = (*connection)->waitForDisconnect();
RELEASE_ASSERT(result, result.message());
result = (*connection)->waitForNoPost();
RELEASE_ASSERT(result, result.message());
connection->reset();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point, thanks - applied as suggested in 23f4a2c: close, waitForDisconnect, waitForNoPost, reset, with RELEASE_ASSERT throughout.

@phlax
phlax force-pushed the test-mcp-router-close-backends branch from f011fff to 23f4a2c Compare November 3, 2026 17:34
…re releasing them

If Envoy already closed a backend connection, close() and
waitForDisconnect() return at once, but work the test posted to the
upstream thread (the final encodeData) can still hold the connection.
Wait for that to drain and release the connection, as
cleanupUpstreamAndDownstream() does, under RELEASE_ASSERT so a failure
cannot skip the rest of the cleanup.

Signed-off-by: Ryan Northey <[email protected]>
@phlax
phlax force-pushed the test-mcp-router-close-backends branch from 23f4a2c to 02ab9ef Compare November 3, 2026 17:35
@phlax

phlax commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

apologies for force push over existing commit - bad bot

@phlax
phlax enabled auto-merge (squash) November 3, 2026 18:00
@phlax
phlax disabled auto-merge November 3, 2026 18:04
@phlax
phlax merged commit 602099b into envoyproxy:main Nov 3, 2026
30 of 31 checks passed
phlax added a commit to phlax/envoy that referenced this pull request Nov 12, 2026
…envoyproxy#48970)

The mcp_router fixture keeps its fake backend connections in its own
members rather than fake_upstream_connection_, so
cleanupUpstreamAndDownstream() never closes them and they are destroyed
with the fixture while the fake upstream thread may still be draining
the response write. That frees the HTTP/1 server codec under the write,
and the codec's flood-tracking sentinel fragment calls back into it when
the write buffer drains:

  codec_impl.cc:1067: runtime error: member call on address ... which
  does not point to an object of type 'ServerConnectionImpl'

(asan, HeaderForwardingDisallowedWinsOverForwardAll/1, main runs
41674010049 and 41675082565).

Close both backend connections and wait for the disconnect in TearDown
before the standard cleanup, as cleanupUpstreamAndDownstream() does for
fake_upstream_connection_.

---------

Signed-off-by: Ryan Northey <[email protected]>
jwendell pushed a commit that referenced this pull request Nov 12, 2026
…#48970)

The mcp_router fixture keeps its fake backend connections in its own
members rather than fake_upstream_connection_, so
cleanupUpstreamAndDownstream() never closes them and they are destroyed
with the fixture while the fake upstream thread may still be draining
the response write. That frees the HTTP/1 server codec under the write,
and the codec's flood-tracking sentinel fragment calls back into it when
the write buffer drains:

  codec_impl.cc:1067: runtime error: member call on address ... which
  does not point to an object of type 'ServerConnectionImpl'

(asan, HeaderForwardingDisallowedWinsOverForwardAll/1, main runs
41674010049 and 41675082565).

Close both backend connections and wait for the disconnect in TearDown
before the standard cleanup, as cleanupUpstreamAndDownstream() does for
fake_upstream_connection_.

---------

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.

3 participants