[PR #367] fix(io): Add robust symlink cleanup to prevent resource leakage #391

Open
opened 2026-10-26 09:49:44 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/deepseek-ai/3FS/pull/367
Author: @yurekami
Created: 12/25/2025
Status: 🔄 Open

Base: main ← Head: fix/iovec-symlink-cleanup


📝 Commits (1)

  • b250b38 fix(io): Add robust symlink cleanup to prevent resource leakage

📊 Changes

1 file changed (+106 additions, -1 deletions)

View changed files

📝 hf3fs_fuse/io.py (+106 -1)

📄 Description

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

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/deepseek-ai/3FS/pull/367 **Author:** [@yurekami](https://github.com/yurekami) **Created:** 12/25/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/iovec-symlink-cleanup` --- ### 📝 Commits (1) - [`b250b38`](https://github.com/deepseek-ai/3FS/commit/b250b38282e544ffba7b3800a9ec9238baacd01d) fix(io): Add robust symlink cleanup to prevent resource leakage ### 📊 Changes **1 file changed** (+106 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `hf3fs_fuse/io.py` (+106 -1) </details> ### 📄 Description ## 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: ```python # 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](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/3FS#391
No description provided.