codefloe/forgejo-codefloe
Watch
4
12
Fork
You've already forked forgejo-codefloe
0

fix(db): pin the request to the primary when a transaction starts #111

Merged
pat-s merged 1 commit from t3code/fix-pr-comment-500 into v16.0/forgejo-codefloe 2026-09-07 07:19:51 +00:00
Owner

Summary

  • Pin the request to the primary when a transaction starts, so a write inside db.WithTx gives its request read-your-writes consistency.
  • Restore the fix from #107: creating a code comment on a HOST_REPLICA setup intermittently answered 404 Not found. because the conversation refetch read a lagging replica.
  • Cover transactional writes in models/db/engine_codefloe_test.go, the case the existing tests missed.

Motivation

#108 pins a request to the primary after its first write, via an xorm hook that flags the request context on any non-SELECT statement.
The hook receives the context the statement runs on, and db.WithTx / db.TxContext create their session with x.NewSession(), which xorm gives engine.defaultContext (xorm/session.go:102-108) rather than the request context.
Every statement in a transaction therefore runs on the process-wide context, PinPrimary is a no-op, and the request stays unpinned.

Nearly every write in Forgejo runs inside db.WithTx, so the pin only ever fired for autocommit writes.
The existing TestPrimaryPinningReadsYourWrites passes because it writes through db.GetEngine(request), which is the autocommit path, and only enters WithTx once the request is already pinned.

Because #108 removed the explicit db.WithMaster from #107 on the assumption the pin covered it, the review conversation refetch went back to reading the replica:

  • pull_service.CreateCodeComment writes the comment inside a transaction, leaving the request unpinned.
  • renderConversation refetches the conversation through the unpinned request context, so the read goes to the replica.
  • Replication has not caught up, FetchCodeConversation returns no rows, fetchCodeConversation maps the empty result to ErrCommentNotExist and ctx.NotFoundOrServerError answers 404 with a plain-text Not found..
  • The diff view swallows the status and shows a generic Network error toast, which is why this reads as a server error rather than a 404.

Observed on codefloe.com, where HOST_PRIMARY and HOST_REPLICA are separate Patroni backends: most comments win the race, some do not.

Design

  • models/db/context.go: PinPrimary(parentCtx) in TxContext and txWithNoCheck, right before the session is created. Both are the only paths that open a new transaction; the reuse paths run under a parent that has already pinned.
  • Pinning at BEGIN rather than at the first write inside the transaction keeps this independent of xorm's session context handling, and matches what #108 already accepted for BEGIN and PRAGMA.

Known limits

  • A read-only transaction now pins too, so its remaining reads skip the replica. Over-eager on purpose, in the same way as #108.
  • Reads that run before the first write of a request are unaffected and still use the replica. GetCurrentReview in services/pull/review.go is one of them: adding comments in quick succession can miss the pending review created by the previous request and open a second one. That is a separate bug and is not addressed here.
  • A session held in a variable across a write and a later read still routes that read to the replica, unchanged from #108.

Testing

  • TestPrimaryPinningTransactionalWrite: a stale replica and a write performed only inside db.WithTx; the request must be pinned afterwards and must read its own transactional write, while an unpinned context keeps reading the replica. Fails on v16.0/forgejo-codefloe with expected: "tx write", actual: "stale replica".
  • newLaggingReplicaSetup extracts the primary/replica harness that TestPrimaryPinningReadsYourWrites had inline, so both tests share it.
  • go vet ./models/db/ and go test -tags 'sqlite sqlite_unlock_notify' ./models/db/ pass.
  • gofmt -l models/db/ is empty.
## Summary - Pin the request to the primary when a transaction starts, so a write inside `db.WithTx` gives its request read-your-writes consistency. - Restore the fix from #107: creating a code comment on a `HOST_REPLICA` setup intermittently answered `404 Not found.` because the conversation refetch read a lagging replica. - Cover transactional writes in `models/db/engine_codefloe_test.go`, the case the existing tests missed. ## Motivation #108 pins a request to the primary after its first write, via an xorm hook that flags the request context on any non-SELECT statement. The hook receives the context the statement runs on, and `db.WithTx` / `db.TxContext` create their session with `x.NewSession()`, which xorm gives `engine.defaultContext` (`xorm/session.go:102-108`) rather than the request context. Every statement in a transaction therefore runs on the process-wide context, `PinPrimary` is a no-op, and the request stays unpinned. Nearly every write in Forgejo runs inside `db.WithTx`, so the pin only ever fired for autocommit writes. The existing `TestPrimaryPinningReadsYourWrites` passes because it writes through `db.GetEngine(request)`, which is the autocommit path, and only enters `WithTx` once the request is already pinned. Because #108 removed the explicit `db.WithMaster` from #107 on the assumption the pin covered it, the review conversation refetch went back to reading the replica: - `pull_service.CreateCodeComment` writes the comment inside a transaction, leaving the request unpinned. - `renderConversation` refetches the conversation through the unpinned request context, so the read goes to the replica. - Replication has not caught up, `FetchCodeConversation` returns no rows, `fetchCodeConversation` maps the empty result to `ErrCommentNotExist` and `ctx.NotFoundOrServerError` answers `404` with a plain-text `Not found.`. - The diff view swallows the status and shows a generic `Network error` toast, which is why this reads as a server error rather than a 404. Observed on codefloe.com, where `HOST_PRIMARY` and `HOST_REPLICA` are separate Patroni backends: most comments win the race, some do not. ## Design - `models/db/context.go`: `PinPrimary(parentCtx)` in `TxContext` and `txWithNoCheck`, right before the session is created. Both are the only paths that open a new transaction; the reuse paths run under a parent that has already pinned. - Pinning at `BEGIN` rather than at the first write inside the transaction keeps this independent of xorm's session context handling, and matches what #108 already accepted for `BEGIN` and `PRAGMA`. ## Known limits - A read-only transaction now pins too, so its remaining reads skip the replica. Over-eager on purpose, in the same way as #108. - Reads that run before the first write of a request are unaffected and still use the replica. `GetCurrentReview` in `services/pull/review.go` is one of them: adding comments in quick succession can miss the pending review created by the previous request and open a second one. That is a separate bug and is not addressed here. - A session held in a variable across a write and a later read still routes that read to the replica, unchanged from #108. ## Testing - `TestPrimaryPinningTransactionalWrite`: a stale replica and a write performed only inside `db.WithTx`; the request must be pinned afterwards and must read its own transactional write, while an unpinned context keeps reading the replica. Fails on `v16.0/forgejo-codefloe` with `expected: "tx write", actual: "stale replica"`. - `newLaggingReplicaSetup` extracts the primary/replica harness that `TestPrimaryPinningReadsYourWrites` had inline, so both tests share it. - `go vet ./models/db/` and `go test -tags 'sqlite sqlite_unlock_notify' ./models/db/` pass. - `gofmt -l models/db/` is empty.
pat-s force-pushed t3code/fix-pr-comment-500 from 8ba42e8f79
Some checks failed
testing / backend-checks (pull_request) Has been cancelled
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / frontend-checks (pull_request) Has been cancelled
to 65ed56c81c
All checks were successful
testing / frontend-checks (pull_request) Successful in 1m9s
testing / backend-checks (pull_request) Successful in 6m30s
testing / test-unit (pull_request) Successful in 10m39s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m47s
testing / test-remote-cacher (garnet) (pull_request) Successful in 1m13s
testing / test-remote-cacher (valkey) (pull_request) Successful in 1m13s
testing / test-mysql (pull_request) Successful in 35m43s
testing / test-pgsql (pull_request) Successful in 54m0s
testing / test-e2e (pull_request) Successful in 32m15s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Has been skipped
testing / test-sqlite (pull_request) Successful in 50m58s
2026-09-07 06:11:28 +00:00
Compare
pat-s merged commit 96dc142b72 into v16.0/forgejo-codefloe 2026-09-07 07:19:51 +00:00
pat-s deleted branch t3code/fix-pr-comment-500 2026-09-07 07:19:51 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
codefloe/forgejo-codefloe!111
No description provided.