diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index 3f364551e..8da7614de 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -255,6 +255,7 @@ public protocol EventLoop: EventLoopGroup { /// on the completion of the task. /// /// - note: You can only cancel a task before it has started executing. + /// - note: The `in` value is clamped to a maximum when running on a Darwin-kernel. @discardableResult func scheduleTask(in: TimeAmount, _ task: @escaping () throws -> T) -> Scheduled diff --git a/Sources/NIO/Selector.swift b/Sources/NIO/Selector.swift index d6bb4d19b..3a2cbec7d 100644 --- a/Sources/NIO/Selector.swift +++ b/Sources/NIO/Selector.swift @@ -372,7 +372,24 @@ internal class Selector { case .now: return timespec(tv_sec: 0, tv_nsec: 0) case .blockUntilTimeout(let nanoseconds): - return timespec(timeAmount: nanoseconds) + // A scheduledTask() specified with a zero or negative timeAmount, will be scheduled immediately + // and therefore SHOULD NOT result in a kevent being created with a negative or zero timespec. + precondition(nanoseconds.nanoseconds > 0, "\(nanoseconds) is invalid (0 < nanoseconds)") + + var ts = timespec(timeAmount: nanoseconds) + // Check that the timespec tv_nsec field conforms to the definition in the C11 standard (ISO/IEC 9899:2011). + assert((0..<1_000_000_000).contains(ts.tv_nsec), "\(ts) is invalid (0 <= tv_nsec < 1_000_000_000)") + + // The maximum value in seconds supported by the Darwin-kernel interval timer (kern_time.c:itimerfix()) + // (note that - whilst unlikely - this value *could* change). + let sysIntervalTimerMaxSec = 100_000_000 + + // Clamp the timespec tv_sec value to the maximum supported by the Darwin-kernel. + // Whilst this schedules the event far into the future (several years) it will still be triggered provided + // the system stays up. + ts.tv_sec = min(ts.tv_sec, sysIntervalTimerMaxSec) + + return ts } } diff --git a/Tests/NIOTests/EventLoopTest+XCTest.swift b/Tests/NIOTests/EventLoopTest+XCTest.swift index 6570826f4..c4d1544e8 100644 --- a/Tests/NIOTests/EventLoopTest+XCTest.swift +++ b/Tests/NIOTests/EventLoopTest+XCTest.swift @@ -78,6 +78,7 @@ extension EventLoopTest { ("testWeFailOutstandingScheduledTasksOnELShutdown", testWeFailOutstandingScheduledTasksOnELShutdown), ("testSchedulingTaskOnFutureFailedByELShutdownDoesNotMakeUsExplode", testSchedulingTaskOnFutureFailedByELShutdownDoesNotMakeUsExplode), ("testEventLoopGroupProvider", testEventLoopGroupProvider), + ("testScheduleMaximum", testScheduleMaximum), ] } } diff --git a/Tests/NIOTests/EventLoopTest.swift b/Tests/NIOTests/EventLoopTest.swift index 9be329c2a..cc5d3a2cb 100644 --- a/Tests/NIOTests/EventLoopTest.swift +++ b/Tests/NIOTests/EventLoopTest.swift @@ -1295,4 +1295,26 @@ public final class EventLoopTest : XCTestCase { XCTFail("Not the same") } } + + // Test that scheduling a task at the maximum value doesn't crash. + // (Crashing resulted from an EINVAL/IOException thrown by the kevent + // syscall when the timeout value exceeded the maximum supported by + // the Darwin kernel #1056). + public func testScheduleMaximum() { + let eventLoop = EmbeddedEventLoop() + let maxAmount: TimeAmount = .nanoseconds(.max) + let scheduled = eventLoop.scheduleTask(in: maxAmount) { true } + + var result: Bool? + var error: Error? + scheduled.futureResult.whenSuccess { result = $0 } + scheduled.futureResult.whenFailure { error = $0 } + + scheduled.cancel() + + XCTAssertTrue(scheduled.futureResult.isFulfilled) + XCTAssertNil(result) + XCTAssertEqual(error as? EventLoopError, .cancelled) + } + }