fix: cleanup possibel race conditions in mcpproxy - #70
Conversation
Signed-off-by: Calum Murray <cmurray@redhat.com>
WalkthroughThe changes modify startup behavior and error propagation in the MCP proxy server management. The Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
pkg/mcpproxy/server_manager.go (1)
109-115: Consider usinggctxinstead ofctxfor WaitReady.The servers run with
gctx(errgroup context), butWaitReadyusesctx. If a server fails inRunafter closing itsreadychannel (e.g.,Servefails immediately),gctxis cancelled butctxis not. The remainingWaitReadycalls would still succeed withnil, andStartwould returnnildespite a failed server.Since
close(s.ready)happens very early inRun(beforeServe), this window is small but exists. Usinggctxwould provide tighter error propagation:// Wait for all servers to be ready before returning for name, srv := range m.servers { - if err := srv.WaitReady(ctx); err != nil { + if err := srv.WaitReady(gctx); err != nil { cancel() // Cancel all servers if one fails to become ready return fmt.Errorf("server %s failed to become ready: %w", name, err) } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
pkg/mcpproxy/server.go(3 hunks)pkg/mcpproxy/server_manager.go(2 hunks)
🔇 Additional comments (4)
pkg/mcpproxy/server.go (3)
36-37: Good addition for startup error tracking.The
startErrfield paired withreadychannel provides proper synchronization—the write tostartErrhappens-beforeclose(ready), andWaitReadyreads it only after the channel closes, establishing a valid happens-before relationship.
181-189: Correct error signaling on startup failure.The flow is correct: on listen failure,
startErris set beforeclose(ready), ensuring callers ofWaitReadysee the error. The success path at line 189 and failure path at line 182 are mutually exclusive, avoiding double-close.
260-266: LGTM!Returning
s.startErrafter the ready channel closes correctly propagates startup failures to callers.pkg/mcpproxy/server_manager.go (1)
22-23: Doc comment accurately reflects the new blocking behavior.The updated comment clearly states the contract:
Startblocks until all servers are ready or an error occurs.
This resolves some possible race conditions in the mcpproxy server/server manager
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.