Skip to content
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
[#802](https://github.com/hynek/structlog/pull/802)


### Fixed

- `structlog.BytesLogger`, `structlog.PrintLogger`, and `structlog.WriteLogger` now hold *weak* references to the files they use for output.
This prevents their leakage in long-running processes that open many logfiles, such as task executors that create a per-task `BytesLogger` or `WriteLogger`.
[#807](https://github.com/hynek/structlog/pull/807)


### Changed

- `structlog.dev.ConsoleRenderer` does not warn anymore when the `exception` key has a rendered value despite having a fancy formatter configured.
Expand Down
5 changes: 4 additions & 1 deletion src/structlog/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import copy
import sys
import threading
import weakref

from pickle import PicklingError
from sys import stderr, stdout
from typing import IO, Any, BinaryIO, TextIO


WRITE_LOCKS: dict[IO[Any], threading.Lock] = {}
WRITE_LOCKS: weakref.WeakKeyDictionary[IO[Any], threading.Lock] = (
weakref.WeakKeyDictionary()
)


def _get_lock_for_file(file: IO[Any]) -> threading.Lock:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# repository for complete details.

import copy
import gc
import pickle

from io import BytesIO, StringIO
Expand All @@ -23,6 +24,35 @@
from .helpers import stdlib_log_methods


@pytest.mark.parametrize(
("logger_cls", "mode"),
[(PrintLogger, "w"), (WriteLogger, "w"), (BytesLogger, "wb")],
)
def test_write_locks_released_on_gc(logger_cls, mode, tmp_path):
"""
WRITE_LOCKS entry is removed automatically when the file object is GC'd.

Closing the file is not enough -- the entry persists until the file object
itself is collected.
"""
gc.collect()
size_before = len(WRITE_LOCKS)
f = (tmp_path / "test.log").open(mode)
logger = logger_cls(f)

assert len(WRITE_LOCKS) == size_before + 1

# close() alone does not remove the entry
f.close()

assert len(WRITE_LOCKS) == size_before + 1

del logger, f
gc.collect()

assert len(WRITE_LOCKS) == size_before


class TestLoggers:
"""
Tests common to the Print and WriteLoggers.
Expand Down