From 65aaf94d1176c8c39d35f11e386ee104b991a9f3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 1 Apr 2026 18:40:51 +0000 Subject: [PATCH 1/3] Initial plan From 2d4a4d78f2265f5995490c39550b2e9488d4380b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 1 Apr 2026 19:00:37 +0000 Subject: [PATCH 2/3] Fix flaky test: add synchronization point in ReadEventsAsync_InStreamingMode_YieldsNewlyWrittenEvents The test was racy on .NET 10 because all 3 events could be written before the readTask entered its polling loop. Add a TaskCompletionSource that the readTask signals when it receives the first event, ensuring events 2 and 3 are written only after the streaming enumerator is confirmed to be active. Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/f39d7d87-181b-4085-837a-7ec97f9c2b50 Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com> --- .../Server/DistributedCacheEventStreamStoreTests.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs b/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs index 0983e6ad9..6036c0d43 100644 --- a/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs @@ -904,10 +904,16 @@ public async Task ReadEventsAsync_InStreamingMode_YieldsNewlyWrittenEvents() using var cts = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken); cts.CancelAfter(TimeSpan.FromSeconds(10)); var events = new List>(); + + // Use a TCS as a sync point: set when the reader has confirmed receipt of the first event. + // This guarantees the streaming enumerator is definitely active before we write events 2 and 3. + var readerStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var readTask = Task.Run(async () => { await foreach (var evt in reader.ReadEventsAsync(cts.Token)) { + readerStarted.TrySetResult(); events.Add(evt); if (events.Count >= 3) { @@ -916,8 +922,13 @@ public async Task ReadEventsAsync_InStreamingMode_YieldsNewlyWrittenEvents() } }, CancellationToken); - // Write 3 new events - the reader should pick them up since it's in streaming mode + // Write the first event and wait for the reader to confirm it has been received. + // This establishes a synchronization point: once readerStarted is signalled, we know + // the streaming loop is running and will reliably observe any subsequently written events. var event1 = await writer.WriteEventAsync(new SseItem(null), CancellationToken); + await readerStarted.Task.WaitAsync(cts.Token); + + // Write the remaining 2 events now that the reader is confirmed active var event2 = await writer.WriteEventAsync(new SseItem(null), CancellationToken); var event3 = await writer.WriteEventAsync(new SseItem(null), CancellationToken); From f26cbb5baa4df14f5a85426fcbf8e26526f8b0cb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 1 Apr 2026 22:49:17 +0000 Subject: [PATCH 3/3] Fix compilation: use TaskCompletionSource for net472 compatibility Agent-Logs-Url: https://github.com/modelcontextprotocol/csharp-sdk/sessions/16a97d7e-9e05-4de3-8d32-051560bec535 Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com> --- .../Server/DistributedCacheEventStreamStoreTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs b/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs index 6036c0d43..f306064f8 100644 --- a/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/DistributedCacheEventStreamStoreTests.cs @@ -907,13 +907,13 @@ public async Task ReadEventsAsync_InStreamingMode_YieldsNewlyWrittenEvents() // Use a TCS as a sync point: set when the reader has confirmed receipt of the first event. // This guarantees the streaming enumerator is definitely active before we write events 2 and 3. - var readerStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var readerStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var readTask = Task.Run(async () => { await foreach (var evt in reader.ReadEventsAsync(cts.Token)) { - readerStarted.TrySetResult(); + readerStarted.TrySetResult(true); events.Add(evt); if (events.Count >= 3) {