[PR #4622] Fix delegation race condition causing subagent corruption #11041

Open
opened 2026-02-16 18:15:49 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/4622

State: open
Merged: No


Subagents were randomly receiving primary agent configuration (including the task tool, wrong system prompts, and execution tools). This happened intermittently due to a race condition in how agent state is accessed during delegation.

Root Cause

Commit a1214fff2e removed a session-level mutex. After the refactor, parent and child sessions could access the shared Instance.state() concurrently, causing Agent.get() calls to race and subagents to pick up corrupted or partially-loaded agent configuration.

Solution

Restored the SessionLock mechanism with minimal changes:

  • Reinstated session/lock.ts with only the necessary functions (acquire, abort)
  • Wrapped the prompt loop with session-level locking to prevent concurrent agent state access

The fix preserves parallel execution - locks are per-session, so different subagents can still run concurrently.

Testing

I'll be testing this over the next couple of days since this issue occurs multiple times a day. That should give us a good indication of whether it's fully resolved.

Closes #4439

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/4622 **State:** open **Merged:** No --- Subagents were randomly receiving primary agent configuration (including the task tool, wrong system prompts, and execution tools). This happened intermittently due to a race condition in how agent state is accessed during delegation. ## Root Cause Commit a1214fff2eaa removed a session-level mutex. After the refactor, parent and child sessions could access the shared `Instance.state()` concurrently, causing `Agent.get()` calls to race and subagents to pick up corrupted or partially-loaded agent configuration. ## Solution Restored the SessionLock mechanism with minimal changes: - Reinstated `session/lock.ts` with only the necessary functions (`acquire`, `abort`) - Wrapped the prompt loop with session-level locking to prevent concurrent agent state access The fix preserves parallel execution - locks are per-session, so different subagents can still run concurrently. ## Testing I'll be testing this over the next couple of days since this issue occurs multiple times a day. That should give us a good indication of whether it's fully resolved. Closes #4439
yindo added the pull-request label 2026-02-16 18:15:49 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11041