fix: flush non-blocking log appender on shutdown instead of mem::forget#2
Open
orrinfrazier wants to merge 1 commit into
Open
fix: flush non-blocking log appender on shutdown instead of mem::forget#2orrinfrazier wants to merge 1 commit into
orrinfrazier wants to merge 1 commit into
Conversation
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
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.
Closes #12
Summary
init_loggingleaked thetracing_appenderWorkerGuardviamem::forget, so buffered log lines were never guaranteed to flush on shutdown — losing the final lines that matter for crash/shutdown post-mortems.WorkerGuardfrominit_logging(now#[must_use]) and hold it inmain()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 privatecreate_file_appender(config) -> (NonBlocking, WorkerGuard); changeinit_loggingto returnWorkerGuard; removemem::forget(guard)and its import.main.rs: bindlet _log_flush_guard = init_logging(&config);before the tokio runtime so it drops last on scope exit (afterdrop(rt)andinfo!("Shutdown complete.")).lib.rs: update the doc example to hold the guard.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— cleancargo fmt -p cuprated -- --check— cleancargo build --bin cuprated— buildsNotes
tempfilewas already a dev-dependency,tracing-appenderalready a dependency).mainrather thanNode:init_loggingruns beforeNode::launch, and the terminalShutdown complete.line is emitted inmainafter the node/runtime drop, so the guard must outliveNode.Refs:
issues/S10-1-log-flush-shutdown.md