fix: race condition losing opencode worker port in database#384
Conversation
The opencode_port UPDATE could execute before the worker_runs INSERT committed, since both are fire-and-forget tokio::spawn calls. The UPDATE silently matched 0 rows and the port was permanently lost, preventing the UI from showing the OpenCode embed. Retry with exponential back-off (50ms base, 5 retries) when rows_affected is zero, giving the INSERT time to land.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughAdds retry mechanism with exponential back-off to Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
| tracing::warn!( | ||
| worker_id = %id, | ||
| "worker_runs row never appeared after {MAX_RETRIES} retries, \ | ||
| opencode metadata (port={port}) lost" | ||
| ); |
There was a problem hiding this comment.
Minor logging nit: {MAX_RETRIES} / {port} won’t be interpolated in tracing::warn! (it’s a literal string). Using structured fields (or format args) makes the log actually show the values.
| tracing::warn!( | |
| worker_id = %id, | |
| "worker_runs row never appeared after {MAX_RETRIES} retries, \ | |
| opencode metadata (port={port}) lost" | |
| ); | |
| tracing::warn!( | |
| worker_id = %id, | |
| port, | |
| max_retries = MAX_RETRIES, | |
| "worker_runs row never appeared after retries; opencode metadata lost" | |
| ); |
…port-race fix: race condition losing opencode worker port in database
Summary
opencode_portis never written toworker_runs, causing the OpenCode embed to not render in the UI.Root Cause
log_worker_started(INSERT) andlog_opencode_metadata(UPDATE) are both fire-and-forgettokio::spawncalls. Inchannel_dispatch.rs,spawn_worker_task()startsworker.run()before theWorkerStartedevent is sent. The worker creates its OpenCode session quickly and emitsOpenCodeSessionCreated, triggering the UPDATE — but the INSERT fromWorkerStartedhasn't committed yet. The UPDATE matches 0 rows, silently "succeeds" (onlyErrwas checked, notrows_affected), and the port is permanently lost.Fix
log_opencode_metadatanow checksrows_affected()and retries with exponential back-off (50ms, 100ms, 200ms, 400ms, 800ms) when the row doesn't exist yet. The first retry at 50ms will almost always succeed. Logs a warning if the row never appears after 5 retries.Note
Modified
src/conversation/history.rsto add exponential backoff retry logic inlog_opencode_metadata(). The function now checksrows_affected()on UPDATE and retries with increasing delays (50ms base) up to 5 times when the target row hasn't been inserted yet. Prevents silent data loss of OpenCode port metadata. Includes enhanced debug logging for visibility during retries.Written by Tembo for commit 0a3e8c8. This will update automatically on new commits.