Skip to content

Conversation

@al45tair
Copy link
Contributor

@al45tair al45tair commented May 6, 2025

Dispatch uses unsigned times, and cannot cope with times before its clock started. As such, passing negative times from Swift through to the C++ code results in large unsigned values, which then causes us to wait forever. This is undesirable.

rdar://148899609

Dispatch uses unsigned times, and cannot cope with times before its
clock started.  As such, passing negative times from Swift through to
the C++ code results in large unsigned values, which then causes us to
wait forever.  This is undesirable.

rdar://148899609
@al45tair al45tair requested a review from a team as a code owner May 6, 2025 15:31
@al45tair al45tair added 🍒 release cherry pick Flag: Release branch cherry picks swift 6.2 labels May 6, 2025
…och.

Add a test to check that attempting to `Task.sleep(until:)` passing a
time before the start of the Dispatch clocks works and doesn't wait
forever.

rdar://148899609
@al45tair
Copy link
Contributor Author

al45tair commented May 6, 2025

Explanation: If you ask Task.sleep() to sleep until a time before the Dispatch clock started, we internally compute a negative timestamp, which can't be represented by Dispatch as it only uses unsigned times, then cast it to unsigned, check it against DISPATCH_TIME_MAX_VALUE and since it's higher than that, use DISPATCH_TIME_FOREVER.
Risk: Low. Fixes a bug that only triggers if you ask to wait for a time before the start of the Dispatch clock.
Original PR: #81332
Reviewed by: @ktoso, @mikeash
Resolves: rdar://148899609
Tests: I've added a test that fails before this code was added and passes afterwards.

@al45tair
Copy link
Contributor Author

al45tair commented May 6, 2025

@swift-ci Please test

@al45tair al45tair requested a review from mikeash May 9, 2025 09:24
func clamp(_ components: (seconds: Int64, attoseconds: Int64))
-> (seconds: Int64, attoseconds: Int64) {
if components.seconds < 0
|| components.seconds == 0 && components.attoseconds < 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a time with positive seconds and negative attoseconds actually make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-) All three of us worried about that. It might make sense, but it can't happen because these components are calculated from a 128-bit attosecond count in Swift.Duration — so you could never end up with positive seconds and negative attoseconds.

@al45tair al45tair merged commit 02c160c into swiftlang:release/6.2 May 13, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🍒 release cherry pick Flag: Release branch cherry picks swift 6.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants