Repository navigation
[Fix] Re-read incident components before moving a component - #130
Conversation
6604831 to
0cf973b
Compare
There was a problem hiding this comment.
Summary
The change re-reads an incident's component list via the new db.GetIncidentComponents right before deciding whether to close the old incident in moveComponentIfFound (internal/api/v2/v2.go), instead of trusting the snapshot taken before this request created its incident. Extraction into an incidentStore interface makes the logic testable; stub-based tests cover the stale-snapshot, unknown-component and reload-error paths. go-test, build and golangci-lint CI runs pass.
Findings
Warning — residual race window (v2.go, moveComponentIfFound)
The reload is done before the move, not atomically with it. A concurrent request can still change the component list between GetIncidentComponents and MoveComponentFromOldToAnotherIncident, so the close decision can be stale in exactly the ways this fix targets (closing an incident that now has components, or leaving one open with none). The window is much smaller than before, but the invariant is only guaranteed if the count and the move happen in the same DB transaction. Suggested fix: move the re-check inside MoveComponentFromOldToAnotherIncident (recount within that transaction and decide close there), or at minimum document this as an accepted residual race.
Warning — the "found" match still uses the stale snapshot (v2.go, moveComponentIfFound)
for _, incComp := range inc.Components still decides whether the component is in the old incident using the pre-request snapshot. If the component was concurrently removed from inc, the code will still call MoveComponentFromOldToAnotherIncident with a component that no longer belongs to that incident; I could not verify how the DB layer behaves in that case (whether it no-ops, errors, or detaches from the wrong source). Conversely, a component concurrently added to inc won't be detected. Consider verifying against the reloaded components slice instead of inc.Components, and confirm the move call is a safe no-op when the component isn't linked to incOld.
Warning — new failure mode on the reload (v2.go, moveComponentIfFound)
Previously this code path made no DB call before the move; now a reload error — including ErrDBIncidentDSNotExist if the old incident was deleted concurrently — propagates as false, err to the caller (v2.go:855). I couldn't read the caller's error handling, so I can't confirm whether this transient error now fails the whole request with 500 even though the new incident was already created. Please confirm the caller's behavior; if a transient reload failure shouldn't fail the request, consider logging and falling back to closeInc := len(inc.Components) == 1 (or never closing) on reload error.
Suggestion — GetIncidentComponents returns more than needed (internal/db/db.go:658)
The query selects only component.FieldID, component.FieldName but maps through componentFromEnt, producing Component values with zero timestamps and no attributes; the only consumer is len(components). Either use a Count() query or document that this returns a lightweight component list, so a future caller doesn't assume full components.
Note — CI "check" run failed
The check workflow run is failure while go-test, build, golangci-lint version and ent-codegen all pass. I can't inspect the failure from here; if it's caused by this branch (e.g. a flaky job in this PR's scope), it should be fixed before merging. Otherwise please re-run to confirm it's unrelated.
Verdict
No definitive bug found in the diff and unit tests pass; the issues above are robustness gaps worth addressing. Approve with the caveats that the "check" CI failure be explained and the caller behavior for the new reload error be confirmed.
There was a problem hiding this comment.
Summary
The change fixes a stale-snapshot bug in tryMoveComponentIfFound (#123): the "close the old incident if it's the only component" decision now uses the component list re-read via db.GetIncidentComponents immediately before MoveComponentFromOldToAnotherIncident, instead of the inc.Components snapshot taken when the request began. The refactor into a testable incidentStore interface + moveComponentIfFound is clean, the new db.GetIncidentComponents maps the ent edge rows correctly, and the stub-based tests cover the stale-snapshot cases, the unknown-component path, and reload errors. CI build / vet / go-test / CodeQL all pass.
No correctness blocker in the changed code itself. One important gap and a minor note below.
Warning — the same bug is not fixed in the sibling path
moveComponentFromToSystemIncidents still makes the close decision from the stale snapshot:
internal/api/v2/v2.go:694—closeOld := len(oldInc.Components) == 1internal/api/v2/v2.go:696—MoveComponentFromOldToAnotherIncident(..., closeOld)internal/api/v2/v2.go:709—if len(oldInc.Components) == 1 {(impact-increase branch)
This is the exact stale-snapshot pattern the PR is fixing, applied to a different move path. A concurrent change can still close an old system-incident move with a peer component, or leave one open with none. If #123 is meant to cover "close the old incident from a snapshot fetched before the request created its incident," this path is in scope and remains broken.
Suggested fix: apply the same store.GetIncidentComponents re-read before computing closeOld here (and guard the len(oldInc.Components) == 1 check at line 709 against the re-read list), or factor a shared helper so both move paths reload consistently. At minimum, confirm this path is intentionally out of scope and note it in the PR / a follow-up.
Suggestion — TOCTOU window is reduced, not eliminated
Even after the re-read there is still a race between GetIncidentComponents (v2.go:915) and MoveComponentFromOldToAnotherIncident (v2.go:921). A concurrent add/remove can land in between. This is inherent to the read-then-write approach and out of scope for this fix, but worth noting: a fully safe fix would evaluate "is this the last component" inside the move's transaction (e.g. MoveComponentFromOldToAnotherIncident deciding closeOld from a row-locked count), which would also make the sibling path correct in one place.
Minor
- The loop in
moveComponentIfFoundre-reads components on every matching iteration. If a malformedinc.Componentssnapshot ever listed the same component ID twice, the move would be attempted twice. In practice the snapshot is unique, so this is a non-issue in the real flow; no change required, just noting the loop is no longer strictly idempotent.
tryMoveComponentIfFounddecided whether to close the old incident frominc.Components, a snapshot fetched before the request created its incident; a concurrent change could close an incident that still had components or leave one open with none. The component list is now re-read throughdb.GetIncidentComponentsright before the comparison. Signature and returned behaviour are otherwise unchanged.Verification:
go build ./...,go vet ./...,go test ./internal/.... Stub-based tests cover the stale-snapshot cases, the unknown-component path and reload errors.Closes #123