-
Notifications
You must be signed in to change notification settings - Fork 357
Fix StdioServerTransport.DisposeAsync hang #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
halter73
merged 7 commits into
modelcontextprotocol:main
from
halter73:cancel-stdio-read
Apr 8, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e6a591b
Fix StdioServerTransport.DisposeAsync hang
halter73 98b3879
Connect client before sending SIGINT in test
halter73 f675320
Use LoggedTest
halter73 d3a54d7
Address PR feedback
halter73 8ec4a36
Merge remote-tracking branch 'origin/main' into cancel-stdio-read
halter73 b9e2ef6
React to IClientTransport and McpClientFactoryChanges
halter73 a866612
More KestrelInMemoryConnection cleanup
halter73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
tests/ModelContextProtocol.Tests/StdioServerIntegrationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using ModelContextProtocol.Client; | ||
using ModelContextProtocol.Protocol.Transport; | ||
using ModelContextProtocol.Tests.Utils; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace ModelContextProtocol.Tests; | ||
|
||
public class StdioServerIntegrationTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper) | ||
{ | ||
public static bool CanSendSigInt { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX); | ||
private const int SIGINT = 2; | ||
|
||
[Fact(Skip = "Platform not supported by this test.", SkipUnless = nameof(CanSendSigInt))] | ||
public async Task SigInt_DisposesTestServerWithHosting_Gracefully() | ||
{ | ||
using var process = new Process | ||
{ | ||
StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = "dotnet", | ||
Arguments = "TestServerWithHosting.dll", | ||
RedirectStandardInput = true, | ||
RedirectStandardOutput = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
} | ||
}; | ||
|
||
process.Start(); | ||
|
||
await using var streamServerTransport = new StreamServerTransport( | ||
process.StandardOutput.BaseStream, | ||
process.StandardInput.BaseStream, | ||
serverName: "TestServerWithHosting"); | ||
|
||
await using var client = await McpClientFactory.CreateAsync( | ||
new TestClientTransport(streamServerTransport), | ||
loggerFactory: LoggerFactory, | ||
cancellationToken: TestContext.Current.CancellationToken); | ||
|
||
// I considered writing a similar test for Windows using Ctrl-C, then saw that dotnet watch doesn't even send a Ctrl-C | ||
// signal because it's such a pain without support for CREATE_NEW_PROCESS_GROUP in System.Diagnostics.Process. | ||
// https://github.com/dotnet/sdk/blob/43b1c12e3362098a23ca1018503eb56516840b6a/src/BuiltInTools/dotnet-watch/Internal/ProcessRunner.cs#L277-L303 | ||
// https://github.com/dotnet/runtime/issues/109432, https://github.com/dotnet/runtime/issues/44944 | ||
Assert.Equal(0, kill(process.Id, SIGINT)); | ||
|
||
using var shutdownCts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken); | ||
shutdownCts.CancelAfter(TimeSpan.FromSeconds(10)); | ||
await process.WaitForExitAsync(shutdownCts.Token); | ||
|
||
Assert.True(process.HasExited); | ||
Assert.Equal(0, process.ExitCode); | ||
} | ||
|
||
[DllImport("libc", SetLastError = true)] | ||
private static extern int kill(int pid, int sig); | ||
|
||
private sealed class TestClientTransport(ITransport sessionTransport) : IClientTransport | ||
{ | ||
public string Name => nameof(TestClientTransport); | ||
|
||
public Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default) | ||
=> Task.FromResult(sessionTransport); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.