Use C++ shared_ptr for IPC file descriptor cleanup#1832
Merged
Andy-Jost merged 4 commits intoNVIDIA:mainfrom Mar 30, 2026
Merged
Use C++ shared_ptr for IPC file descriptor cleanup#1832Andy-Jost merged 4 commits intoNVIDIA:mainfrom
Andy-Jost merged 4 commits intoNVIDIA:mainfrom
Conversation
Replace Python-level os.close() in IPCAllocationHandle with a C++ shared_ptr custom deleter that calls POSIX close() directly, avoiding unraisable exception errors during late interpreter shutdown. Made-with: Cursor
|
Contributor
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
Made-with: Cursor
Made-with: Cursor
create_fd_handle and create_fd_handle_ref throw std::runtime_error on Windows since POSIX file descriptors are Linux-only. Made-with: Cursor
rwgk
approved these changes
Mar 30, 2026
Collaborator
rwgk
left a comment
There was a problem hiding this comment.
Looks good to me (manual review + Cursor).
Cursor GPT-5.3 Codex High flagged: I don’t see a dedicated automated test for the specific regression (“late interpreter shutdown / unraisable os.close path”).
I think that's fine to ignore. I've seen the "Error in sys.excepthook" messages many times, we definitely have plenty of indirect coverage.
cpcloud
approved these changes
Mar 30, 2026
Contributor
cpcloud
left a comment
There was a problem hiding this comment.
The C++ fd-handle cleanup approach looks correct and preserves the IPC ownership model while avoiding late-interpreter-shutdown issues.
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
When a
DeviceMemoryResourceis destroyed after Python finalization has begun, its IPC allocation handle (a POSIX file descriptor on Linux) must still be closed. Previously,IPCAllocationHandle.__dealloc__calledos.close()to do this, but during late interpreter shutdown theosmodule may already be partially torn down, producing cryptic "Error in sys.excepthook" messages about unraisable exceptions.This PR fixes the problem by managing the file descriptor through a C++
shared_ptrwith a custom deleter that calls POSIXclose()directly, bypassing Python entirely. The newFileDescriptorHandlefollows the same pattern as other handle types in theresource_handlesmodule.Changes
resource_handles.hpp/cpp— addFileDescriptorHandletypedef,create_fd_handle(owning, calls::close),create_fd_handle_ref(non-owning),as_intptrandas_pyoverloads_resource_handles.pxd/pyx— declare and expose the new handle type and factory functions_ipc.pxd/pyx— replaceint _handlewithFileDescriptorHandle _h_fd;close()becomesreset(); remove__dealloc__Test Plan
sys.excepthookerrors after test runMade with Cursor