Skip to content

Commit 0410986

Browse files
committed
[Server] Overwrite the answer of an abandoned request instead of forgetting it
1 parent 939057b commit 0410986

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎src/Server/Protocol.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,10 @@ private function dropAbandonedRequests(SessionInterface $session): void
822822
'session_id' => $session->getId()->toRfc4122(),
823823
]);
824824

825-
// Per entry, so saving does not write back what other streams changed in the others.
825+
// Per entry, so saving does not write back what other streams changed in the others. The answer
826+
// may have been stored after this session was loaded, so it is overwritten, not forgotten.
826827
$session->forget(self::SESSION_PENDING_REQUESTS.".{$id}");
827-
$session->forget(self::SESSION_RESPONSES.".{$id}");
828+
$session->set(self::SESSION_RESPONSES.".{$id}", null);
828829
}
829830
}
830831

‎tests/Unit/Server/ProtocolTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,26 @@ public function testAbandonedRequestIsDroppedWhenAnotherRequestIsSent(): void
22462246
$this->assertNull($session->get('_mcp.responses.00000000-0000-4000-8000-000000001000'));
22472247
}
22482248

2249+
#[TestDox('An answer to an abandoned client request is dropped even if it arrived after the session was loaded')]
2250+
public function testAnswerStoredAfterLoadingIsDroppedWithAbandonedRequest(): void
2251+
{
2252+
[$protocol, $sessionManager, $session] = $this->createProtocolWithPendingRequests([
2253+
'00000000-0000-4000-8000-000000001000' => ['request_id' => '00000000-0000-4000-8000-000000001000', 'timeout' => 5, 'timestamp' => time() - 100],
2254+
]);
2255+
2256+
$snapshot = $sessionManager->createWithId($session->getId());
2257+
$snapshot->get('_mcp.pending_requests');
2258+
2259+
$other = $sessionManager->createWithId($session->getId());
2260+
$other->set('_mcp.responses.00000000-0000-4000-8000-000000001000', ['jsonrpc' => '2.0', 'id' => '00000000-0000-4000-8000-000000001000', 'result' => []]);
2261+
$other->save();
2262+
2263+
$protocol->sendRequest(new PingRequest(), 5, $snapshot);
2264+
$snapshot->save();
2265+
2266+
$this->assertNull($sessionManager->createWithId($session->getId())->get('_mcp.responses.00000000-0000-4000-8000-000000001000'));
2267+
}
2268+
22492269
#[TestDox('A timed out client request within the grace period is left to its own stream')]
22502270
public function testTimedOutRequestWithinGracePeriodIsKeptWhenAnotherRequestIsSent(): void
22512271
{

0 commit comments

Comments
 (0)