Skip to content

🍒[5.9][TaskGroup] Reenable test and fix memory issue #67700

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

Conversation

ktoso
Copy link
Contributor

@ktoso ktoso commented Aug 3, 2023

Description: This re-enables a task group test and fixes a memory issue where we realized we must release the group lock before resuming the waiting task. If we don't do this, we're racing the group's destroy() (coming from the parent task, waiting on waitAll with the unlock()), risking calling unlock() on already released memory - very rarely causing crashes due to this. It is hard to reproduce this issue, due to the race appearing very rarely, but it is consistently reproducible in some environments.
Risk: Low, the fix reorders an unlock() with the waitingTask resume which cannot cause any other consistency issues as there are no other actions taken after the resume. Arguably, this always should have been release + run order.
Reward: Medium, it seems this crash does not manifest frequently in real applications, however it is a real issue we need to fix.
Review by: @al45tair @erik
Testing: CI testing, confirmed the fix on specially prepared environments.
Original PR: #67392
Radar: rdar://112759360 rdar://110474311

ktoso added 2 commits August 3, 2023 18:02
released memory.

Because the waitAll() group method is called before the group returns
from withTaskGroup and is used to ensure all tasks have completed before
we destroy the task group, this method is then immediately followed by
calling TaskGroup::destroy.

If we, as previously, first resume the waiting task and then attempt to
unlock the lock held by the group, we are in an unsafe situation with
racing the task group destroy() and the unlock().

Therefore, we must release the lock before we resume the waiting task.
@ktoso ktoso requested a review from a team as a code owner August 3, 2023 11:01
@ktoso
Copy link
Contributor Author

ktoso commented Aug 3, 2023

@swift-ci please test

@ktoso ktoso changed the title Reenable test and fix potential memory [TaskGroup] Reenable test and fix memory issue Aug 3, 2023
@ktoso ktoso added concurrency Feature: umbrella label for concurrency language features 🍒 release cherry pick Flag: Release branch cherry picks swift 5.9 concurrency runtime Feature: umbrella label for concurrency runtime features labels Aug 3, 2023
@ktoso ktoso changed the title [TaskGroup] Reenable test and fix memory issue 🍒[5.9][TaskGroup] Reenable test and fix memory issue Aug 4, 2023
@DougGregor DougGregor merged commit b0adffb into swiftlang:release/5.9 Aug 4, 2023
@ktoso ktoso deleted the pick-reenable-async_taskgroup_discarding_dontLeak_class_error.swift branch August 7, 2023 02:10
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 concurrency Feature: umbrella label for concurrency language 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.

3 participants