Commit b01a091
fix(io): close aio_fd in FastFileWriter._fini to prevent fd leak (#8005)
Fixes #8003
## Summary
`FastFileWriter._fini()` overwrote `self._aio_fd = INVALID_FD` without
calling `os.close()`, leaking one fd per save. With unlink-based
checkpoint rotation this stranded the unlinked inode in the ext4
orphan list, fs blocks were never reclaimed, and long-running save
loops hit ENOSPC at iter ~60 (60 GB/iter on a 4 TB partition).
This PR adds explicit `os.fsync()` + `os.close()` in `_fini()` and a
regression test that asserts no `/proc/self/fd` entry points at a
deleted file after a save+close+unlink cycle.
## Verification
- 20-iteration repro of `save() / close() / unlink()` leaked 20 fds
before the fix, 0 after.
- 700-iter / 42 TB / 60 h endurance run on ext4/NVMe: `df_used`
stable at 736 GB (drift +281 MB / 697 rotations) with the fix;
same workload hit ENOSPC at iter ~60 without it.
- Performance impact: ~5% wall-time overhead from the added
`os.fsync()` at ~10 GB/s peak.
## Test plan
- [x] New regression test
`tests/unit/ops/aio/test_fast_file_writer_fd_close.py` verifies fd
cleanup after a single save and after 5/20-iter rotation loops via
`/proc/self/fd` scoped to `tmp_path`.
- [x] Gated on `async_io` compatibility, Linux, and CUDA accelerator
so unsupported CI matrix entries skip cleanly.
- [x] Confirmed test FAILS without this PR's `_fini()` change and
PASSES with it.
- [x] `pre-commit run --files <changed files>` clean.
## Notes
- The `__del__` assertion `assert self._aio_fd == INVALID_FD` passes
even with the bug because it checks the Python attribute that
`_fini` itself sets. The new test checks OS-level state via
`/proc/self/fd`.
- `os.fsync()` is included for post-close durability — required for
correctness on the unaligned-tail path that re-opens the file as
buffered I/O. If maintainers prefer to drop it for performance,
removing only the `os.fsync(...)` line still fixes the leak.
Happy to adjust shape, naming, or test placement to fit project
conventions. Thanks for the review.
Signed-off-by: jg-heo <csjg.heo@gmail.com>
Co-authored-by: Masahiro Tanaka <81312776+tohtana@users.noreply.github.com>1 parent 66af8f0 commit b01a091
2 files changed
Lines changed: 113 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
117 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
118 | 124 | | |
119 | 125 | | |
120 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
0 commit comments