Skip to content

Clear the stale STARTUP message from the exception-replay queue - #771

Open
mrayva wants to merge 1 commit into
pgEdge:mainfrom
mrayva:pr/stale-startup-replay
Open

mrayva wants to merge 1 commit into
pgEdge:mainfrom
mrayva:pr/stale-startup-replay

Conversation

@mrayva

@mrayva mrayva commented Dec 1, 2026

Copy link
Copy Markdown

Fixes the "unknown action of type" apply crash reported in #610.

apply_replay_head/apply_replay_tail (the exception-replay queue) were only cleared in handle_commit(). The one-time 'S' STARTUP message sent at connection setup goes through the same queue_append path as transactional messages, so it became the head of the list before any transaction had begun.

If the first transaction a freshly connected worker processes hits an exception that needs replay, apply_replay_queue_start_replay() points apply_replay_next at apply_replay_head. That was still the stale STARTUP entry rather than the transaction's own BEGIN, so replay dispatched on a message type it does not handle and the worker crashed.

The change clears the stale STARTUP entry from the replay queue so replay starts at the transaction's BEGIN.

Testing

  • Compiles cleanly against PG 18.6.
  • Run on a 3-node PG 18 mesh with the conflict and maturity batteries (crash recovery, WAL retention, partition, bulk transaction, no-PK table): no regressions.
  • No new TAP test yet, because this needs a fault on the first transaction after a reconnect. I can add one if you tell me which helper you prefer to use for that.
  • Not run on PG 15–17.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Meb83hmmxffhEKhUbTkXgi

apply_replay_head/tail (the exception-replay queue) was only ever
cleared in handle_commit(). The one-time 'S' STARTUP message sent at
connection setup flows through the exact same queue_append path as
transactional messages, so it became the head of that list the instant
it was received -- before any transaction had even begun.

If the very first transaction a freshly-connected worker processes hits
an exception requiring replay, apply_replay_queue_start_replay() points
apply_replay_next at apply_replay_head, which was still that stale
STARTUP entry, not the transaction's own BEGIN.

Replaying it hit a second, compounding issue: the generic per-message
pre-processing decides whether to consume an 8-byte remote_insert_lsn
field based on the currently negotiated protocol version. The STARTUP
message's wire format deliberately never includes that field, since
version negotiation hasn't happened yet when it's first sent/parsed.
By the time the stale entry gets replayed, the version has already been
negotiated to 5 (STARTUP's own job), so the check wrongly fires and
consumes 8 bytes that aren't there, shifting every subsequent read 8
bytes into the STARTUP message's own parameter text. "max_proto_version"
is always the first parameter sent, so the misaligned read lands on the
same character every time, producing the always-identical "unknown
action of type o" crash.

Root-caused with a temporary shared-memory ring buffer that captured
raw wire bytes at both ingestion (PQgetCopyData) and replay
(apply_replay_queue_next_entry) time, tagged with a correlating
sequence id -- the entry returned at crash time was byte-for-byte
identical to this worker's own STARTUP message from earlier in the same
connection, not corrupted data.

Fix: a deferred-reset flag, set at the end of handle_startup() once
negotiation completes, consumed in the main loop right after
replication_handler() returns and any need_free bookkeeping for that
message is done -- never inside handle_startup() itself, since
resetting there would free the entry's buffer while the caller's
msg/entry still alias it for the rest of that loop iteration.

Verified against a 100%-reliable repro (isolate a subscriber, delete a
row locally, run a large single-statement UPDATE on the provider that
touches that row, re-enable the subscription so the first transaction
it replays after reconnecting requires exception replay): crashed 3/3
times before this fix, passed 2/2 times after, including one run
against a fully clean production build with no debug instrumentation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019z2vqY5jJj13hY87Wq9JrE
(cherry picked from commit 26077da)
(cherry picked from commit edef36d)
@mrayva
mrayva requested a review from a team as a code owner December 1, 2026 15:11
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The startup handler now marks the replay queue for reset after processing a startup message. The main loop performs the reset after message handling and entry-free bookkeeping.

Changes: Startup replay queue reset

Layer / File(s) Summary
Defer replay queue reset until message processing completes
src/spock_apply.c
handle_startup sets a pending-reset flag after processing the startup message. The main loop resets the replay queue and clears the flag after message handling and entry-free bookkeeping.
Merge Risk: Minimal · up to ea686

This change clears the stale startup entry from the replay queue so a freshly connected worker no longer crashes with "unknown action of type" when exception replay is needed on its first transaction. No remaining merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: clearing the stale STARTUP message from the exception-replay queue. The raw summary and objectives describe the same fix, a reset of the replay queue after startup han…
Description check Passed The description states the crash it fixes (issue #610), explains why the one-time STARTUP message enters the exception-replay queue, and describes the fix of clearing that stale entry. It also lists t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: clearing the stale STARTUP message from the exception-replay queue. The raw summary and objectives describe the same fix, a reset of the replay queue after startup handling. The title is specific and does not use vague terms.

Full details: Description check

Explanation

The description states the crash it fixes (issue #610), explains why the one-time STARTUP message enters the exception-replay queue, and describes the fix of clearing that stale entry. It also lists the testing performed. It is related to the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f11300f2-976d-4a04-8f78-25e08adc682d
Commits

Reviewing files that changed from the base of the PR and between 1cf6c0b and ea686dc.

Files selected for processing: 1

  • src/spock_apply.c

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@mason-sharp
mason-sharp requested a review from rasifr December 1, 2026 18:42

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant