mirror of
https://github.com/coder/coder.git
synced 2026-06-03 21:18:24 +00:00
703629f5e9
## Problem `TestE2E_WriteFileTriggersGitWatch` and `TestE2E_SubagentAncestorWatch` flake intermittently in `test-go-race-pg` with: ``` agentgit_test.go:1271: timed out waiting for server message ``` ## Root Cause In `handleWatch()`, `GetPaths(chatID)` was called **before** `Subscribe(chatID)` on the PathStore. If `AddPaths()` fired between those two calls: 1. `GetPaths()` returned empty (paths not added yet). 2. `AddPaths()` stored the paths and called `notifySubscribers()` — but the subscription channel didn't exist yet, so the notification was a no-op. 3. `Subscribe()` created the channel, but the notification was already lost. 4. The handler never scanned, and the mock clock never advanced the 30s fallback ticker → timeout. Both failing tests connect the WebSocket with an empty PathStore and immediately call `AddPaths()` from the test goroutine, making them vulnerable to this scheduling interleaving. ## Fix Swap the order: call `Subscribe()` first, then `GetPaths()`. This guarantees: | `AddPaths` fires... | `Subscribe` sees it? | `GetPaths` sees it? | Outcome | |---|---|---|---| | Before `Subscribe` | No | **Yes** | Picked up by `GetPaths` | | Between the two calls | **Yes** (queued) | **Yes** | Redundant but safe (delta dedupes) | | After `GetPaths` | **Yes** | No | Goroutine handles it | No window exists where both miss it. Verified with 10,000 iterations (`-race -count=5000`) — zero failures. Fixes coder/internal#1389