Skip to content

FileLogger: use throwing FileHandle API so a failed write cannot cras… - #31

Open
MartinPrusa wants to merge 1 commit into
Qase:masterfrom
MartinPrusa:fix/file-logger-crash-on-failed-write
Open

MartinPrusa wants to merge 1 commit into
Qase:masterfrom
MartinPrusa:fix/file-logger-crash-on-failed-write

Conversation

@MartinPrusa

Copy link
Copy Markdown

Problem
FileLogger.log(:) appends through the legacy FileHandle.seekToEndOfFile() / write(:) API. When the underlying write fails (typically ENOSPC, "No space left on device", when the device storage is full) Foundation raises an Objective-C NSFileHandleOperationException. That exception cannot be caught from Swift, so the whole app crashes on the logger's background queue.

Fix
Use the throwing FileHandle API (seekToEnd(), write(contentsOf:), close()), available since iOS 13.4 / macOS 10.15.4, well below the package's platform floor. A failed write now throws a Swift error that lands in the existing catch and is reported through loggerForInternalErrors; the entry is dropped and the app keeps running.
currentWritableFileHandle is now internal (was private) so the test can inject a failing handle.
Test
test_failed_write_is_reported_instead_of_crashing swaps the writable handle for a read-only one and logs again. With the old API the test process dies with NSFileHandleOperationException: Bad file descriptor; with this change the error is reported and the previously written content stays intact.

…h the app

`seekToEndOfFile()` and `write(_:)` raise an Objective-C `NSFileHandleOperationException`
when the underlying write fails (e.g. "No space left on device"). Such an exception cannot
be caught from Swift, so a full device storage took the whole app down.

Switch to `seekToEnd()` / `write(contentsOf:)` / `close()`, which throw a Swift error that
lands in the existing `catch` and is reported through `loggerForInternalErrors` instead.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
}
}

func test_failed_write_is_reported_instead_of_crashing() throws {

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.

This does not test actual fix at all. I think verify this fix with a unit would be to difficult, so I propose simply removing this and merge it without test.

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.

2 participants