Skip to content

AppLog drops lines written on the way out of the process, so clean shutdown is usually lost #174

Closed

Description

@chattymin

AppLog.write hands the line to a background queue (AppLog.swift:28) and returns. When the caller is on its way out of the process, the line races the teardown and is frequently lost.

The site that matters today is CrashReporter.markClean() (CrashReporter.swift:65-68), which runs from the NSApplication.willTerminateNotification observer registered in install() (:45-47). AppKit posts that notification and then exits, so clean shutdown is written only if the log queue happens to get scheduled first.

Measured with an AppKit app reproducing the sequence — enqueue on a serial DispatchQueue, then NSApp.terminate(nil):

runs=100 landed=58 lost=42

Consistent with what is on disk here: the current 1.2M PokeTokenBar.log has 1 launch: line and 0 clean shutdown lines; the rotated 2.0M PokeTokenBar.old.log has 1 and 1.

The consequence is not just a missing line. clean shutdown is the human-readable half of the shutdown record, so the log reads as if sessions end abruptly even when they don't. The marker file itself is removed synchronously (FileManager.removeItem, :66), so the detection in install() :37 is unaffected — this is a diagnostics defect, not a false crash report.

Fix shape: an AppLog.flush() that does queue.sync {}, called on the paths that exit right after logging. Sweep for callers, not just this one — #163 adds a second such site in applicationDidFinishLaunching.

Class, for docs/reference/defect-log.md: an async logger is not a logger on an exit path. Any AppLog.write whose caller reaches exit() in the same turn needs a flush, and the absence of the line cannot be used as evidence that the branch did not run.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomershelp wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions