Fixed issues with rolling file and thread interruption - #483
Open
faogustavo wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #480 where RollingFileLogWriter could crash callers by propagating InterruptedException from trySendBlocking, and reduces how often logging threads block by adding bounded buffering between callers and the writer coroutine.
Changes:
- Introduces a multiplatform
sendBlockingUnlessInterruptedhelper to avoid blocking/throwing on already-interrupted JVM threads while preserving interrupt state. - Switches
RollingFileLogWriter’s internal channel from rendezvous to a bounded buffer (LOGGING_CHANNEL_CAPACITY) to reduce blocking frequency. - Adds a JVM-specific regression test that reproduces logging from an interrupted thread and asserts it does not throw or clear the interrupt flag.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| kermit-io/src/commonMain/kotlin/co/touchlab/kermit/io/RollingFileLogWriter.kt | Buffers log messages via a bounded channel and routes sends through the new interruption-aware helper. |
| kermit-io/src/commonMain/kotlin/co/touchlab/kermit/io/ChannelSend.kt | Declares the expect API and documents the interruption/behavior contract. |
| kermit-io/src/commonJvmMain/kotlin/co/touchlab/kermit/io/ChannelSend.kt | Implements JVM behavior to avoid blocking/throwing on interrupted threads and restores interrupt state if consumed. |
| kermit-io/src/nativeMain/kotlin/co/touchlab/kermit/io/ChannelSend.kt | Implements Native behavior by delegating to trySendBlocking (no thread interruption). |
| kermit-io/src/commonJvmTest/kotlin/co/touchlab/kermit/io/RollingFileLogWriterInterruptTest.kt | Adds a regression test to verify interrupted-thread logging does not crash and preserves the interrupt flag. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+46
to
+50
| // rollOnSize = 0 plus a large maxLogFiles makes rollLogs() walk thousands of paths for every single message, so the writer | ||
| // coroutine drains the channel far slower than this thread can fill it. That keeps the channel buffer full, which is what | ||
| // deterministically forces the send onto its blocking path instead of relying on a race. | ||
| val writer = createWriter(dir, rollOnSize = 0, maxLogFiles = 20_000) | ||
|
|
Comment on lines
56
to
+60
| * Writes to the file are done by a different coroutine. The main reason for this is to make writes to the log file sink thread-safe, and | ||
| * so that file rolling can be performed without additional synchronization or locking. The channel that buffers log messages is currently | ||
| * unbuffered, so logging threads will block until the I/O is complete. However, buffering could easily be introduced to potentially | ||
| * increase logging throughput. The envisioned usage scenarios for this class probably do not warrant this. | ||
| * so that file rolling can be performed without additional synchronization or locking. Log messages reach that coroutine through a channel | ||
| * that buffers up to [LOGGING_CHANNEL_CAPACITY] of them, so logging threads normally hand a message off and return without waiting for the | ||
| * I/O. Once that buffer is full, logging threads block until the writer catches up, which keeps memory bounded and limits how many messages | ||
| * can be lost if the process dies. Threads that have already been interrupted are never blocked -- see [sendBlockingUnlessInterrupted]. |
KevinSchildhorn
approved these changes
Aug 7, 2026
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: #480
To prevent the issues with the rolling file implementation, two changes have been implemented:
To ensure the changes correctly fix the problem, a new test class was added to the
jvmTestsourceset, which is the only one with Thread and this interruption behavior.