mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
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>