Skip to content

docs: warn that preparation is not thread-safe #801

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

Merged
merged 3 commits into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ op(f, prep, backend, x, [t]) # fast because it skips preparation

!!! warning
The `prep` object is the last argument before `backend` and it is always mutated, regardless of the bang `!` in the operator name.
As a consequence preparation is **not thread safe** and sharing `prep` object between threads may lead to undefined behavior. If you need to run differentiation concurrently, prepare separate `prep` objects for each thread.

### Reusing preparation

Expand Down
2 changes: 2 additions & 0 deletions DifferentiationInterface/src/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function docstring_prepare(operator; samepoint=false, inplace=false)
The preparation result `prep` is only reusable as long as the arguments to `$operator` do not change type or size, and the function and backend themselves are not modified.
Otherwise, preparation becomes invalid and you need to run it again.
In some settings, invalid preparations may still give correct results (e.g. for backends that require no preparation), but this is not a semantic guarantee and should not be relied upon.
!!! danger
The preparation result `prep` is **not thread safe**. Sharing it between threads may lead to undefined behavior. If you need to run differentiations concurrently, prepare separate `prep` objects for each thread.

When `strict=Val(true)` (the default), type checking is enforced between preparation and execution (but size checking is left to the user).
While your code may work for different types by setting `strict=Val(false)`, this is not guaranteed by the API and can break without warning.
Expand Down
Loading