Skip to content

fix: flush non-blocking log appender on shutdown instead of mem::forget#2

Open
orrinfrazier wants to merge 1 commit into
mainfrom
fix/s10-1-log-flush-shutdown
Open

fix: flush non-blocking log appender on shutdown instead of mem::forget#2
orrinfrazier wants to merge 1 commit into
mainfrom
fix/s10-1-log-flush-shutdown

Conversation

@orrinfrazier
Copy link
Copy Markdown
Owner

@orrinfrazier orrinfrazier commented May 24, 2026

Closes #12

Summary

  • init_logging leaked the tracing_appender WorkerGuard via mem::forget, so buffered log lines were never guaranteed to flush on shutdown — losing the final lines that matter for crash/shutdown post-mortems.
  • Return the WorkerGuard from init_logging (now #[must_use]) and hold it in main() for the program lifetime, so it drops — and flushes the file appender — after the final shutdown log line.

Changes

  • logging.rs: extract the appender construction into a private create_file_appender(config) -> (NonBlocking, WorkerGuard); change init_logging to return WorkerGuard; remove mem::forget(guard) and its import.
  • main.rs: bind let _log_flush_guard = init_logging(&config); before the tokio runtime so it drops last on scope exit (after drop(rt) and info!("Shutdown complete.")).
  • lib.rs: update the doc example to hold the guard.
  • Add a unit test (file_appender_flushes_on_guard_drop) that writes a line, drops the guard, and asserts the line was flushed to a temp log file.

Testing

  • cargo test -p cuprated --lib — 12 passed, 0 failed (includes the new flush test)
  • cargo clippy -p cuprated --all-targets -- -D warnings — clean
  • cargo fmt -p cuprated -- --check — clean
  • cargo build --bin cuprated — builds

Notes

  • No new dependencies (tempfile was already a dev-dependency, tracing-appender already a dependency).
  • The guard is intentionally held in main rather than Node: init_logging runs before Node::launch, and the terminal Shutdown complete. line is emitted in main after the node/runtime drop, so the guard must outlive Node.

Refs: issues/S10-1-log-flush-shutdown.md

init_logging built the tracing_appender non-blocking writer and immediately
leaked its WorkerGuard with mem::forget, so buffered log lines were never
guaranteed to flush to the file on shutdown — losing precisely the final
lines that matter for crash/shutdown post-mortems.

Extract the appender construction into create_file_appender and return the
WorkerGuard from init_logging (now #[must_use]). main() binds it before the
tokio runtime so it drops last on scope exit — after drop(rt) and the final
'Shutdown complete.' line — flushing the file appender on shutdown.

Add a unit test that writes a line, drops the guard, and asserts the line
was flushed to disk.

Refs: issues/S10-1-log-flush-shutdown.md
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.

cuprated: flush non-blocking log appender on shutdown instead of mem::forget

1 participant