Skip to content

fix: race condition losing opencode worker port in database#384

Merged
jamiepine merged 1 commit into
mainfrom
fix/opencode-port-race
Mar 10, 2026
Merged

fix: race condition losing opencode worker port in database#384
jamiepine merged 1 commit into
mainfrom
fix/opencode-port-race

Conversation

@jamiepine

@jamiepine jamiepine commented Mar 10, 2026

Copy link
Copy Markdown
Member

Summary

  • Fixes the recurring bug where opencode_port is never written to worker_runs, causing the OpenCode embed to not render in the UI.

Root Cause

log_worker_started (INSERT) and log_opencode_metadata (UPDATE) are both fire-and-forget tokio::spawn calls. In channel_dispatch.rs, spawn_worker_task() starts worker.run() before the WorkerStarted event is sent. The worker creates its OpenCode session quickly and emits OpenCodeSessionCreated, triggering the UPDATE — but the INSERT from WorkerStarted hasn't committed yet. The UPDATE matches 0 rows, silently "succeeds" (only Err was checked, not rows_affected), and the port is permanently lost.

Fix

log_opencode_metadata now checks rows_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.rs to add exponential backoff retry logic in log_opencode_metadata(). The function now checks rows_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.

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.
@coderabbitai

coderabbitai Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 573512d7-066d-4722-824f-225ec58a6cdb

📥 Commits

Reviewing files that changed from the base of the PR and between 861b68f and 0a3e8c8.

📒 Files selected for processing (1)
  • src/conversation/history.rs

Walkthrough

Adds retry mechanism with exponential back-off to log_opencode_metadata function in src/conversation/history.rs to handle race conditions during database updates. Implements up to 5 retry attempts when UPDATE affects zero rows, includes debug and warning logging for retry attempts and failures, and preserves original fire-and-forget behavior.

Changes

Cohort / File(s) Summary
Retry Logic for Database Race Condition
src/conversation/history.rs
Introduces retry loop (max 5 attempts) with exponential back-off to log_opencode_metadata. Handles case where worker_runs row may not exist yet due to uncommitted insert. Adds conditional logging (debug on retries, warning on persistent failure). Retains fire-and-forget semantics on update errors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: race condition losing opencode worker port in database' accurately summarizes the main fix: addressing a race condition that causes the OpenCode worker port to not be saved.
Description check ✅ Passed The description is well-related to the changeset, explaining the root cause of the bug, the fix implemented with retry logic, and how it resolves the issue of losing the opencode_port.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/opencode-port-race

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment on lines +540 to +544
tracing::warn!(
worker_id = %id,
"worker_runs row never appeared after {MAX_RETRIES} retries, \
opencode metadata (port={port}) lost"
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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"
);

@jamiepine jamiepine merged commit 57dc122 into main Mar 10, 2026
4 checks passed
rktmeister pushed a commit to rktmeister/spacebot that referenced this pull request Mar 11, 2026
…port-race

fix: race condition losing opencode worker port in database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant