Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- fix: close output suppression streams after use by @emme1t in #2427
- fix(ci): remove unsupported macOS 14 release runners by @abetlen in #2416

## [0.3.37]
Expand Down
79 changes: 26 additions & 53 deletions llama_cpp/_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import os
import sys

from contextlib import ExitStack
from typing import Any, Dict

# Avoid "LookupError: unknown encoding: ascii" when open() called in a destructor
outnull_file = open(os.devnull, "w")
errnull_file = open(os.devnull, "w")

STDOUT_FILENO = 1
STDERR_FILENO = 2

Expand All @@ -16,65 +12,42 @@ class suppress_stdout_stderr(object):
# this context manager inside of a __del__ method
sys = sys
os = os
_open = staticmethod(open)

def __init__(self, disable: bool = True):
self.disable = disable

# Oddly enough this works better than the contextlib version
def __enter__(self):
if self.disable:
return self

self.old_stdout_fileno_undup = STDOUT_FILENO
self.old_stderr_fileno_undup = STDERR_FILENO

self.old_stdout_fileno = self.os.dup(self.old_stdout_fileno_undup)
self.old_stderr_fileno = self.os.dup(self.old_stderr_fileno_undup)

self.old_stdout = self.sys.stdout
self.old_stderr = self.sys.stderr

# In Jupyter notebooks, ipykernel replaces sys.stdout/stderr with
# OutStream objects that hold their own copy of the original fd in
# _original_stdstream_copy. This bypasses our dup2 redirect, so we
# need to point that copy at the real fd temporarily.
# https://github.com/ipython/ipykernel/blob/912923542d55e6c80c0e7c1f94c648b52a225011/ipykernel/iostream.py#L618-L622
self._saved_stdout_copy = getattr(
self.sys.stdout, "_original_stdstream_copy", None
)
self._saved_stderr_copy = getattr(
self.sys.stderr, "_original_stdstream_copy", None
)
if self._saved_stdout_copy is not None:
self.sys.stdout._original_stdstream_copy = self.old_stdout_fileno_undup
if self._saved_stderr_copy is not None:
self.sys.stderr._original_stdstream_copy = self.old_stderr_fileno_undup

self.os.dup2(outnull_file.fileno(), self.old_stdout_fileno_undup)
self.os.dup2(errnull_file.fileno(), self.old_stderr_fileno_undup)

self.sys.stdout = outnull_file
self.sys.stderr = errnull_file
with ExitStack() as redirects:
# An explicit encoding also works when called during interpreter shutdown.
null_stream = redirects.enter_context(
self._open(self.os.devnull, "w", encoding="utf-8")
)
for name, fd in (("stdout", STDOUT_FILENO), ("stderr", STDERR_FILENO)):
original_fd = self.os.dup(fd)
redirects.callback(self.os.close, original_fd)
self.os.dup2(null_stream.fileno(), fd)
redirects.callback(self.os.dup2, original_fd, fd)

stream = getattr(self.sys, name)
# ipykernel's OutStream also writes through its own saved fd.
saved_copy = getattr(stream, "_original_stdstream_copy", None)
if saved_copy is not None:
stream._original_stdstream_copy = fd
redirects.callback(
setattr, stream, "_original_stdstream_copy", saved_copy
)
setattr(self.sys, name, null_stream)
redirects.callback(setattr, self.sys, name, stream)
self._redirects = redirects.pop_all()
return self

def __exit__(self, *_):
if self.disable:
return

self.sys.stdout = self.old_stdout
self.sys.stderr = self.old_stderr

self.os.dup2(self.old_stdout_fileno, self.old_stdout_fileno_undup)
self.os.dup2(self.old_stderr_fileno, self.old_stderr_fileno_undup)

self.os.close(self.old_stdout_fileno)
self.os.close(self.old_stderr_fileno)

# Restore ipykernel's OutStream fd copies
if self._saved_stdout_copy is not None:
self.sys.stdout._original_stdstream_copy = self._saved_stdout_copy
if self._saved_stderr_copy is not None:
self.sys.stderr._original_stdstream_copy = self._saved_stderr_copy
if not self.disable:
self._redirects.close()


class MetaSingleton(type):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_suppress_stdout_stderr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import subprocess
import sys
from textwrap import dedent


def test_suppression_releases_null_streams_at_shutdown():
script = dedent(
"""
import gc
import os
import sys
from llama_cpp._utils import suppress_stdout_stderr

with suppress_stdout_stderr(disable=False):
print("hidden", flush=True)
with suppress_stdout_stderr(disable=False):
os.write(2, b"hidden")
os.write(1, b"hidden")

print("restored", flush=True)
print("restored", file=sys.stderr, flush=True)

class Cleanup:
def __del__(self):
with suppress_stdout_stderr(disable=False):
os.write(2, b"hidden during shutdown")
print("cleanup finished", file=sys.stderr, flush=True)

gc.disable()
cleanup = Cleanup()
cleanup.cycle = cleanup
"""
)
result = subprocess.run(
[sys.executable, "-Walways::ResourceWarning", "-c", script],
capture_output=True,
text=True,
check=True,
)
assert result.stdout == "restored\n"
assert result.stderr == "restored\ncleanup finished\n"