Skip to content

Close owned C streams after trie file I/O - #112

Open
omrzk wants to merge 1 commit into
pytries:masterfrom
omrzk:fix/close-owned-file-streams
Open

omrzk wants to merge 1 commit into
pytries:masterfrom
omrzk:fix/close-owned-file-streams

Conversation

@omrzk

@omrzk omrzk commented Nov 17, 2026

Copy link
Copy Markdown

Each trie read or write creates a C FILE stream over the caller's descriptor and leaves it open. Repeated save/load and pickle operations retain the stream allocations and read buffers. Writes also ignore buffered flush failures, so BaseTrie.save('/dev/full') can report success.

Give each C stream a duplicated descriptor and close it on success and failure, preserving the caller's file ownership and read position. Report failures when closing/flushing a write, and release a loaded trie if restoring the caller's position raises.

Validation: update_c.sh and the repository's tox -e py pass (44 tests, Python 3.10). The new BaseTrie flush-error case fails on current master; the other 30 file/trie tests pass there. A native stream-counter probe through 3,000 actual save/load cycles observes 6,000 retained streams on master and zero after this fix; measured RSS growth falls from about 26 MiB to less than 1 MiB. Another 521 lifecycle assertions cover caller file/position retention, descriptor reuse, interleaved records, BaseTrie/Trie pickling, malformed reads, wrong file modes, flush failures and caller seek errors, with no descriptor or stream retention.

The full 44-test suite and 521 lifecycle assertions also pass on Python 3.14. Tests ran on Linux. Native Windows/macOS builds were not run. Reading after a Python file has prefetched a preceding record into its buffer still fails on both master and this patch; that existing buffering limitation is outside this change.

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.

1 participant