Repository navigation
test: Close the mcp_router backend connections before destroying them - #48970
Conversation
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]>
There was a problem hiding this comment.
🟢 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
| ASSERT_TRUE((*connection)->close()); | ||
| ASSERT_TRUE((*connection)->waitForDisconnect()); |
There was a problem hiding this comment.
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.
| 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(); |
There was a problem hiding this comment.
Good point, thanks - applied as suggested in 23f4a2c: close, waitForDisconnect, waitForNoPost, reset, with RELEASE_ASSERT throughout.
f011fff to
23f4a2c
Compare
…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]>
23f4a2c to
02ab9ef
Compare
|
apologies for force push over existing commit - bad bot |
…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]>
…#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]>
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_.