Skip to content

Fixed issues with rolling file and thread interruption - #483

Open
faogustavo wants to merge 1 commit into
mainfrom
gv/480
Open

Fixed issues with rolling file and thread interruption#483
faogustavo wants to merge 1 commit into
mainfrom
gv/480

Conversation

@faogustavo

Copy link
Copy Markdown
Contributor

Closes: #480

To prevent the issues with the rolling file implementation, two changes have been implemented:

  1. Included a buffer to store some logs, so the blocking is less frequent
    1. Note that the block only happened until the entry entered the flow. Once it was in the call, it was unblocked
    2. In this case, adding a buffer only grants us flexibility, keeping the original implementation
  2. Created a send function that properly handles thread interruption so it does not propagate it as a crash to the users

To ensure the changes correctly fix the problem, a new test class was added to the jvmTest sourceset, which is the only one with Thread and this interruption behavior.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 sendBlockingUnlessInterrupted helper 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].
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.

RollingFileLogWriter can throw InterruptedException

3 participants