Skip to content

fix(log/test): add readiness barrier to prevent test startup race - #372

Merged
0xB10C merged 1 commit into
peer-observer:masterfrom
deadmanoz:fix/log-extractor-readiness-barrier
Mar 10, 2026
Merged

0xB10C merged 1 commit into
peer-observer:masterfrom
deadmanoz:fix/log-extractor-readiness-barrier

Conversation

@deadmanoz

@deadmanoz deadmanoz commented Feb 26, 2026 •

Copy link
Copy Markdown
Collaborator

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, spkMan activation, etc.) could be delivered to NATS with no subscriber listening. For tests with a no-op test_setup() (like log_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 and tail -f entering its follow mode. The tail -f debug.log > pipe bridge reads existing file content on startup, but there's a gap before it begins watching for new appends via inotify/polling. If test_setup() generates new log lines during that gap (e.g. submitblock writing block-connected events), tail -f never delivers them and the test times out. This affected tests like block_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 -f follow race, and run 22343481310 proved the subscription ordering race.

Completely resolves #366!

Context

The log extractor integration test harness (check()) orchestrates pipeline involving two regtest bitcoind nodes, a NATS server, a named pipe via mkfifo, a tail -f bridge 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):

  1. 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.

  2. Marker handshake - after spawning the extractor, a unique PIPELINE_READY:{nanos} line is appended to debug.log. The test waits for this line to arrive via NATS as an UnknownLogMessage, which proves the full debug.log → tail -f → pipe → extractor → NATS pipeline is live and following new appends. Any events consumed during this wait are buffered and replayed through check_event before 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 - racy

The 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 TF
Loading

New check() flow - synchronized

Subscribe 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 TF
Loading

Testing

The fix was validated across multiple CI runs on the fork:

Commit Barriers present CI result
a2955a0 readiness oneshot only 3 tests flaked (run 22341358999)
0caa70f + event buffering 3 tests flaked (run 22343481310)
19ca479 + subscribe before spawn all 10 jobs green (run 22344475830)
0e319c2 squashed final (subscribe + marker) awaiting CI

The 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).

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.
@0xB10C

0xB10C commented Mar 10, 2026

Copy link
Copy Markdown
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).

@0xB10C
0xB10C merged commit ece5d0a into peer-observer:master Mar 10, 2026
10 checks passed
@0xB10C

0xB10C commented Mar 10, 2026

Copy link
Copy Markdown
Collaborator

Thanks. Let's see if there are any other intermittent failures

@deadmanoz
deadmanoz deleted the fix/log-extractor-readiness-barrier branch May 28, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test flakiness uncovered by nix-coverage job

2 participants