Skip to content

Conversation

@yurekami
Copy link
Contributor

Summary

  • Adds context manager support (__enter__/__exit__) to iovec and ioring classes for Pythonic resource management
  • Adds atexit handler to clean up orphaned symlinks on normal process exit
  • Adds global thread-safe symlink registry to track active symlinks
  • Adds explicit close() methods with idempotent behavior for manual cleanup

Related Issue

Fixes #334 - Shared memory leakage due to symlinks not being properly deleted

Changes

The iovec class creates symlinks in /3fs-virt/iovs/ that point to shared memory. Previously, cleanup only happened in __del__, which is unreliable in Python (not guaranteed to run, timing issues, exception handling problems).

This PR improves cleanup reliability by:

  1. Context manager support: Users can now use with make_iovec(...) as iov: for automatic cleanup
  2. Atexit handler: Registered at module load to clean up any remaining symlinks on exit
  3. Global registry: Thread-safe tracking of active symlinks for cleanup coordination
  4. Explicit close(): Idempotent method that can be called safely multiple times

Example usage:

# Preferred: using context manager (new)
with make_iovec(shm, mount_point) as iov:
    ior.prepare(iov[:], True, fd, offset)
    # ...
# symlink automatically cleaned up

# Alternative: manual management
iov = make_iovec(shm, mount_point)
try:
    # use iov
finally:
    iov.close()  # explicit cleanup

Test plan

  • Verify symlinks are cleaned up when using context manager
  • Verify symlinks are cleaned up on normal process exit via atexit
  • Verify symlinks are cleaned up when exception occurs in context
  • Verify thread-safety of global registry under concurrent access
  • Verify backward compatibility with existing code using __del__

🤖 Generated with Claude Code

Add context manager support and atexit handlers to iovec/ioring classes
to address symlink leakage issues reported in deepseek-ai#334.

Changes:
- Add global symlink registry with thread-safe lock for tracking
- Add atexit handler _cleanup_symlinks() for cleanup on normal exit
- Add context manager protocol (__enter__/__exit__) to iovec class
- Add context manager protocol to ioring class
- Add explicit close() methods with idempotent behavior
- Replace simple __del__ with close() call for safe cleanup

This ensures symlinks in /3fs-virt/iovs/ are cleaned up even when:
- Exceptions occur during I/O operations
- The process exits normally without explicit cleanup
- Users forget to manually unlink symlinks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shared memory leakage issue due to symlink to /dev/shm not deleted

1 participant