Skip to content

fix(sync): record answers with the transaction's own context - #222

Merged
bketelsen merged 1 commit into
mainfrom
fix/208-detached-records
Nov 12, 2026
Merged

bketelsen merged 1 commit into
mainfrom
fix/208-detached-records

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #208. Several records of a sync answer run in a transaction started with context.WithoutCancel(ctx), so that the record outlives the session, but the closure passed the session's cancellable ctx to the statements in it. database/sql checks the context of each statement in a transaction ((*Tx).grabConn returns ctx.Err()), so once the session's context ended, the first statement failed, store.DB.Tx rolled back and the record was lost.

  • On the hub (internal/hub/log.go, apply), v.Push had already changed the vault when RecordPush ran. A device that disconnected at that moment left the hub with the change but not its answer, so the device's repeat of the push was not seen as a repeat. The refused missing record in Push had the same fault.
  • On a device (internal/syncer/loop.go), the hub's answer (answer's answerOnly, the conflict branch and the agree of a head the file holds; refuseHere; dropOther) and the clean-up of applying rows (applying, finishOne) could be lost the same way, and the push was sent again.
  • In the vault (internal/vault/apply.go, recordOnly), the record of a head the file already holds failed with context canceled.

Each of these now passes the same non-cancellable context (bg := context.WithoutCancel(ctx)) to the transaction and to every statement in it. Work before the point where the record must be kept still runs on the session's context.

The sync protocol and the stored data are unchanged: no RPC, schema or migration changes. Only records that were meant to be kept are now kept.

Risk tier

No path in .github/risk-paths.json matches (internal/hub/log.go, internal/syncer/loop.go, internal/vault/apply.go and test files).

Risk tier: 2 — a behaviour change in how sync records its answers.

Verification

  • make check green (host)
  • make sync-sim green, for a change to sync, as a shorter seeded run: SIM_SEEDS=3 SIM_STEPS=2000 make sync-sim (stepwise and concurrent; TestISimulation and TestRegressions pass). The default 10 seeds of 10,000 steps were not run.
  • go test -race ./internal/hub/ ./internal/syncer/ ./internal/vault/ -count=1 green
  • Revert test for a fix: in a scratch worktree at the PR head, git checkout HEAD~1 -- internal/hub/log.go internal/syncer/loop.go internal/vault/apply.go, then go test -count=1 ./internal/hub/ ./internal/syncer/ ./internal/vault/ -run '<the 3 tests below>'; all 3 fail:
    • TestAPushAnswerIsRecordedWhenTheCallersContextEnds: the hub lost the answer to the push: read push answer: store: not found
    • TestAnAnswerIsRecordedWhenTheSessionEndsAfterTheHubAnswered: unanswered pushes = [{… Path:n.md …}]; want none: the hub's answer is recorded
    • TestApplyRecordsAHeadTheFileHoldsWithASessionThatEnded: Apply of a head the file holds = file 1: context canceled

New tests:

  • internal/hub/hub_test.go: the hub's renamed crash point cancels the push's context after the vault changed; the answer must be in the hub's store, and a repeat of the push gets it and appends nothing. internal/hub/export_test.go adds Hub.PushAs to call log.push with a context the test holds.
  • internal/syncer/answerctx_test.go: the device's answered crash point ends its loop with the hub's answers in hand; no push may stay unanswered, and the agreed row must hold the answer's head. internal/syncer/export_test.go adds Syncer.CancelLoop and Syncer.WaitLoop.
  • internal/vault/sync_test.go: a vault.Apply of a head the file holds, whose record cancels the caller's context and then runs a statement, must succeed.

A record of a sync answer ran in a transaction started with
context.WithoutCancel(ctx), so that it outlives the session, but its
statements took the session's ctx. database/sql checks the context of
each statement in a transaction, so once the session ended the first
statement failed, the transaction rolled back and the record was lost:
on the hub, a pushed change kept without its answer, so a repeat of the
push was not seen as a repeat; on a device, an answer the hub gave, so
the push went again.

The hub's push answers, the device's answers, agreed rows and applying
rows, and the vault's record of a head the file already holds now pass
the same non-cancellable context to the transaction and to every
statement in it.

Fixes #208
@github-actions github-actions Bot added the level:L3 Skim label Nov 12, 2026
@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Cross-review: Ready, blocking: none, no findings. Reviewer openai/gpt-6.1-sol, coder deepseek/deepseek-flash, base 5fbd9194b6993ed2d7f736119452883742ae4e67, head d1c5fd45c6a01c96cd478bad7fcf721f7d50bd12, all 8 changed files, with the other WithoutCancel sites in internal/hub, internal/syncer and internal/vault checked for the same fault.

@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 12, 2026
@bketelsen
bketelsen merged commit e090378 into main Nov 12, 2026
12 checks passed
@bketelsen
bketelsen deleted the fix/208-detached-records branch November 12, 2026 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sync: records made with context.WithoutCancel still fail when the session's context ends

2 participants