fix(mcp): a close doesn't wait on a client's open event stream

A standalone event stream (the GET an MCP client keeps open) is no answer on
its way out and ends only with its session, which a close ends after it has
waited for the answers: counting it as an answer made every close sit out the
whole grace (10 s) whenever an assistant was connected, and a bridge asking
meanwhile could stall for its own 60 s. Only POSTs are counted, as the
server's own close counts them. A hand demonstration found it; the new test
(an rmcp client with its stream open, the close answered within 5 s) fails
without the fix (12 s).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 09:50:06 -07:00
1 parent 89403a7159
commit 8056e3bbc8
3 files changed
+55 -2

No files matched your search

@@ -504,3 +504,53 @@ fn closing_and_opening_a_book_over_and_over_loses_and_duplicates_nothing() {
"{closed_for:?}"
);
}
/// A client's standalone event stream (the GET an MCP client keeps open
/// for server messages) ends only with its session: a close must not wait
/// on it as it waits on answers on their way out (it would sit out the
/// whole grace for every connected assistant).
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_close_doesnt_wait_for_a_clients_open_event_stream() {
let home = Arc::new(
tokio::task::spawn_blocking(|| Home::new("open-close-stream"))
.await
.unwrap(),
);
let server = {
let home = home.clone();
tokio::task::spawn_blocking(move || {
let server = Server::start(&home);
home.create(A, "USD");
server
})
.await
.unwrap()
};
let client = tokio::task::spawn_blocking({
let home = home.clone();
move || home.token(A)
})
.await
.unwrap();
let c = common::connect_with(&home, A, Some(client)).await.unwrap();
common::call(&c, "books.chart", json!({})).await.unwrap();
// Time for the client to open its stream.
tokio::time::sleep(Duration::from_millis(500)).await;
let started = std::time::Instant::now();
let closed = {
let home = home.clone();
tokio::task::spawn_blocking(move || close_book(&home, A))
.await
.unwrap()
};
assert_eq!(closed, CloseOutcome::Closed);
assert!(
started.elapsed() < Duration::from_secs(5),
"the close waited {:?} on a client's open stream",
started.elapsed()
);
let _ = c.cancel().await;
tokio::task::spawn_blocking(move || assert!(server.close().success()))
.await
.unwrap();
}
+4 -2
View File
@@ -2191,8 +2191,10 @@ async fn mcp(
}
}
// The answer may still be on its way: it counts, for the server's close
// and for this book's, until its body ends.
let answering = _inside.answering();
// and for this book's, until its body ends. A standalone event stream
// (a GET) is no answer, and ends only with its session, after the close
// has waited for the answers.
let answering = (method != axum::http::Method::GET).then(|| _inside.answering());
response.map(|body| {
use http_body_util::BodyExt;
axum::body::Body::new(body.map_frame(move |frame| {
+1
View File
@@ -4311,6 +4311,7 @@ one book never refuses or holds up a call on another.
| `book_open_close::the_open_option_takes_none_and_refuses_a_book_that_doesnt_exist` | `--open none` leaves every book closed; `--open nobody` exits 2 before opening anything |
| `book_open_close::the_sessions_request_names_each_connection_and_never_its_token` | over the real socket: two tokens, two clients, an agent's report, neither token's text nor SHA-256 in the answer, an ended session gone, a close ends the book's sessions |
| `book_open_close::closing_and_opening_a_book_over_and_over_loses_and_duplicates_nothing` | the hammer: 6 agents on one book and 4 on another make journaled drafts and reads while the first is closed and reopened 12 times; both journals are replayed afterwards: every acknowledged draft is in its journal exactly once under the id it was told, no refused call left anything, the closed book's journal and access log never moved between the close answer and the reopening, the other book never refused a call and kept answering while the first was closed, and the books replay with gapless ids |
| `book_open_close::a_close_doesnt_wait_for_a_clients_open_event_stream` | an rmcp client keeps its standalone event stream (a GET) open: closing its book still answers at once, not after the grace for answers on their way out (it took 12 s while the stream was counted as an answer, which stalled the bridge in a hand demonstration) |
| `commands::tests::doctor_says_who_is_connected` | the doctor's lines for sessions |
**For P12 (mutation targets).** `Gate::enter` (count before looking at `shut`),