Skip to content

🍒[5.9][TaskGroup] Fix unlock order, add missing detaches and add more assertions #67819

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 2 commits into from
Aug 11, 2023

Conversation

ktoso
Copy link
Contributor

@ktoso ktoso commented Aug 9, 2023

Description: A task group resumes the "waiting task" in numerous situations. Currently tasks were scheduled and then the group was unlocked -- this can lead to races between the scheduled task and the group unlock and unpredictable behavior. Instead, we must unlock the group and THEN schedule the waiting task on order to avoid potential use-after free of the lock (as the unlock() happens).
Risk: Medium, the change reorganizes code in order to allow us to unlock and THEN schedule the task. This forced some general refactoring in order to be able to get this pattern.
Reward Medium, resolves very rare crashes which could occur when just the right scheduling timing would happen. These issues are very rare, and have remained undetected until recently.
Review by: @mikeash @DougGregor
Testing: CI testing, enabled all task group tests for the first time in a long time and all passing consistently on all platforms.
Original PR: #67590
Radar: rdar://113331923 (test reenable rdar://113016918)
Related Radar: The following was the same issue however in a more crucial code path: rdar://113032582

@ktoso ktoso requested a review from a team as a code owner August 9, 2023 08:21
reenable async_taskgroup_discarding_dontLeak.swift

[DiscardingTaskGroup] Properly detach when LAST task is failed, and prior failure was already stored

[TaskGroup] Must detach discarded task, THEN unlock before resume waiting

revamping locking scheme, test this a bunch

stabilize println based test a bit more against timing

re-enable tsan test: async_taskgroup_next

reenable async_taskgroup_next_on_pending

disable debugging tricks

unlock test: async_taskgroup_asynciterator_semantics

make use of unreachable

cleanups

cleanup for freestanding mode
@ktoso ktoso force-pushed the pick-taks-group-unlock-before-run branch from 1ab7b64 to 2e6381f Compare August 9, 2023 08:23
@ktoso
Copy link
Contributor Author

ktoso commented Aug 9, 2023

@swift-ci please test

@ktoso ktoso added 🍒 release cherry pick Flag: Release branch cherry picks swift 5.9 concurrency runtime Feature: umbrella label for concurrency runtime features labels Aug 9, 2023
@ktoso ktoso changed the title [5.9][TaskGroup] Fix unlock order, add missing detaches and add more assertions 🍒[5.9][TaskGroup] Fix unlock order, add missing detaches and add more assertions Aug 9, 2023
@ktoso ktoso requested review from mikeash and al45tair August 9, 2023 09:33
Copy link
Contributor

@al45tair al45tair left a comment

Choose a reason for hiding this comment

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

LGTM

auto waitingContext =
static_cast<TaskFutureWaitAsyncContext *>(
waitingTask->ResumeContext);
// Run the task.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this comment isn't entirely accurate :-) (same as on the review of the original PR).

@ktoso
Copy link
Contributor Author

ktoso commented Aug 10, 2023

Added the docs-only code review change 1195955 from the original PR review.

Might as well give this another full test run as well 👍

@ktoso
Copy link
Contributor Author

ktoso commented Aug 10, 2023

@swift-ci please test

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

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

LGTM

@ktoso ktoso merged commit 0027fac into swiftlang:release/5.9 Aug 11, 2023
@ktoso ktoso deleted the pick-taks-group-unlock-before-run branch August 11, 2023 02:19
ktoso added a commit to ktoso/swift that referenced this pull request Aug 11, 2023
…before-run

🍒[5.9][TaskGroup] Fix unlock order, add missing detaches and add more assertions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concurrency runtime Feature: umbrella label for concurrency runtime features 🍒 release cherry pick Flag: Release branch cherry picks swift 5.9
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants