Scheduling tasks Int.max ns into future results in an obtuse fatal error (#1056) (#1642)

Motivation:

When scheduling a task too far into the future on a Darwin-kernel,
a fatal error is produced. Whilst the error indicates that a parameter
to the kevent syscall was invalid (EINVAL) it gives no further
information as to the root cause.

As the user's intent is to schedule an task far into the future, a fatal
error should not result. Instead the time should be clamped to the
maximum value that the Darwin-kernel supports. This ensures that
, provided the system remains running, the task will eventually run
(though this may be several yeasr into the future).

Modifications:

Duplicated the bounds checks from those in Darwin-kernel interval timer,
clamping timespec values to the maximum value. Added an assert to check
that the timespec tv_nsec field is valid (according to definition of
the timespec datatype in ISO C11). And a precondition on the tv_sec
field being > 0 (tasks schedule with times <= 0 should not result in
a kevent being registered as they are scheduled immediately).

Added note clarifying behaviour under Darwin-based OSes to the EventLoop
scheduleTask() method.

Added new unit test scheduling a task at the maximum value (which
under any regression would crash).

Result:

Scheduled tasks where the timeout exceed the maximum value supported by
Darwin-based OSes will be clamped to the maximum.

Co-authored-by: Graeme Jenkinson <graemee_jenkinson@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
authored and GitHub committed 2020-09-24 15:05:54 +01:00
1 parent cf9140a686
commit 976ccdd0d0
4 files changed
+42 -1

No files matched your search

+1
View File
@@ -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<T>(in: TimeAmount, _ task: @escaping () throws -> T) -> Scheduled<T>
+18 -1
View File
@@ -372,7 +372,24 @@ internal class Selector<R: Registration> {
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
}
}
@@ -78,6 +78,7 @@ extension EventLoopTest {
("testWeFailOutstandingScheduledTasksOnELShutdown", testWeFailOutstandingScheduledTasksOnELShutdown),
("testSchedulingTaskOnFutureFailedByELShutdownDoesNotMakeUsExplode", testSchedulingTaskOnFutureFailedByELShutdownDoesNotMakeUsExplode),
("testEventLoopGroupProvider", testEventLoopGroupProvider),
("testScheduleMaximum", testScheduleMaximum),
]
}
}
+22
View File
@@ -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)
}
}