beads/cmd
Julian Knutsen c1b35aed3c
fix(storage): serialize guarded deletes against each other on a Dolt sql-server (#7244)
* fix(dolt): serialize guarded deletes against each other (mc-zndi7.73)

On a Dolt sql-server, two concurrent `bd delete --if-revision R --force`
requests for the same row were both landing: Dolt's commit-time merge
treats two concurrent transactions that each delete the same row as
identical diffs and lands both with no conflict, so every same-token
racer exited 0 instead of exactly one. A delete racing an update/close
already produced a real write conflict (the racing write's row_lock
bump is a genuine edit Dolt's merge must reconcile against the delete),
but deleter.go used withWriteTx (no retry), so the loser surfaced a raw
Dolt Error 1213 instead of being retried and reclassified as a version
mismatch.

Fix: a per-id delete fence (GET_LOCK/RELEASE_LOCK, Dolt/MySQL built-in
session primitives) serializes guarded deletes on a real Dolt
sql-server, so a same-token loser's existence probe now runs only after
the winner's delete has already committed and finds the row gone,
failing through the ordinary NotFoundError path. The fence pins ONE
*sql.Conn for GET_LOCK, the retried write, and RELEASE_LOCK, because
setupTestStore's tests (and any store configured with a pool of one)
run with MaxOpenConns=1, and a two-connection design would deadlock
under it. withRetryTx/commitWriteTx are generalized (withRetryTxOn/
commitWriteTxOn) to run their shared retry-and-commit body against
either the pool or this one pinned connection. deleter.go also now
uses withRetryTx instead of the non-retrying withWriteTx, so a delete
racing an update/close is retried like every other guarded write.
classifyIfRevisionFailure (cmd/bd/if_revision.go) gains a
storage.ErrNotFound case, mapping the row-vanished-from-under-the-guard
outcome to precondition_failed/ExitGuardMismatch on both the proxied
routes (which have no pre-write existence check) and this same-token
delete race.

No schema or migration change: GET_LOCK/RELEASE_LOCK are built-in
Dolt/MySQL session primitives, the same idiom
schema.MigrateUpWithLock already uses for migrations, reimplemented
standalone in internal/storage/dolt/delete_fence.go to avoid borrowing
the schema package's migration-flavored timeout and error sentinels.

Adds real concurrency race tests in the style of #7233's
shared_server_if_revision_concurrency_test.go: same-token deletes,
delete vs close, delete vs update, on both the shared-server and
proxied routes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(uow): serialize guarded deletes against each other on the proxied route (mc-zndi7.73)

internal/storage/dolt's new per-id delete fence only covers the
shared-server/direct-CLI route. The proxied-server (and bd serve)
route goes through internal/storage/uow instead, a completely
separate deleter implementation that shares no code with the dolt
package, so it had the exact same bug: two concurrent guarded deletes
of the same row both landed, because Dolt's commit-time merge treats
two identical "row absent" diffs as non-conflicting.

Fix: a twin fence (internal/storage/uow/delete_fence.go) applied
around the write whenever req.ExpectedVersion is set. Unlike the
dolt-package fence, this one uses two independent connections (one for
GET_LOCK/RELEASE_LOCK, one drawn normally by the retried write) rather
than pinning a single connection: no cmd/bd call site ever constrains
this package's pool to one connection, so a dedicated lock connection
cannot starve the write. The fence unwraps d.provider via the existing
uow.UnwrapProvider helper first, because the real proxied chain always
wraps the bare *doltSQLProvider in a notifyingProvider and then an
externaldeps uowProvider before handing it to NewDeleter.

No code changes were needed in cmd/bd/if_revision.go: a losing
racer's existence probe already returns the same NotFoundError/
storage.ErrNotFound classifyIfRevisionFailure already maps to
precondition_failed for the shared-server race.

Also fixes internal/storage/dolt/version_scope_completeness_test.go's
TestTxMintingWrappersScopeVersionedHistory, broken by the prior
mc-zndi7.73 commit's withRetryTxOn/commitWriteTxOn split: the BeginTx
call (and the versioned-history scope call beside it) moved from
commitWriteTx's own body down into commitWriteTxOn, so the guard's
hardcoded txMintingWrappers list needed to name the new function that
actually mints the transaction.

No schema or migration change: GET_LOCK/RELEASE_LOCK are the same
built-in Dolt/MySQL session primitives the dolt-package fence uses.

Agent-Signature: claude-code-claude-sonnet-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* chore(bazel): sync BUILD files for mc-zndi7.73's new delete-fence sources

make bazel-sync, picking up internal/storage/dolt/delete_fence.go and
the two cmd/bd race-test files added by the prior commit (missed
there) plus this commit's new internal/storage/uow/delete_fence.go.

Agent-Signature: claude-code-claude-sonnet-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 09:39:44 -07:00
..
bd fix(storage): serialize guarded deletes against each other on a Dolt sql-server (#7244) 2026-10-05 09:39:44 -07:00