fix(cutile): synchronize stream in to_host_vec before dropping owned tensor - #278
Open
emersonbusson wants to merge 1 commit into
Open
emersonbusson wants to merge 1 commit into
emersonbusson wants to merge 1 commit into
Conversation
…ing owned tensor (NVlabs#252)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #252.
Synchronizes the execution stream in
CopyDeviceToHostVec::executebefore droppingself.tensor, eliminating a stream-ordered race and use-after-free between the in-flight DtoH memcpy and asynchronous deallocation on thedeallocator_stream.Root Cause
When copying an owned tensor to host memory via
tensor.to_host_vec().sync_on(&stream):CopyDeviceToHostVecholds ownership of the source tensor viaself.tensor: Arc<Tensor<T>>.CopyDeviceToHostVec::execute,memcpy_dtoh_asyncis enqueued onctx.get_cuda_stream().execute(self, &ctx)consumesselfby value, meaningself(and its innerself.tensor) is dropped at the return ofexecute.DeviceBuffer::drop, which immediately enqueuescuMemFreeAsyncon the thread'sdeallocator_stream.deallocator_streamdoes not establish an event wait onctx.get_cuda_stream(), the free operation ondeallocator_streamraces with the in-flight copy onctx.get_cuda_stream(), triggering a stream-ordered use-after-free flagged by Compute-Sanitizer (as reported in [BUG]: Possible use after free due to deallocation being queued on a different stream beforesync_onis called #252).Fix
By explicitly calling
ctx.get_cuda_stream().synchronize()insideCopyDeviceToHostVec::executebeforeself.tensoris dropped:ctx.get_cuda_stream()is guaranteed to be 100% complete before any deallocation can be submitted to thedeallocator_stream.SAFETY: ... returns only once the copy has completed, so all size elements are initialized here) holds deterministically across all stream configurations.Validation
to_host_vec_owned_sync_on_stream_orderincutile/tests/gpu/tensor.rsmatching the reproduction snippet from [BUG]: Possible use after free due to deallocation being queued on a different stream beforesync_onis called #252.