Skip to content

Conversation

@simonjbeaumont
Copy link
Contributor

Motivation

We were using a @TaskLocal static var to hold the FiniteFieldArithmeticContext (FFAC) on each of the curve types. The intention here was this would operate as a thread-local value, since task-locals behave like thread locals when the caller is not part of a task context. However, they still require binding, (i.e. the use of e.g. P256.$__ffac.withValue { ... }1, otherwise they just return the default value, which is globally shared. We were not doing that, nor do we have a sensible place where we could.

This causes crashes when using these values in a multithreaded environment since the same value will be used across threads.

Modifications

  • Use an explicit thread-local API from Foundation to store and read the per-curve FFAC.
  • Add a test that shows each thread gets the same value on each read, but distinct from all other threads.

Result

Fixes crash when using ECToolbox-based APIs in multithreaded code.

Footnotes

  1. https://developer.apple.com/documentation/swift/tasklocal#Using-task-local-values-outside-of-tasks

@simonjbeaumont simonjbeaumont enabled auto-merge (squash) August 8, 2025 13:38
@simonjbeaumont simonjbeaumont added the 🆕 semver/minor Adds new public API. label Aug 8, 2025
@simonjbeaumont simonjbeaumont disabled auto-merge August 8, 2025 13:57
@simonjbeaumont simonjbeaumont requested a review from Lukasa August 8, 2025 16:01
@simonjbeaumont simonjbeaumont merged commit 2347f20 into apple:main Aug 8, 2025
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants