Skip to content

[Concurrency] Relax task-local mis-use prevention in task groups #73881

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

Closed

Conversation

ktoso
Copy link
Contributor

@ktoso ktoso commented May 24, 2024

We have a mechanism protecting task group child tasks from accessing values in "outer scope" if they are defined like this:

await withTaskGroup { group in 
  await $local.withValue(value) {  // push "value"
    group.addTask {} // would refer to "value" by "direct pointer" to parent task
  } // pop value ---- if we allowed the above "direct pointer" the child task now refers to trash memory
}  

So to prevent this, today, we crash eagerly when we detect this.

The problem:

We crash to aggresively, also falsely triggering on this:

await withTaskGroup { group in 
  await $local.withValue(value) {  // does crash, but shouldn't
  }
  group.addTask {} 
}  

This patch solves this behavior by more precise runtime detecting of the specific problem situation -- we now crash inside the addTask and NOT inside the withValue.

Future direction: We could start copying defensively when we notice the "bad situation" rather than just crash. @FranzBusch was arguing that behavior would be preferable -- because we cannot always control the shapes of APIs, and if someone were to offer a synchronous callback API and they had set a task local value inside a task group "by accident" without even knowing about it, today we would crash - causing weird non-local interactions.

I'll see if I can pull off the copying "only when we have to in the bad-bad and rare case".

resolves rdar://127156303

@ktoso ktoso changed the title [Concurrency] Fix missing return in legacy isSameExecutor mode detection [Concurrency] Relax task-local mis-use prevention in task groups May 24, 2024
@ktoso
Copy link
Contributor Author

ktoso commented May 24, 2024

@swift-ci please smoke test

@ktoso ktoso force-pushed the wip-fix-task-locals-in-groups-too-pedantic branch from a6859b4 to 54f5d50 Compare May 28, 2024 07:16
@ktoso
Copy link
Contributor Author

ktoso commented May 28, 2024

@swift-ci please smoke test

@ktoso ktoso force-pushed the wip-fix-task-locals-in-groups-too-pedantic branch from 54f5d50 to 3c46a56 Compare May 28, 2024 12:44
@ktoso
Copy link
Contributor Author

ktoso commented May 28, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented May 31, 2024

replaced by #73978

@ktoso ktoso closed this May 31, 2024
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.

1 participant