Skip to content

Commit

Permalink
Avoid using stderr explicitly
Browse files Browse the repository at this point in the history
Motivation

It's tempting for us to use the FILE * for stderr whenever we
want to print to it. However, the Linux libc modules aren't
appropriately annotated to call that field constant, and indeed it
isn't. This causes Swift to throw a fit if we even touch it.

Modifications

Introduce an awkward function that issues direct writes to
stderr instead of using the FILE *.

Result

Swift is happier.
  • Loading branch information
Lukasa committed Jan 24, 2025
1 parent 5a572f5 commit 5cac7a0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Sources/NIOPosix/SelectableEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ import NIOConcurrencyHelpers
import NIOCore
import _NIODataStructures

private func printError(_ string: StaticString) {
string.withUTF8Buffer { buf in
var buf = buf
while buf.count > 0 {
// 2 is stderr
let rc = write(2, buf.baseAddress, buf.count)
if rc < 0 {
let err = errno
if err == EINTR { continue }
fatalError("Unexpected error writing: \(err)")
}
buf = .init(rebasing: buf.dropFirst(Int(rc)))
}
}
}

/// Execute the given closure and ensure we release all auto pools if needed.
@inlinable
internal func withAutoReleasePool<T>(_ execute: () throws -> T) rethrows -> T {
Expand Down Expand Up @@ -438,12 +454,11 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
if Self.strictModeEnabled {
fatalError("Cannot schedule tasks on an EventLoop that has already shut down.")
}
fputs(
printError(
"""
ERROR: Cannot schedule tasks on an EventLoop that has already shut down. \
This will be upgraded to a forced crash in future SwiftNIO versions.\n
""",
stderr
"""
)
return false
}
Expand Down

0 comments on commit 5cac7a0

Please sign in to comment.