Skip to content

[6.0][Concurrency] Defensive copying of tasks locals in TaskGroups (rather than crashing) #74038

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 May 31, 2024

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

@ktoso ktoso requested a review from a team as a code owner May 31, 2024 01:52
@ktoso ktoso requested a review from DougGregor May 31, 2024 01:52
@ktoso
Copy link
Contributor Author

ktoso commented May 31, 2024

@swift-ci please test

@ktoso
Copy link
Contributor Author

ktoso commented Jun 3, 2024

Minor follow up from main review before merging this ⌛

ktoso added 2 commits June 3, 2024 13:56
…roups

fix C++11 warning about char* conversion

remove free(message) since message is not dynamically allocated anymore

fix embedded swift error reporting task-local abuse in group

cleanup: remove unrecognized clang ide diagnostic options

These are something CLion understands and suggests, but yeah let's avoid
having warnings in the build.

[Concurrency] Implement only-when-necessary defensive TL copying in in task groups

[Concurrency] Correct handling of mixed nested values

minor cleanups and renames

[Concurrency] Simplify task locals destroy; add more tests for locals

add extra test
@ktoso ktoso force-pushed the pick-wip-task-locals-defensive-copying-rather-than-crashing branch from d202bb9 to 2686ed4 Compare June 3, 2024 04:57
@ktoso
Copy link
Contributor Author

ktoso commented Jun 3, 2024

@swift-ci please test

@DougGregor DougGregor merged commit d138ca4 into swiftlang:release/6.0 Jun 3, 2024
5 checks passed
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.

2 participants