Skip to content

T-1365 Send queued logs when the JVM exits - #31

Merged
PetrHeinz merged 2 commits into
mainfrom
claude/t-1365-flush-on-exit
Sep 25, 2026
Merged

PetrHeinz merged 2 commits into
mainfrom
claude/t-1365-flush-on-exit

Conversation

@PetrHeinz

Copy link
Copy Markdown
Member

The sender runs on a daemon thread, so an application that returns from main without stopping logback takes the JVM down with whatever is still queued in the appender: the docs' own "start logging" example (log a line, return) never sends anything unless the batch interval happens to fire first. And even when logback is stopped explicitly, stop() returned immediately whenever a flush was already in progress on another thread, leaving the in-flight batch and anything queued behind it to the daemon thread that the exiting JVM then kills.

Two changes in LogtailAppender:

  • start() registers a JVM shutdown hook that calls stop(), and stop() deregisters it again so contexts stopped normally (logback's own <shutdownHook/>, Spring Boot, tests) don't accumulate hooks. When stop() runs from a hook, ours or logback's, removeShutdownHook throws IllegalStateException; that is the one case deliberately ignored.
  • The isFlushing flag becomes a ReentrantLock held for the whole flush, including the sub-batch loop that used to be a recursion. flush() still skips when another flush holds the lock; stop() now blocks on it, marks the appender stopped so no new events slip in, and sends everything left. Subclasses that read the protected isFlushing field can use flushLock.isLocked() instead.

The test decorator's private lock existed only to wait for an asynchronous flush, so it now waits on the appender's lock instead.

The first commit is the two failing tests on their own and is expected to fail CI: one spawns a JVM that logs a line against a local HTTP server and returns from main, the other holds a flush inside the request and checks that stop() waits for it. The second commit makes them pass.

🤖 Generated with Claude Code

PetrHeinz and others added 2 commits September 23, 2026 16:28
… or stop() overlaps a flush

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@PetrHeinz
PetrHeinz marked this pull request as ready for review September 25, 2026 14:41
@PetrHeinz
PetrHeinz merged commit 124e12a into main Sep 25, 2026
5 checks passed
@PetrHeinz
PetrHeinz deleted the claude/t-1365-flush-on-exit branch September 25, 2026 15:40
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.

1 participant