Skip to content

test/app + tutorial: safety-hang on WAL corruption exits the process (code 0) instead of hanging #1614

Open

Description

@iamdenyo

Summary

The engine's documented "safety-hang" mechanism — never auto-restart on a corrupted WAL, because an unsafe restart risks double-signing — is silently defeated by crates/test/app's own run() wiring, and the official tutorial (docs/tutorials/channels.md) teaches the exact same broken pattern verbatim. The process exits with status 0 instead of hanging, which is the one thing this mechanism exists to prevent.

Related to #1434 (WAL replay corruption tolerance), but distinct: that issue is about whether detection/replay correctly tolerates tail corruption from an interrupted append. This issue is about what happens after detection correctly triggers the safety-hang path — the engine does the right thing, but the application layer that wraps it doesn't keep the process alive.

Background

crates/engine/src/util/failure.rs's hang_on_safety_failure is correct and unit-tested in isolation: on a safety-critical failure (e.g. WAL CRC mismatch), it casts NodeMsg::SafetyFailure and then pending::<()>().awaits forever — that future provably never resolves. crates/engine/src/node.rs's Node actor is designed to stay alive during this state (stop children, keep running) specifically so an external process supervisor sees a stuck, not-exited process and doesn't restart it.

Root cause

crates/test/app/src/node.rs (and identically, docs/tutorials/channels.md):

let app_handle = tokio::spawn(
    async move {
        if let Err(e) = crate::app::run(&mut state, &mut channels).await {
            tracing::error!(%e, "Application error");
        }
    }
    .instrument(span),
);
async fn run(self) -> eyre::Result<()> {
    let handles = self.start().await?;
    handles.app.await.map_err(Into::into)
}

Two independent defects:

  1. The app task's Err is only logged, never propagated — the spawned task's JoinHandle<()> always completes Ok(()).
  2. run() only awaits handles.app — handles.engine.handle (the real Node actor's handle, which is the one designed to never resolve during safety-hang) is never consulted in the exit path at all; it's only ever .abort()-ed on explicit shutdown.

Steps to reproduce

cd code
cargo build -p arc-malachitebft-test-app
BIN=target/debug/malachitebft-test-app

$BIN testnet --home /tmp/malachite-repro --nodes 1
$BIN start --home /tmp/malachite-repro/0 --validator &
PID=$!
# let it write a few WAL entries, then crash it
sleep 1 && kill -9 $PID

# corrupt one byte inside a WAL entry (not the header)
python3 -c "
with open('/tmp/malachite-repro/0/wal/consensus.wal', 'r+b') as f:
    f.seek(300); b = f.read(1); f.seek(300); f.write(bytes([b[0]^0xFF]))
"

$BIN start --home /tmp/malachite-repro/0 --validator
echo "exit code: $?"
ERROR ...: Safety-critical failure; hanging to prevent unsafe auto-restart reason=Corrupted WAL entry encountered: CRC mismatch
ERROR ...: Entering safety-hang state: stopping all children, Node remains alive — operator intervention required before restart
...
exit code: 0

The process exits within milliseconds despite the log claiming it "remains alive."

Impact

Under any supervisor configured to always restart a validator (systemd Restart=always, Docker restart: unless-stopped, Kubernetes restartPolicy: Always — all common for "keep my validator online"), this is exactly the unsafe auto-restart the mechanism exists to prevent. Under a supervisor that only restarts on failure, exit code 0 looks like an intentional, successful stop — no alert at all.

This isn't just a test-harness bug: docs/tutorials/channels.md teaches this exact code, including the comment "The run method... lets the node run until it is stopped (ideally never)" — directly contradicted by the measured behavior. Anyone following the tutorial for the channel-based integration inherits the identical gap.

Suggested fix

let app_handle = tokio::spawn(
    async move { crate::app::run(&mut state, &mut channels).await }.instrument(span),
);

(requires widening Handle.app from JoinHandle<()> to JoinHandle<eyre::Result<()>>)

async fn run(self) -> eyre::Result<()> {
    let mut handles = self.start().await?;

    tokio::select! {
        result = &mut handles.engine.handle => {
            // Node supervisor itself terminated — clean shutdown, or a
            // liveness failure the engine judged safe to restart from.
            result.map_err(|e| eyre::eyre!("Node actor task panicked: {e}"))
        }
        result = &mut handles.app => {
            // App loop ended on its own — may be mid safety-hang. Don't
            // exit on this alone; log, then defer to the handle that only
            // resolves on a genuine shutdown.
            match result {
                Ok(Ok(())) => {}
                Ok(Err(e)) => tracing::error!(%e, "Application error"),
                Err(e) => tracing::error!(%e, "Application task panicked"),
            }
            (&mut handles.engine.handle).await
                .map_err(|e| eyre::eyre!("Node actor task panicked: {e}"))
        }
    }
}

Verified: with the patch applied, the identical repro leaves the process alive (confirmed via kill -0 8s later, no exit) instead of exiting. A normal testnet run with --validator is unaffected.

This should land in both crates/test/app/src/node.rs and docs/tutorials/channels.md. Happy to open a PR for both if useful.

Activity

  1. added
    need-triage
    on Nov 3, 2026
  2. added and removed
    need-triage
    on Nov 3, 2026
  3. romac commented on Nov 3, 2026

    @romac
    Contributor

    Thanks for the report, this issue is already known internally and will be fixed in the next release.

  4. added
    bug
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtracked internally

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions