Skip to content

[Concurrency] Don't pass negative times to the Dispatch code. #81335

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

Open
wants to merge 2 commits into
base: release/6.2
Choose a base branch
from

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
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

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.

2 participants