-
Notifications
You must be signed in to change notification settings - Fork 134
[Review][Java] Fix random segabort/segfault/double free problems #1045
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
/ok to test b26ff62 |
Correct me if I'm wrong, but I thought the code flow was more like: create(void** ptrPtr) {
*ptrPtr = new Something;
}
void* ptr; // (Automatically) allocate space for a pointer.
create(&ptr); Edit: I think I might be nitpicking. I'll examine the crux of your fix shortly. |
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/BruteForceIndexImpl.java
Show resolved
Hide resolved
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/BruteForceIndexImpl.java
Outdated
Show resolved
Hide resolved
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/CuVSResourcesImpl.java
Show resolved
Hide resolved
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/CuVSResourcesImpl.java
Outdated
Show resolved
Hide resolved
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/BruteForceIndexImpl.java
Show resolved
Hide resolved
You are absolutely correct, I was writing "C the way we can use in Java": if we cannot generate a "pointer" (a MemorySegment) referencing a stack variable,
or, equivalent in Java
My point is: the original code was missing the indirection and dereference. |
Excellent catch, thanks a lot @ldematte.
We can use this issue, if needed: #1037 |
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/BruteForceIndexImpl.java
Show resolved
Hide resolved
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/BruteForceIndexImpl.java
Show resolved
Hide resolved
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'm generally positive on this change.
There are a couple of minor quibbles, and some clarifying questions.
/ok to test 0fb438e |
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.
LGTM. Thanks for fixing this intermittent failure.
/ok to test a87d96e |
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.
LGTM! Good catch, @ldematte. 👏
/merge |
This PR tidies up the lifecycle of `MemorySegment`s by using specific confined `Arena`s where possible, and specific index-bound `Arena`s where the lifetime needs to be bound to the lifetime of an index. It addresses all remaining usages of `CuVSResources#arena` after #1024 and #1045; as such, we can remove `CuVSResources#arena` completely, and force native memory usages to be tracked and dealt with specifically. This would towards the goal of #1037 Authors: - Lorenzo Dematté (https://github.com/ldematte) - MithunR (https://github.com/mythrocks) - Ben Frederickson (https://github.com/benfred) Approvers: - Chris Hegarty (https://github.com/ChrisHegarty) - MithunR (https://github.com/mythrocks) URL: #1069
This PR tidies up the lifecycle of `MemorySegment`s by using specific confined `Arena`s where possible, and specific index-bound `Arena`s where the lifetime needs to be bound to the lifetime of an index. It addresses all remaining usages of `CuVSResources#arena` after rapidsai#1024 and rapidsai#1045; as such, we can remove `CuVSResources#arena` completely, and force native memory usages to be tracked and dealt with specifically. This would towards the goal of rapidsai#1037 Authors: - Lorenzo Dematté (https://github.com/ldematte) - MithunR (https://github.com/mythrocks) - Ben Frederickson (https://github.com/benfred) Approvers: - Chris Hegarty (https://github.com/ChrisHegarty) - MithunR (https://github.com/mythrocks) URL: rapidsai#1069
The cuVS C API we are leveraging uses a common C pattern for "object creation": you pass down a pointer, and that gets populated. Often, this is a **, and memory gets allocated on the other side:
In Panama, this means
create
Existing code sometimes skipped the extraction bit (the dereference), and/or created the structure directly instead of using the C
create
functions; this lead to some segfaults (addressing the wrong memory/wrong size) and to some double frees (the JVM freed MemorySegments we allocated via an arena, but we also called thedestroy
functions, so the C side tried to free them too).Plus, this PR tries to reduce the lifetime of some allocations, avoiding to use the
CuVSResources
arena when it is clearly not needed -- but I did not want to expand the scope too much, so I just did the more obvious ones. Optimizing memory allocation is still a WIP, especially on the search side, but this I think will deserve its own PR.These changes makes the test suite pass reliably - I have not seen a segmentation fault over hundreds of executions locally, and lowers the native memory occupied a little.