A timed-out append is re-sent while the first still lands: duplicate events #2

Closed
opened 2026-12-02 07:49:45 +00:00 by neptunus · 4 comments
Member

Purpose (bug): a gateway append that times out is never re-sent, so a slow store cannot write the same event twice.

What happens

route_call/3 calls the gateway worker with CALL_TIMEOUT (5000 ms) through reckon_gater_retry:with_retry/2. A timeout comes back as {error, timeout}, and is_retriable_error/1 treats it as transient (it is not on the non-retriable list), so the call is re-sent to a worker.

But a gen_server:call timeout does not cancel the request. reckon-db's gateway worker (reckon_db_gateway_worker:stream_append_op/3) appends inline, and the first append_events still runs and lands. The re-sent call then appends again: with ANY_VERSION, or through the auto-versioned path that re-reads the version, the same events are stored twice.

When it happens:

  • one append that takes more than 5 s (a slow disk under contention: reckon-db#7 measured about 21 ms per commit on a forge runner, and a contended append retries up to 60 times);
  • any call queued behind a slow one in the same worker (gateway_pool_size defaults to 1). Its wait in the mailbox counts against its 5 s too, and it still runs after gater has given up and re-sent it.

Done when

  • An append (append_events, append_if_no_tag_matches) whose call times out is not re-sent blindly. It answers an error the caller can act on, or the retry is made safe (for example, idempotent by event id).
  • A test with a worker that answers after the timeout shows the events stored once.

Refs reckon-db-org/reckon-db#7 (where reckon-db bounds one append's own time below this timeout).

Purpose (bug): a gateway append that times out is never re-sent, so a slow store cannot write the same event twice. ## What happens `route_call/3` calls the gateway worker with `CALL_TIMEOUT` (5000 ms) through `reckon_gater_retry:with_retry/2`. A timeout comes back as `{error, timeout}`, and `is_retriable_error/1` treats it as transient (it is not on the non-retriable list), so the call is re-sent to a worker. But a `gen_server:call` timeout does not cancel the request. reckon-db's gateway worker (`reckon_db_gateway_worker:stream_append_op/3`) appends inline, and the first `append_events` still runs and lands. The re-sent call then appends again: with `ANY_VERSION`, or through the auto-versioned path that re-reads the version, the same events are stored twice. When it happens: - one append that takes more than 5 s (a slow disk under contention: reckon-db#7 measured about 21 ms per commit on a forge runner, and a contended append retries up to 60 times); - any call queued behind a slow one in the same worker (gateway_pool_size defaults to 1). Its wait in the mailbox counts against its 5 s too, and it still runs after gater has given up and re-sent it. ## Done when - An append (`append_events`, `append_if_no_tag_matches`) whose call times out is not re-sent blindly. It answers an error the caller can act on, or the retry is made safe (for example, idempotent by event id). - A test with a worker that answers after the timeout shows the events stored once. Refs reckon-db-org/reckon-db#7 (where reckon-db bounds one append's own time below this timeout).
Member

Threat model (Saturnus, before code)

Change. route_call/3 keeps today's retry for every request except the two appends, append_events (3 and 4) and append_if_no_tag_matches.

  • An append is re-sent only on an error that proves the request never reached a worker:
    • no_workers: nothing was selected.
    • worker_down: the noproc exit, where the worker was already dead when the call was made.
  • Every other error goes back to the caller unchanged, at once: a call timeout, node_down, a worker that died during the call, or an error the worker answered.
  • Mechanism: reckon_gater_retry:with_retry/4 takes the predicate that decides "retriable". with_retry/2,3 keep is_retriable_error/1, so no other call changes and the retry_config record is untouched.

Assets.

  • Every event a caller sent is stored at most once.
  • A caller that got {ok, _} has its events stored.
  • A caller gets an answer in bounded time.

T1, a duplicate (this bug).

  • How it happens: a gen_server:call timeout does not cancel the request. The worker appends inline, so the first append lands after gater has given up. A re-send then stores the events again (ANY_VERSION, or the auto-versioned path).
  • The same holds for an append queued behind a slow one (pool size 1), for nodedown after delivery, and for a worker exit mid-append.
  • Mitigation: none of these errors re-sends. The retry list for appends is an allow-list of proven non-delivery, so a new error shape defaults to not re-sent.
  • Proof:
    • A unit test with a worker that answers after the timeout: the events are stored once, and the caller gets {error, timeout}.
    • A unit test that worker_down is still retried.
    • Mutants: put timeout on the allow-list; make the append path use is_retriable_error/1; drop the append match from the request classifier. Each must turn a test red.

T2, a lost event.

  • No acknowledged event can be lost. A caller now gets {error, timeout} where gater used to retry, and that error means "outcome unknown". The append may still land, so the caller must not treat it as "not written".
  • The caller decides: read the stream, or re-send with an exact expected version, which a landed first append turns into wrong_expected_version instead of a duplicate.
  • This goes in the moduledoc of append_events.
  • Residual risk: a caller that blindly re-sends on {error, timeout} with ANY_VERSION reintroduces the duplicate one level up. reckon_evoq and the mcl callers are checked in the HANDOFF.

T3, more errors surface.

  • Before, a transient error was hidden for up to 10 tries. Now an append's node_down or Ra error reaches the caller.
  • That is the honest answer for a non-idempotent write, not a regression of stored data.
  • reckon-db#7's T2 mitigation, a per-append budget well below 5 s, makes the timeout case rare.

T4, an unbounded wait. Unchanged. The append path makes at most one call per proven non-delivery, and keeps the same attempt cap and backoff.

T5, an outside attacker. Nothing new is reachable. A hostile caller can already slow a store. The change only stops gater from multiplying that caller's writes.

Out of scope (named, not changed). The other write calls through route_call (delete_stream, ack_event, save_subscription, remove_subscription, delete_snapshot_sync, scavenge*) are idempotent or answer an error on a second run. If one proves not to be, it gets its own issue.

## Threat model (Saturnus, before code) **Change.** `route_call/3` keeps today's retry for every request except the two appends, `append_events` (3 and 4) and `append_if_no_tag_matches`. - An append is re-sent only on an error that proves the request never reached a worker: - `no_workers`: nothing was selected. - `worker_down`: the `noproc` exit, where the worker was already dead when the call was made. - Every other error goes back to the caller unchanged, at once: a call `timeout`, `node_down`, a worker that died during the call, or an error the worker answered. - Mechanism: `reckon_gater_retry:with_retry/4` takes the predicate that decides "retriable". `with_retry/2,3` keep `is_retriable_error/1`, so no other call changes and the `retry_config` record is untouched. **Assets.** - Every event a caller sent is stored at most once. - A caller that got `{ok, _}` has its events stored. - A caller gets an answer in bounded time. **T1, a duplicate (this bug).** - How it happens: a `gen_server:call` timeout does not cancel the request. The worker appends inline, so the first append lands after gater has given up. A re-send then stores the events again (ANY_VERSION, or the auto-versioned path). - The same holds for an append queued behind a slow one (pool size 1), for `nodedown` after delivery, and for a worker exit mid-append. - Mitigation: none of these errors re-sends. The retry list for appends is an allow-list of proven non-delivery, so a new error shape defaults to not re-sent. - Proof: - A unit test with a worker that answers after the timeout: the events are stored once, and the caller gets `{error, timeout}`. - A unit test that `worker_down` is still retried. - Mutants: put `timeout` on the allow-list; make the append path use `is_retriable_error/1`; drop the append match from the request classifier. Each must turn a test red. **T2, a lost event.** - No acknowledged event can be lost. A caller now gets `{error, timeout}` where gater used to retry, and that error means "outcome unknown". The append may still land, so the caller must not treat it as "not written". - The caller decides: read the stream, or re-send with an exact expected version, which a landed first append turns into `wrong_expected_version` instead of a duplicate. - This goes in the moduledoc of `append_events`. - Residual risk: a caller that blindly re-sends on `{error, timeout}` with ANY_VERSION reintroduces the duplicate one level up. reckon_evoq and the mcl callers are checked in the HANDOFF. **T3, more errors surface.** - Before, a transient error was hidden for up to 10 tries. Now an append's `node_down` or Ra error reaches the caller. - That is the honest answer for a non-idempotent write, not a regression of stored data. - reckon-db#7's T2 mitigation, a per-append budget well below 5 s, makes the timeout case rare. **T4, an unbounded wait.** Unchanged. The append path makes at most one call per proven non-delivery, and keeps the same attempt cap and backoff. **T5, an outside attacker.** Nothing new is reachable. A hostile caller can already slow a store. The change only stops gater from multiplying that caller's writes. **Out of scope (named, not changed).** The other write calls through `route_call` (`delete_stream`, `ack_event`, `save_subscription`, `remove_subscription`, `delete_snapshot_sync`, `scavenge*`) are idempotent or answer an error on a second run. If one proves not to be, it gets its own issue.
Author
Member

Correction to the last line of the body: reckon-db does NOT bound one append's own time below this timeout. A per-append time budget was tried in reckon-db#7 and dropped by Supervisor's ruling, because a budget safely inside 5 s turned the slow tail into refusals. An append in reckon-db retries up to 60 times, and on slow storage it can take well over 5 s. So this fix cannot rely on reckon-db answering in time.

To reproduce slow appends locally: run reckon-db's reckon_db_concurrent_append_SUITE in the pinned CI image with podman run --cpus 0.1. There, 20 writers x 100 appends take about 37 s and 36 of 2000 are refused as version_conflict (reckon-db#7).

Correction to the last line of the body: reckon-db does NOT bound one append's own time below this timeout. A per-append time budget was tried in reckon-db#7 and dropped by Supervisor's ruling, because a budget safely inside 5 s turned the slow tail into refusals. An append in reckon-db retries up to 60 times, and on slow storage it can take well over 5 s. So this fix cannot rely on reckon-db answering in time. To reproduce slow appends locally: run reckon-db's `reckon_db_concurrent_append_SUITE` in the pinned CI image with `podman run --cpus 0.1`. There, 20 writers x 100 appends take about 37 s and 36 of 2000 are refused as `version_conflict` (reckon-db#7).
Member

Correction to my threat model above (from Neptunus): reckon-db does NOT bound one append below gater's 5 s. The per-append budget of reckon-db#7 c7921 was dropped by a Supervisor ruling.

  • T3 changes. A slow append over 5 s will happen, and with this fix it answers {error, timeout} (outcome unknown) instead of being re-sent.
  • T1 and T2 do not change. The fix never relied on reckon-db answering in time.
  • Both duplicate paths are covered:
    • one append over 5 s;
    • a call queued behind a slow one in the same worker, including the auto-versioned {append_events, _, StreamId, Events} path that re-reads the version.
  • In both cases the timeout is not re-sent.
**Correction to my threat model above (from Neptunus):** reckon-db does NOT bound one append below gater's 5 s. The per-append budget of reckon-db#7 c7921 was dropped by a Supervisor ruling. - **T3 changes.** A slow append over 5 s will happen, and with this fix it answers `{error, timeout}` (outcome unknown) instead of being re-sent. - **T1 and T2 do not change.** The fix never relied on reckon-db answering in time. - **Both duplicate paths are covered:** - one append over 5 s; - a call queued behind a slow one in the same worker, including the auto-versioned `{append_events, _, StreamId, Events}` path that re-reads the version. - In both cases the timeout is not re-sent.
Member

Done, on main 582d7530 (PR 3; Mercurius PASS r=1 on 4180c45a, main merged once). Runs: 6834-6837 on the tip, main 6838/6839 green.

  • Done when 1: append_events and append_if_no_tag_matches are re-sent only on no_workers / worker_down (reckon_gater_retry:is_undelivered_error/1); a timeout goes back to the caller as {error, timeout}.
  • Done when 2: test/unit/reckon_gater_append_once_tests.erl has a worker that answers after the timeout: the events are stored once.
Done, on main 582d7530 (PR 3; Mercurius PASS r=1 on 4180c45a, main merged once). Runs: 6834-6837 on the tip, main 6838/6839 green. - Done when 1: append_events and append_if_no_tag_matches are re-sent only on no_workers / worker_down (reckon_gater_retry:is_undelivered_error/1); a timeout goes back to the caller as {error, timeout}. - Done when 2: test/unit/reckon_gater_append_once_tests.erl has a worker that answers after the timeout: the events are stored once.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
2 participants
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
reckon-db-org/reckon-gater#2
No description provided.