Fix race condition in StdioClientTransport on .NET Framework (#996)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This commit is contained in:
parent
a9427f9f12
commit
e6db415940
1 changed files with 19 additions and 8 deletions
|
|
@ -25,6 +25,12 @@ namespace ModelContextProtocol.Client;
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public sealed partial class StdioClientTransport : IClientTransport
|
public sealed partial class StdioClientTransport : IClientTransport
|
||||||
{
|
{
|
||||||
|
#if !NET
|
||||||
|
// On .NET Framework, we need to synchronize access to Console.InputEncoding
|
||||||
|
// to prevent race conditions when multiple transports are created concurrently.
|
||||||
|
private static readonly object s_consoleEncodingLock = new();
|
||||||
|
#endif
|
||||||
|
|
||||||
private readonly StdioClientTransportOptions _options;
|
private readonly StdioClientTransportOptions _options;
|
||||||
private readonly ILoggerFactory? _loggerFactory;
|
private readonly ILoggerFactory? _loggerFactory;
|
||||||
|
|
||||||
|
|
@ -159,6 +165,10 @@ public sealed partial class StdioClientTransport : IClientTransport
|
||||||
#if NET
|
#if NET
|
||||||
processStarted = process.Start();
|
processStarted = process.Start();
|
||||||
#else
|
#else
|
||||||
|
// IMPORTANT: This must be synchronized to prevent race conditions when multiple
|
||||||
|
// transports are created concurrently.
|
||||||
|
lock (s_consoleEncodingLock)
|
||||||
|
{
|
||||||
Encoding originalInputEncoding = Console.InputEncoding;
|
Encoding originalInputEncoding = Console.InputEncoding;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -169,6 +179,7 @@ public sealed partial class StdioClientTransport : IClientTransport
|
||||||
{
|
{
|
||||||
Console.InputEncoding = originalInputEncoding;
|
Console.InputEncoding = originalInputEncoding;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!processStarted)
|
if (!processStarted)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue