Skip to content

Commit 85742bc

Browse files
authored
Avoid using stderr explicitly (#3089)
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.
1 parent c2ccad6 commit 85742bc

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Sources/NIOPosix/SelectableEventLoop.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ import NIOConcurrencyHelpers
1919
import NIOCore
2020
import _NIODataStructures
2121

22+
private func printError(_ string: StaticString) {
23+
string.withUTF8Buffer { buf in
24+
var buf = buf
25+
while buf.count > 0 {
26+
// 2 is stderr
27+
let rc = write(2, buf.baseAddress, buf.count)
28+
if rc < 0 {
29+
let err = errno
30+
if err == EINTR { continue }
31+
fatalError("Unexpected error writing: \(err)")
32+
}
33+
buf = .init(rebasing: buf.dropFirst(Int(rc)))
34+
}
35+
}
36+
}
37+
2238
/// Execute the given closure and ensure we release all auto pools if needed.
2339
@inlinable
2440
internal func withAutoReleasePool<T>(_ execute: () throws -> T) rethrows -> T {
@@ -438,12 +454,11 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
438454
if Self.strictModeEnabled {
439455
fatalError("Cannot schedule tasks on an EventLoop that has already shut down.")
440456
}
441-
fputs(
457+
printError(
442458
"""
443459
ERROR: Cannot schedule tasks on an EventLoop that has already shut down. \
444460
This will be upgraded to a forced crash in future SwiftNIO versions.\n
445-
""",
446-
stderr
461+
"""
447462
)
448463
return false
449464
}

0 commit comments

Comments
 (0)