Skip to content

[Concurrency] Fix Task.sleep on values greater than Int64.max. #78817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented Jan 22, 2025

In the Dispatch implementation, clamp the delay to INT64_MAX. Swift's nanoseconds value is unsigned, but we ultimately use it with dispatch_time, which takes a signed int64_t. Extremely large values get interpreted as negative, which results in not sleeping.

INT64_MAX nanoseconds is about 292 years, so it should be difficult to notice a practical effect from sleeping for less time than requested due to the clamping.

rdar://143278824

In the Dispatch implementation, clamp the delay to INT64_MAX. Swift's nanoseconds value is unsigned, but we ultimately use it with dispatch_time, which takes a signed int64_t. Extremely large values get interpreted as negative, which results in not sleeping.

INT64_MAX nanoseconds is about 292 years, so it should be difficult to notice a practical effect from sleeping for less time than requested due to the clamping.

rdar://143278824
@mikeash mikeash requested a review from ktoso as a code owner January 22, 2025 17:47
@mikeash
Copy link
Contributor Author

mikeash commented Jan 22, 2025

@swift-ci please test

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Lgtm! That’s totally the tight thing to do here, thank you!

@mikeash
Copy link
Contributor Author

mikeash commented Feb 18, 2025

@swift-ci please test

@mikeash mikeash enabled auto-merge February 18, 2025 21:19
@mikeash mikeash merged commit 44ff7e5 into swiftlang:main Feb 19, 2025
3 of 5 checks passed
@tbkka
Copy link
Contributor

tbkka commented Apr 21, 2025

Hmmmm..... We recently had some reports of negative sleep times causing problems. I think @al45tair is looking into some of those. Wonder if it's related to this?

@mikeash
Copy link
Contributor Author

mikeash commented Apr 21, 2025

This may have re-broken with the recent executor changes.

@al45tair
Copy link
Contributor

I think it's likely a related problem — I don't think it's re-broken since the async_task_sleep test wouldn't have passed.

@al45tair
Copy link
Contributor

(Also, the "negative sleep times" thing is actually being caused by waiting for an absolute time prior to now, rather than waiting for a negative delay.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants