[6.0][Concurrency] Defensive copying of tasks locals in TaskGroups (rather than crashing) #74038
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Short description: (very detailed discussion/description in #73978)
Previously a specific nesting of
withTaskGroup { group in $taskLocal.withValue() { group.addTask {} } }
was forced to defensively crash in order to prevent the child task wrongly directly linking to the parent's memory (which would be popped when the withValue returned, but the child task was still running).We detected this situation to keep memory safety however this situation can happen in real situations and has caused e.g. swift-testing issues and workarounds.
Instead we are now copying this specific kind of "risky" value such that the pattern can be properly supported, without any user intervention. This expands the range of accepted not-crashing-at-runtime code using swift concurrency.
Result: Instead of crashing, we now can support this pattern - which sometimes comes up unbeknownst to actual developers, because they may be using libraries which wrap them in task groups and whatnot.
Scope/Impact: Expands the range of not-crashing code using task-locals and task groups in combination. A specific pattern which was previously banned is now allowed.
Risk: Low, memory management is tricky but carefully maintained using task local allocations. Testing covers all scenarios which could have been affected.
Testing: CI testing, added more tests to cover specific scenarios. Removed tests are those which "expected a crash".
Review: @DougGregor
Original PR: #73978 including review-follow-up #74089
Radar: rdar://127156303