fix(sync-service): cold nested subquery shape times out subscribing to a dependency materializer (#4715)#4729
Open
robacourt wants to merge 3 commits into
Open
fix(sync-service): cold nested subquery shape times out subscribing to a dependency materializer (#4715)#4729robacourt wants to merge 3 commits into
robacourt wants to merge 3 commits into
Conversation
A cold nested subquery shape can return an initial HTTP 500. The outer consumer subscribes to each dependency materializer with a hardcoded 5s `GenServer.call` timeout (`Consumer.all_materializers_alive?/1` -> `Materializer.subscribe/1`). The dependency materializer stays blocked in `handle_continue(:start_materializer)` on `Consumer.await_snapshot_start(..., :infinity)` until its own snapshot starts, so if that snapshot takes longer than 5s the subscribe times out, Electric removes the outer shape, and the client's first request fails. This adds a failing (`:slow`) router test that stalls only the dependency shape's snapshot past the 5s window and asserts the cold nested shape still serves 200. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…aterializers (#4715) `Materializer.subscribe/1` was the only materializer call using the default 5s `GenServer.call` timeout; `wait_until_ready/1` and `new_changes/3` both use `:infinity`. A materializer cannot answer any call until `handle_continue(:start_materializer)` returns, and that blocks on `await_snapshot_start(:infinity)` until its own snapshot starts. When a cold dependency snapshot takes longer than 5s, the outer consumer's `all_materializers_alive?/1` subscribe timed out, the outer shape was removed, and the client's first request returned 500. Subscribe now waits with `:infinity`. Liveness is already covered by the caller monitoring the materializer before subscribing, so a dead materializer surfaces as a call exit rather than being masked by a short timeout. The dependency graph is a DAG, so there is no deadlock risk. Flips the `#4715` reproduction test to green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4729 +/- ##
==========================================
- Coverage 60.04% 60.01% -0.03%
==========================================
Files 397 397
Lines 43766 43766
Branches 12586 12588 +2
==========================================
- Hits 26281 26268 -13
- Misses 17403 17416 +13
Partials 82 82
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A cold nested subquery shape could return an initial HTTP 500 while its dependency snapshots were still in progress. A retry succeeded once the dependency snapshots finished.
Fixes #4715
Problem
When an outer shape's consumer initializes,
Consumer.all_materializers_alive?/1subscribes to each dependency materializer viaMaterializer.subscribe/1. That was the only materializer call using the default 5sGenServer.calltimeout —wait_until_ready/1andnew_changes/3both use:infinity.A materializer can't answer any call until
handle_continue(:start_materializer)returns, and that blocks onConsumer.await_snapshot_start(..., :infinity)until its own snapshot starts. For a cold nested topology, the intermediate materializer itself waits on a deeper dependency, so its snapshot can start later than 5s after the outer consumer subscribes. The subscribe then times out, Electric removes the outer shape, and the client's first request 500s:This matches the logs reported in #4715.
Solution
Materializer.subscribe/*now waits with:infinity, consistent with the other materializer calls. This matches the intended semantics from the issue — "a single cold nested shape waits for dependency materializers and completes without an externally visible 500."Why this is safe:
all_materializers_alive?/1doesProcess.monitor(pid, ...)before subscribing, so a dead materializer surfaces as a call exit rather than being masked by a short timeout.ShapeLogCollector'sEventRouter(viaSetupEffects→ShapeLogCollector.add_shape) only afterall_materializers_alive?/1returns, insideinitialize_event_handler. While the consumer is blocked in the subscribe it is not yet a routing target, so the collector never broadcasts a transaction to it. The overall wait is bounded by the request-levelawait_snapshot_start(outer, 45000)budget.Reproduction / Test Plan
Added a
:slowrouter test (test/electric/plug/router_test.exs) that deterministically reproduces the bug by stalling only the dependency shape's snapshot past the 5s subscribe window (via an injected:create_snapshot_fn), then asserting the cold nested shape still serves 200.mainwith the{:timeout, {GenServer, :call, [_, :subscribe, 5000]}}signaturematerializer_test.exs,consumer_test.exs, and the/v1/shapes - subqueriesrouter tests passRun the reproduction:
mix test test/electric/plug/router_test.exs --include slow --only line:3505Generated with Claude Code