Skip to content

Commit 9bbb82e

Browse files
Adebayo120Adam JamiuCopilotchr-hertel
authored
[Client] Fix client transport leak on reconnect (#614)
* Fix client transport leak on reconnect Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Move reconnect note to Disconnecting and add changelog entry --------- Co-authored-by: Adam Jamiu <adamjamiu@Adams-MacBook-Air.local> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
1 parent ed8098c commit 9bbb82e

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ All notable changes to `mcp/sdk` will be documented in this file.
1111
* Fix tools with an array shape parameter whose keys are nullable or unions (e.g. `array{name: string, nickname: string|null}`) rejecting valid arguments with `-32602`: a `null` inside the shape no longer turns the parameter into `array|null`, and such keys get their own types instead of `object`, including lists and nested shapes (`string[]|null`, `array{x: int}|null`).
1212
* Fix `array<string|null>` and other generics with a union element getting `items` of type `object`. A `null` inside a generic no longer makes the parameter nullable, and neither does a DocBlock class whose name contains `null`.
1313

14+
### Client
15+
16+
* Fix `Client::connect()` leaving the previous transport open when called again: it closes the current transport first, so no stdio process or HTTP session is left behind.
17+
1418
0.9.0
1519
-----
1620

‎docs/client/connecting.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ try {
248248
}
249249
```
250250

251+
Calling `connect()` again on the same client closes its current transport before opening the new one.
252+
251253
## Server Information
252254

253255
After successful connection, retrieve server metadata:

‎src/Client.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public static function builder(): Builder
9292
*/
9393
public function connect(TransportInterface $transport): void
9494
{
95+
$this->disconnect();
96+
9597
$this->transport = $transport;
9698
$this->protocol->connect($transport, $this->config);
9799

‎tests/Unit/Client/ClientTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@
2121

2222
final class ClientTest extends TestCase
2323
{
24+
public function testReconnectClosesPreviousTransport(): void
25+
{
26+
$client = Client::builder()->build();
27+
$firstTransport = $this->createMock(TransportInterface::class);
28+
$firstTransport->expects($this->once())->method('close');
29+
$firstTransport->method('setState')->willReturnCallback(static function (ClientStateInterface $state): void {
30+
$state->setInitialized(true);
31+
});
32+
33+
$secondTransport = $this->createMock(TransportInterface::class);
34+
$secondTransport->expects($this->once())->method('close');
35+
$secondTransport->method('setState')->willReturnCallback(static function (ClientStateInterface $state): void {
36+
$state->setInitialized(true);
37+
});
38+
39+
$client->connect($firstTransport);
40+
$client->connect($secondTransport);
41+
42+
$this->assertTrue($client->isConnected());
43+
44+
$client->disconnect();
45+
}
46+
2447
public function testSendRootsListChangedSendsNotificationWhenCapabilityDeclared(): void
2548
{
2649
$sent = [];

0 commit comments

Comments
 (0)