RFC: Add a lock to CuArray for improved thread safety #2026
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.
This PR adds a lock to
CuArray
so that we can protect against operations that mutate the array object. Note that this doesn't need to extend to the containedArrayStorage
, as that should already be threadsafe, as that's an immutable struct with the refcount being handled atomically.@ToucheSir encountered a case where multithreaded use of
unsafe_free!
, both manually and from finalizers, resulted in a UndefRefError accessing the context. It's not entirely clear to me whether that's an error accessing the thread-local context, or the array context, so @ToucheSir please post the full backtrace.Currently,
unsafe_free!
assumes that it doesn't need a lock, because (1) finalizers don't run concurrently to regular code, and (2)unsafe_free!
doesn't make sense to be called from multiple threads. It may make sense to protect the operation though, because technically it's legal to callunsafe_free!
multiple times.In addition, I suspect we'll need to protect other operations too (like
resize!
) so it probably doesn't hurt adding a lock anyway.cc @vchuravy