Skip to content

Commit be5c09a

Browse files
committed
Fix slow shutdown when a Streamable HTTP client is connected
1 parent eddaea4 commit be5c09a

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

src/ModelContextProtocol.AspNetCore/StreamableHttpHandler.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Http.Features;
33
using Microsoft.AspNetCore.WebUtilities;
4+
using Microsoft.Extensions.Hosting;
45
using Microsoft.Extensions.Logging;
56
using Microsoft.Extensions.Options;
67
using Microsoft.Net.Http.Headers;
@@ -17,8 +18,9 @@ internal sealed class StreamableHttpHandler(
1718
IOptionsFactory<McpServerOptions> mcpServerOptionsFactory,
1819
IOptions<HttpServerTransportOptions> httpServerTransportOptions,
1920
StatefulSessionManager sessionManager,
20-
ILoggerFactory loggerFactory,
21-
IServiceProvider applicationServices)
21+
IHostApplicationLifetime hostApplicationLifetime,
22+
IServiceProvider applicationServices,
23+
ILoggerFactory loggerFactory)
2224
{
2325
private const string McpSessionIdHeaderName = "Mcp-Session-Id";
2426

@@ -94,14 +96,28 @@ await WriteJsonRpcErrorAsync(context,
9496
return;
9597
}
9698

97-
await using var _ = await session.AcquireReferenceAsync(context.RequestAborted);
98-
InitializeSseResponse(context);
99+
// Link the GET request to both RequestAborted and ApplicationStopping.
100+
// The GET request should complete immediately during graceful shutdown without waiting for
101+
// in-flight POST requests to complete. This prevents slow shutdown when clients are still connected.
102+
using var sseCts = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted, hostApplicationLifetime.ApplicationStopping);
103+
var cancellationToken = sseCts.Token;
99104

100-
// We should flush headers to indicate a 200 success quickly, because the initialization response
101-
// will be sent in response to a different POST request. It might be a while before we send a message
102-
// over this response body.
103-
await context.Response.Body.FlushAsync(context.RequestAborted);
104-
await session.Transport.HandleGetRequestAsync(context.Response.Body, context.RequestAborted);
105+
try
106+
{
107+
await using var _ = await session.AcquireReferenceAsync(cancellationToken);
108+
InitializeSseResponse(context);
109+
110+
// We should flush headers to indicate a 200 success quickly, because the initialization response
111+
// will be sent in response to a different POST request. It might be a while before we send a message
112+
// over this response body.
113+
await context.Response.Body.FlushAsync(sseCts.Token);
114+
await session.Transport.HandleGetRequestAsync(context.Response.Body, sseCts.Token);
115+
}
116+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
117+
{
118+
// RequestAborted always triggers when the client disconnects before a complete response body is written,
119+
// but this is how SSE connections are typically closed.
120+
}
105121
}
106122

107123
public async Task HandleDeleteRequestAsync(HttpContext context)

tests/ModelContextProtocol.AspNetCore.Tests/MapMcpTests.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using ModelContextProtocol.Server;
99
using ModelContextProtocol.Tests.Utils;
1010
using System.ComponentModel;
11+
using System.Diagnostics;
1112
using System.Net;
1213
using System.Security.Claims;
1314

@@ -114,10 +115,9 @@ public async Task Messages_FromNewUser_AreRejected()
114115
}
115116

116117
[Fact]
117-
public async Task ClaimsPrincipal_CanBeInjectedIntoToolMethod()
118+
public async Task ClaimsPrincipal_CanBeInjected_IntoToolMethod()
118119
{
119120
Builder.Services.AddMcpServer().WithHttpTransport(ConfigureStateless).WithTools<ClaimsPrincipalTools>();
120-
Builder.Services.AddHttpContextAccessor();
121121

122122
await using var app = Builder.Build();
123123

@@ -211,6 +211,35 @@ public async Task Sampling_DoesNotCloseStream_Prematurely()
211211
m.Message.Contains("request '2' for method 'sampling/createMessage'"));
212212
}
213213

214+
[Fact]
215+
public async Task Server_ShutsDownQuickly_WhenClientIsConnected()
216+
{
217+
Builder.Services.AddMcpServer().WithHttpTransport().WithTools<ClaimsPrincipalTools>();
218+
219+
await using var app = Builder.Build();
220+
app.MapMcp();
221+
222+
await app.StartAsync(TestContext.Current.CancellationToken);
223+
224+
// Connect a client which will open a long-running GET request (SSE or Streamable HTTP)
225+
await using var mcpClient = await ConnectAsync();
226+
227+
// Verify the client is connected
228+
var tools = await mcpClient.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
229+
Assert.NotEmpty(tools);
230+
231+
// Now measure how long it takes to stop the server
232+
var stopwatch = Stopwatch.StartNew();
233+
await app.StopAsync(TestContext.Current.CancellationToken);
234+
stopwatch.Stop();
235+
236+
// The server should shut down quickly (within a few seconds). We use 5 seconds as a generous threshold.
237+
// This is much less than the default HostOptions.ShutdownTimeout of 30 seconds.
238+
Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(5),
239+
$"Server took {stopwatch.Elapsed.TotalSeconds:F2} seconds to shut down with a connected client. " +
240+
"This suggests the GET request is not respecting ApplicationStopping token.");
241+
}
242+
214243
private ClaimsPrincipal CreateUser(string name)
215244
=> new ClaimsPrincipal(new ClaimsIdentity(
216245
[new Claim("name", name), new Claim(ClaimTypes.NameIdentifier, name)],

tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryTransport.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public sealed class KestrelInMemoryTransport : IConnectionListenerFactory
1313
public KestrelInMemoryConnection CreateConnection(EndPoint endpoint)
1414
{
1515
var connection = new KestrelInMemoryConnection();
16-
GetAcceptQueue(endpoint).Writer.TryWrite(connection);
16+
if (!GetAcceptQueue(endpoint).Writer.TryWrite(connection))
17+
{
18+
throw new IOException("The KestrelInMemoryTransport has been shut down.");
19+
};
20+
1721
return connection;
1822
}
1923

@@ -37,7 +41,7 @@ private sealed class KestrelInMemoryListener(EndPoint endpoint, Channel<Connecti
3741

3842
public async ValueTask<ConnectionContext?> AcceptAsync(CancellationToken cancellationToken = default)
3943
{
40-
if (await acceptQueue.Reader.WaitToReadAsync(cancellationToken))
44+
while (await acceptQueue.Reader.WaitToReadAsync(cancellationToken))
4145
{
4246
while (acceptQueue.Reader.TryRead(out var item))
4347
{

tests/ModelContextProtocol.TestSseServer/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ private static void HandleStatelessMcp(IApplicationBuilder app)
370370
var serviceCollection = new ServiceCollection();
371371
serviceCollection.AddLogging();
372372
serviceCollection.AddSingleton(app.ApplicationServices.GetRequiredService<ILoggerFactory>());
373+
serviceCollection.AddSingleton(app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>());
373374
serviceCollection.AddSingleton(app.ApplicationServices.GetRequiredService<DiagnosticListener>());
374375
serviceCollection.AddRoutingCore();
375376

0 commit comments

Comments
 (0)