Repository navigation
[Fix] Guard ReOpenIncident with a version predicate - #134
Conversation
ReOpenIncident cleared end_date without checking the caller's version, so a stale copy could reopen an incident modified after it was read. The update now matches on the loaded version, bumps it, and reports ErrVersionConflict when the row no longer matches. The patch handler maps that conflict to 409 instead of 500.
There was a problem hiding this comment.
Summary
Good fix for #118. ReOpenIncident now runs a single atomic UPDATE ... WHERE id = ? AND version = ? that clears end_date and bumps the version in the same statement, returning db.ErrVersionConflict on zero rows — this correctly closes the stale-write race that UpdateOneID + unconditional ClearEndDate() had. The handler maps the conflict to the existing 409 path, and the new Postgres-backed test in tests/reopen_version_test.go covers both the conflict and the success case. All CI checks (build, go-test, go-test-acc, lint, CodeQL) are green.
Verdict: approve — no blocking issues, but two behavior concerns worth addressing.
Findings
Warning — nil-version rejection surfaces as a 500
internal/db/db.go (ReOpenIncident, new guard): when inc.Version == nil the function returns an ad-hoc errors.New("version is required for incident modification"). This is not db.ErrVersionConflict (and no other sentinel), so in internal/api/v2/v2.go:1216-1226 it falls into the logger.Error(... database error) branch and is reported as a 500. A missing client-supplied version is an input error, not a server error.
Note this is reachable in practice: PatchIncidentHandler only requires a version for maintenance (v2.go:1149) and only copies incData.Version onto storedIncident when non-nil (v2.go:1175-1176). For a non-maintenance incident, the version used is whatever getEventFromContext populated; if that path can ever yield a db.Incident with a nil Version, a previously working reopen now returns 500 instead of the old behavior (unconditional success).
Fix: define a sentinel (e.g. db.ErrVersionMissing) in internal/db/errors.go, return it from the guard, and map it to a 400 in reopenIncident before the conflict branch. Also double-check that getEventFromContext → GetIncident → incidentFromEnt always populates Version.
Suggestion — missing incident maps to 409 instead of 404
Previously ReOpenIncident swallowed ent.IsNotFound and returned nil (silent success — the bug this PR fixes). Now a zero-row update for a deleted incident returns ErrVersionConflict → 409 Conflict. The PR description calls this intentional, but semantically a 404 is the correct status for a deleted resource, and 409 will make it harder for clients to distinguish "someone else changed it" from "it no longer exists". Consider a Count/GetIncident check up front (or an ent.IsNotFound-style probe) to return ErrDBIncidentDSNotExist → 404, reserving ErrVersionConflict for genuine version mismatches. If 409 is accepted as-is, document it in the API contract since it's a user-visible behavior change.
Suggestion — test nit
tests/reopen_version_test.go:42: the stale-version check works, but note version - 1 will be 0 when the incident starts at version 0/1 depending on the schema default; using an obviously different stale value (or asserting version first) makes the test robust to the starting version. Non-blocking.
Things checked
- Atomicity: single
UPDATEwith version predicate + version bump — no read-then-write window. Correct. Update().Save(ctx)returns(int, error)in Ent;affected == 0handling is correct.- Conflict mapping in
reopenIncidentreuses the sameapiErrors.RaiseConflictErr/409 pattern already established atv2.go:1096-1103. - Version increment is appropriate: clearing
end_dateis a real content change, so bumping the optimistic-lock counter is correct. - CI: build, unit tests, Postgres acceptance tests (including the new test), lint, and CodeQL all pass.
ReOpenIncidentissuedClearEndDate()with no version predicate, so a stale copy could reopen an incident another writer had already changed.The update now runs
Where(incident.IDEQ(id), incident.VersionEQ(*inc.Version)), setsversion+1in the same statement and returnsErrVersionConflictwhen zero rows match.reopenIncidentmaps that conflict to the existing 409 response instead of 500. A call for a missing incident now also reportsErrVersionConflictinstead of succeeding silently, consistent with the zero-row rule.Verification:
go build ./...,go vet ./...,go test ./internal/....TestReOpenIncidentVersionPredicateintests/reopen_version_test.gochecks that a stale version leavesend_dateand the version untouched and that a current version clearsend_dateand bumps the version; thetests/suite runs in CI against Postgres.Closes #118