Repository navigation
fix(log/test): add readiness barrier to prevent test startup race - #372
Merged
0xB10C merged 1 commit intoMar 10, 2026
Merged
Conversation
Add two synchronization barriers to the log extractor integration test harness to prevent flaky timeouts (fixes peer-observer#366): 1. Subscribe before spawn: the NATS subscription is created before the extractor starts publishing, so startup events are captured. Without this, tests with no-op test_setup() starve because the only qualifying events were published before the subscriber existed. 2. Marker handshake: a unique line is written to debug.log and awaited via NATS, proving the full tail -> pipe -> extractor -> NATS pipeline is live before test_setup() fires. Events consumed before the marker are buffered and replayed through check_event. If the NATS stream closes before the marker arrives, the test fails fast with a diagnostic message. No production code changes — all fixes are in integration.rs.
Collaborator
|
Thanks, I think this looks good. tiny nit: probably worthwile to split two fixes into two commits (I think you had this before and then squashed). |
Collaborator
|
Thanks. Let's see if there are any other intermittent failures |
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
The log extractor integration tests have been intermittently timing out in CI (see #366). The
check()test harness had two startup race conditions that caused events to be silently lost, starving the assertion loop until the 10-second timeout fired.The first race is between the extractor starting and the NATS subscription being established. In the current master code, the subscriber is created after the extractor was already spawned and publishing. Any events published during that window (such as startup log events from wallet creation,
spkManactivation, etc.) could be delivered to NATS with no subscriber listening. For tests with a no-optest_setup()(likelog_events,logtimemicros,unknown_with_threadname), those startup events were the only events that could satisfy their predicates, so they time out when the race is lost.The second race is between
test_setup()firing andtail -fentering its follow mode. Thetail -f debug.log > pipebridge reads existing file content on startup, but there's a gap before it begins watching for new appends via inotify/polling. Iftest_setup()generates new log lines during that gap (e.g.submitblockwritingblock-connectedevents),tail -fnever delivers them and the test times out. This affected tests likeblock_connected,block_checked, and the mutated block tests.Both races were confirmed by CI failures on intermediate commits during development: run 22341358999 proved the
tail -ffollow race, and run 22343481310 proved the subscription ordering race.Completely resolves #366!
Context
The log extractor integration test harness (
check()) orchestrates pipeline involving two regtestbitcoindnodes, a NATS server, a named pipe viamkfifo, atail -fbridge process, and the log extractor itself. The test generates Bitcoin Core activity via RPC, then asserts that the corresponding log events arrive via NATS. The pipeline path is:bitcoind debug.log → tail -f → named pipe (FIFO) → log extractor → NATS → test subscriber.The fix introduces two synchronization barriers to
check(), both confined to the test harness (no production code changes):Subscribe before spawn - the NATS
connect()+subscribe("*")is moved before the extractor is spawned, so the subscriber captures every event from the moment the extractor starts publishing.Marker handshake - after spawning the extractor, a unique
PIPELINE_READY:{nanos}line is appended todebug.log. The test waits for this line to arrive via NATS as anUnknownLogMessage, which proves the fulldebug.log → tail -f → pipe → extractor → NATSpipeline is live and following new appends. Any events consumed during this wait are buffered and replayed throughcheck_eventbefore the live stream, preventing event starvation. If the NATS stream closes before the marker arrives, the test fails immediately with a diagnostic message rather than waiting for the generic 10-second timeout.Old
check()flow - racyThe subscription is created after the extractor is already publishing, and
test_setup()fires before the pipeline is proven live:sequenceDiagram participant T as Test harness participant N as NATS Server participant E as Log Extractor participant TF as tail -f participant DL as debug.log T->>N: Start NATS server T->>E: Spawn extractor task activate E E->>N: Connect to NATS E->>TF: mkfifo + spawn tail -f activate TF TF->>DL: Start following debug.log Note over TF,DL: Still catching up on existing content E->>N: Publish startup events Note over E,N: Events already flowing T->>N: Connect and subscribe Note right of T: TOO LATE rect rgba(248, 113, 113, 0.12) Note over T,N: RACE 1 - Subscription created AFTER events published. Lost forever. end T->>DL: test_setup triggers node activity DL-->>TF: New log lines appended rect rgba(248, 113, 113, 0.12) Note over TF,DL: RACE 2 - tail -f may not yet be tailing new writes end TF--xE: Lines may never reach extractor T->>T: select! timeout ... PANIC deactivate E deactivate TFNew
check()flow - synchronizedSubscribe before spawn captures all events; the marker handshake proves pipeline liveness before
test_setup()fires:sequenceDiagram participant T as Test harness participant N as NATS Server participant E as Log Extractor participant TF as tail -f participant DL as debug.log T->>N: Start NATS server rect rgba(52, 211, 153, 0.12) Note over T,N: BARRIER 1 - Subscribe BEFORE spawning extractor T->>N: Connect and subscribe to all subjects end T->>E: Spawn extractor task activate E E->>N: Connect to NATS E->>TF: mkfifo + spawn tail -f activate TF TF->>DL: Start following debug.log E->>N: Publish startup events N-->>T: Startup events captured by pre-existing subscription rect rgba(52, 211, 153, 0.12) Note over T,DL: BARRIER 2 - Marker handshake proves pipeline liveness T->>DL: Append PIPELINE_READY marker to debug.log DL-->>TF: tail -f picks up marker line TF-->>E: Forward via named pipe E->>N: Publish marker as UnknownLogMessage N-->>T: Marker arrives at subscriber Note right of T: Pipeline proven live. Pre-marker events buffered. end T->>DL: test_setup triggers node activity DL-->>TF: New log lines appended TF-->>E: Forward to extractor via pipe E->>N: Publish events N-->>T: Events delivered to subscriber T->>T: Replay buffered events through check_event T->>T: Consume live events - check_event matches Note right of T: SUCCESS deactivate E deactivate TFTesting
The fix was validated across multiple CI runs on the fork:
a2955a00caa70f19ca4790e319c2The intermediate failures were instrumental in confirming that both barriers are necessary - each addresses a distinct, CI-proven failure mode.
A readiness oneshot on the production
run()API was also explored but deliberately dropped to keep the diff test-only (it provided faster diagnostics on extractor startup failure but was not a correctness fix).