Fix race condition where Prompt() returns before SessionUpdate handlers complete (#5)
* test: demonstrate Prompt() returns before SessionUpdates complete Add a failing test that surfaces a race condition where Prompt() can return before all SessionUpdate notification handlers have finished processing. The issue occurs because: - SessionUpdate notifications are handled asynchronously (goroutines) - PromptResponse is handled synchronously - The receive loop spawns notification handlers but doesn't track them When a server sends multiple SessionUpdate notifications followed by a PromptResponse, the client's Prompt() call returns immediately upon receiving the response, even though notification handlers may still be queued or running. The test expects all SessionUpdate handlers to complete before Prompt() returns, which represents the intended semantic contract: a prompt operation includes all its updates. Currently fails with 0/10 handlers completed when Prompt() returns. This will be fixed in a subsequent commit. * fix: ensure Prompt() waits for SessionUpdate handlers to complete Fix race condition where Prompt() could return before all SessionUpdate notification handlers finished processing. The issue occurred because notification/request handlers were spawned asynchronously while responses were processed synchronously. This meant the receive loop would: 1. Read SessionUpdate, then spawn goroutine G1 2. Read SessionUpdate, then spawn goroutine G2 3. Read PromptResponse, then handle synchronously, unblock Prompt() At step 3, goroutines G1/G2 _could_ be queued, running, or complete. Solution: - Add notificationWg to Connection to track in-flight handlers - Wrap notification handlers with WaitGroup Add/Done - Call notificationWg.Wait() in SendRequest/SendRequestNoResult after receiving response but before returning to caller This ensures the semantic contract that a prompt operation includes all its updates: when Prompt() returns, all SessionUpdate notifications sent before the PromptResponse have been fully processed. Fixes the test added in previous commit.
M
Miguel Guedes committed
48085b09b2a7e984e332abcd942a2cd28d7b587b
Parent: cc23bb6
Committed by GitHub <noreply@github.com>
on 12/5/2025, 9:10:39 AM