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>
|
||||
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 ILoggerFactory? _loggerFactory;
|
||||
|
||||
|
|
@ -159,15 +165,20 @@ public sealed partial class StdioClientTransport : IClientTransport
|
|||
#if NET
|
||||
processStarted = process.Start();
|
||||
#else
|
||||
Encoding originalInputEncoding = Console.InputEncoding;
|
||||
try
|
||||
// IMPORTANT: This must be synchronized to prevent race conditions when multiple
|
||||
// transports are created concurrently.
|
||||
lock (s_consoleEncodingLock)
|
||||
{
|
||||
Console.InputEncoding = StreamClientSessionTransport.NoBomUtf8Encoding;
|
||||
processStarted = process.Start();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.InputEncoding = originalInputEncoding;
|
||||
Encoding originalInputEncoding = Console.InputEncoding;
|
||||
try
|
||||
{
|
||||
Console.InputEncoding = StreamClientSessionTransport.NoBomUtf8Encoding;
|
||||
processStarted = process.Start();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.InputEncoding = originalInputEncoding;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue