Skip to content

fix: close output suppression streams after use - #2427

Open
emme1t wants to merge 2 commits into
abetlen:mainfrom
emme1t:fix/close-suppression-streams-1828
Open

emme1t wants to merge 2 commits into
abetlen:mainfrom
emme1t:fix/close-suppression-streams-1828

Conversation

@emme1t

@emme1t emme1t commented Nov 21, 2026

Copy link
Copy Markdown

Closes #1828.

Importing llama_cpp opens two null-device streams. With Python resource warnings enabled, exiting the interpreter reports both streams as unclosed. I reproduced this on Windows with just import llama_cpp.

Open the null device when a suppression scope begins and close it when the scope ends. Python streams, native file descriptors and ipykernel stream copies are restored on exit. An explicit encoding and a saved reference to open let a destructor use the same scope during interpreter shutdown.

The new subprocess test exercises nested suppression, writes to native file descriptors, restoration of stdout and stderr, and a destructor that suppresses output during shutdown. On the original code the subprocess emitted two resource warnings.

On Windows with Python 3.12, the complete CPU test suite passed (27 tests, two inference threads) with the pinned llama.cpp build and cached GGUF models. Ruff lint and formatting passed. I also exercised shutdown suppression on CPython 3.11 through 3.14.

This branch has not been deployed

No deployments
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.

sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='nul' mode='w' encoding='cp932'>

1 participant