Repository navigation
fix(sync): record answers with the transaction's own context - #222
Merged
Merged
Conversation
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
Contributor
Author
|
Cross-review: Ready, blocking: none, no findings. Reviewer |
bketelsen
approved these changes
Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 cancellablectxto the statements in it.database/sqlchecks the context of each statement in a transaction ((*Tx).grabConnreturnsctx.Err()), so once the session's context ended, the first statement failed,store.DB.Txrolled back and the record was lost.internal/hub/log.go,apply),v.Pushhad already changed the vault whenRecordPushran. 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 refusedmissingrecord inPushhad the same fault.internal/syncer/loop.go), the hub's answer (answer'sanswerOnly, 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.internal/vault/apply.go,recordOnly), the record of a head the file already holds failed withcontext 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.jsonmatches (internal/hub/log.go,internal/syncer/loop.go,internal/vault/apply.goand test files).Risk tier: 2 — a behaviour change in how sync records its answers.
Verification
make checkgreen (host)make sync-simgreen, for a change to sync, as a shorter seeded run:SIM_SEEDS=3 SIM_STEPS=2000 make sync-sim(stepwise and concurrent;TestISimulationandTestRegressionspass). The default 10 seeds of 10,000 steps were not run.go test -race ./internal/hub/ ./internal/syncer/ ./internal/vault/ -count=1greengit checkout HEAD~1 -- internal/hub/log.go internal/syncer/loop.go internal/vault/apply.go, thengo 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 foundTestAnAnswerIsRecordedWhenTheSessionEndsAfterTheHubAnswered:unanswered pushes = [{… Path:n.md …}]; want none: the hub's answer is recordedTestApplyRecordsAHeadTheFileHoldsWithASessionThatEnded:Apply of a head the file holds = file 1: context canceledNew tests:
internal/hub/hub_test.go: the hub'srenamedcrash 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.goaddsHub.PushAsto calllog.pushwith a context the test holds.internal/syncer/answerctx_test.go: the device'sansweredcrash 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.goaddsSyncer.CancelLoopandSyncer.WaitLoop.internal/vault/sync_test.go: avault.Applyof a head the file holds, whose record cancels the caller's context and then runs a statement, must succeed.