Relationships
#4992 context.runModel() runs the target method without the target's per-behavior lock
Opened by skunk-ape · 11/22/2026· Shipped 11/22/2026
What happens
A method run takes a per-behavior lock, so two runs on the same behavior wait for each other. A nested run started with context.runModel() does not take that lock on its target. It can run at the same time as a swamp behavior run (or another nested run) on the same behavior.
When both runs read the behavior's data and then write it, one update is lost. The concurrency docs say runs on the same behavior serialize. Nested runs break that.
Steps to reproduce
swamp inita fresh repo with the default filesystem datastore.- Add an extension behavior
@repro/counterwith a methodbump. It reads itsstateresource, sleeps 4 seconds, then writes{ count: previous + 1 }. - Add an extension behavior
@repro/callerwith a methodgothat callscontext.runModel({ definition: "ctr", method: "bump" }). swamp behavior create @repro/counter ctrandswamp behavior create @repro/caller clr.- Control: start
swamp behavior run ctr bumptwice, one second apart. The second waits for the first. Count goes 1, 2, 3. - Race: start
swamp behavior run clr go, then one second laterswamp behavior run ctr bump.
Log from step 6 (count was 3 before):
16:23:01.665Z pid=2246243 nested START read count=3
16:23:02.660Z pid=2248304 hand START read count=3
16:23:05.671Z pid=2246243 nested END wrote count=4
16:23:06.666Z pid=2248304 hand END wrote count=4Both commands exit 0. Data versions 4 and 5 both hold count: 4. The same happens with the order reversed: the hand run holds the lock and the nested run goes straight in.
Expected vs actual
Expected: the nested run waits for the target's lock, as a second swamp behavior run does. The two bumps run one after the other and the count ends at 5.
Actual: both run at once on the same behavior. The count ends at 4. One update is lost and nothing reports it.
What the lock protects
Version numbers stay unique without the lock, because each version directory is created with an atomic mkdir (src/infrastructure/persistence/unified_data_repository.ts:3352). So no version is overwritten. What breaks is everything a method assumes about owning the behavior while it runs:
- Read, then write: two runs read the same latest version, and the later write silently drops the earlier one (shown above).
- "Latest" is whichever run finished last, not whichever started last.
- Lifecycle checks such as "create only if no resource exists" both pass, so external side effects (create a cloud resource, send a message) can happen twice.
- In a team or custom datastore the target's data is pulled and pushed under the target's lock. A nested run writes the target with no lock or lease on it.
Cause
On origin/main (3f2b72312):
- A top-level run takes the lock in
src/cli/commands/model_method_run.ts:646-683(methodRunLockMode, thenacquireModelLocksfor the one behavior being run).acquireModelLocksis atsrc/cli/repo_context.ts:3678. Workflow steps lock their own step's behavior the same way. context.runModelis wired insrc/domain/models/in_process_executor.ts:315toModelInvocationService.invoke(src/domain/models/model_invocation_service.ts:243).invokeresolves the target, checks depth, cycles and authorization, builds a child context (line 374) and callsexecutionService.executeWorkflow(line 412). Nothing on that path takes a lock on the target.design/enablers/datastores.md:10662saysrunModel()"carries the outer run's locks". Those are the caller's locks, not the target's. No code comment or design note says leaving the target unlocked is deliberate..claude/skills/swamp/references/behavior/references/concurrency.md:5tells users runs on the same behavior serialize.
Suggested fix
In ModelInvocationService.invoke, take the target's per-behavior lock (acquireModelLocks or the same lock hook workflow steps use) before executeWorkflow, run the child under runUnderModelLocks so its own children see the lock, and release it in a finally.
Caveats:
- Self-call: cycle detection is by (type, method), so a method may
runModelanother method on its own behavior. The caller already holds that lock. The lock must be reentrant for the current run scope (the lock-holder marker already knows which locks the scope holds), or this deadlocks. - Lock order: A's method calling B while B's method calls A can wait on each other. The lock timeout and the existing
lock_wait_cyclecheck should turn that into an error, not a hang. A test should pin it. - The nested run's writes to the target should be pushed under the target's lock in team and custom datastores.
- Nested runs are not available on workers, so remote dispatch needs no change.
Version
Reproduced with swamp 20261120.161258.0-sha.7b758e05, filesystem datastore, Linux. The code is the same on origin/main 3f2b72312.
Shipped
Click a lifecycle step above to view its details.
Sign in to post a ripple.