-
Notifications
You must be signed in to change notification settings - Fork 70
fix(cuda): sync memory subtree streams before drop #2175
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses a race condition in CUDA memory management where panic-induced early drops could cause initial memory buffers to be freed while async merkle tree build operations were still executing on separate streams, resulting in a deadlock.
Key Changes:
- Implements explicit Drop trait for PersistentMemoryInventoryGPU to enforce proper cleanup order
- Ensures merkle subtree streams are synchronized before dropping initial memory buffers
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Commit: c723cf7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean if it works then I guess
| fn drop(&mut self) { | ||
| // Drop merkle subtrees first so their individual streams synchronize before dropping the | ||
| // initial memory buffers. This prevents buffers from dropping before build_async completes. | ||
| self.merkle_tree.drop_subtrees(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IS this enough? Strange. I don't see a sync in MemoryMerkleSubTree drop. There is no drop impl there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just the CudaStream drop implementation. When you remove a subtree, the subtree drops, which drops the stream, which forces a sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, nice
In some edge case where right after we start
build_asyncon the memory merkle subtrees, if the program panics, then the order of drop could be that we drop theinitial_memorybuffers on the default stream first, while thebuild_asynckernels are still running and using those buffers. This leads to a deadloop. I fixed it by just forcing the drop to drop subtrees first (which should sync their special streams) before droppinginitial_memory.compare: https://github.com/axiom-crypto/openvm-reth-benchmark/actions/runs/18733111153