-
Notifications
You must be signed in to change notification settings - Fork 1
perf: reuse logging locks and cache source paths #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
codeforester
merged 33 commits into
main
from
enhancement/381-20261003-perf-lifecycle-logging-costs-107-us-record-about-20x-stdlib
Oct 5, 2026
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
30e9e71
ci: cover all consumer fixtures in style and strict typing gates
codeforester 710208b
ci: keep Markdown examples under the documentation gate
codeforester 8bb0562
fix: preserve consumer logging ownership and routing
codeforester 6556889
perf: reuse logging locks and cache source paths
codeforester a166e45
perf: cache second-precision human log timestamps
codeforester d352e43
Merge remote-tracking branch 'origin/main' into ci/393-20261003-ci-ru…
codeforester 6ee5cc9
Merge branch 'ci/393-20261003-ci-ruff-check-and-mypy-do-not-cover-the…
codeforester f9787b1
Merge branch 'bug/387-20261003-bug-configure-logger-closes-consumer-o…
codeforester 6e4394f
fix: avoid racing Windows lock-sidecar initialization
codeforester 73770a5
ci: separate sustained persistence cost from hosted filesystem tails
codeforester bdf26d9
Merge branch 'ci/393-20261003-ci-ruff-check-and-mypy-do-not-cover-the…
codeforester c3eb5dc
Merge branch 'bug/387-20261003-bug-configure-logger-closes-consumer-o…
codeforester 8ca0027
docs: regenerate public API signatures for configuration trust options
codeforester 9191a7e
Merge branch 'bug/387-20261003-bug-configure-logger-closes-consumer-o…
codeforester 857e52e
Merge main for branch maintenance (#426)
codeforester b48e444
Merge updated PR #414 for branch maintenance (#426)
codeforester 66ce268
Merge updated PR #415 for branch maintenance (#426)
codeforester 9db3464
fix: preserve logger routing and levels
codeforester fbcc75c
fix: harden logging caches and sidecar locks
codeforester 72336ec
ci: expose consumer typing source coverage
codeforester ae935d1
fix: serialize logger reconfiguration
codeforester 990cb66
style: format typing gate test
codeforester 50b0695
style: format sidecar lock condition
codeforester 0f35aae
Merge remote-tracking branch 'origin/main' into ci/393-20261003-ci-ru…
codeforester 6efcb60
test: cover debug logging with consumer handlers
codeforester 436807b
test: cover concurrent and inherited logging state
codeforester 3f58457
Merge remote-tracking branch 'origin/ci/393-20261003-ci-ruff-check-an…
codeforester 92cad10
Merge remote-tracking branch 'origin/bug/387-20261003-bug-configure-l…
codeforester 4f75f72
fix: reopen replaced logging sidecars
codeforester 0f8c319
Merge remote-tracking branch 'origin/main' into enhancement/381-20261…
codeforester 8a2ef79
style: format sidecar recovery branch
codeforester c4e4b9b
test: skip POSIX sidecar deletion on Windows
codeforester 250bbe4
Merge remote-tracking branch 'origin/main' into enhancement/381-20261…
codeforester File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import io | ||
| import logging | ||
| import os | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| import base_cli | ||
| import base_cli.logging as module | ||
| import pytest | ||
| from base_cli.testing import invoke | ||
|
|
||
|
|
||
| def test_sidecar_is_opened_once_and_closed(tmp_path: Path) -> None: | ||
| handler = module.SecureLogFileHandler(tmp_path / "run.log") | ||
| record = logging.LogRecord("test", logging.INFO, __file__, 1, "message", (), None) | ||
| with patch.object(module, "_open_log_lock", wraps=module._open_log_lock) as opened: | ||
| handler.emit(record) | ||
| handler.emit(record) | ||
| assert opened.call_count == 1 | ||
| stream = handler._lock_stream | ||
| handler.close() | ||
| assert stream.closed | ||
|
|
||
|
|
||
| @pytest.mark.skipif(os.name == "nt", reason="Windows does not permit unlinking an open sidecar") | ||
| def test_deleted_sidecar_reopens_and_preserves_later_records(tmp_path: Path) -> None: | ||
| handler = module.SecureLogFileHandler(tmp_path / "run.log") | ||
| try: | ||
| handler.emit(logging.LogRecord("test", logging.INFO, __file__, 1, "before", (), None)) | ||
| handler._lock_path.unlink() | ||
| for message in ("after-0", "after-1", "after-2"): | ||
| handler.emit(logging.LogRecord("test", logging.INFO, __file__, 1, message, (), None)) | ||
| finally: | ||
| handler.close() | ||
|
|
||
| log_text = (tmp_path / "run.log").read_text(encoding="utf-8") | ||
| assert log_text.splitlines() == ["before", "after-0", "after-1", "after-2"] | ||
|
|
||
|
|
||
| def test_shared_formatter_handles_concurrent_user_and_file_logging(tmp_path: Path) -> None: | ||
| user_stream = io.StringIO() | ||
| formatter = module.CliFormatter() | ||
| logger = base_cli.configure_logger( | ||
| "shared-formatter-race", | ||
| tmp_path / "run.log", | ||
| debug=True, | ||
| stream=user_stream, | ||
| formatter=formatter, | ||
| propagate=False, | ||
| ) | ||
| try: | ||
| with ThreadPoolExecutor(max_workers=8) as executor: | ||
| list(executor.map(logger.info, (f"message-{index}" for index in range(64)))) | ||
| assert user_stream.getvalue().count("message-") == 64 | ||
| assert (tmp_path / "run.log").read_text(encoding="utf-8").count("message-") == 64 | ||
| finally: | ||
| for handler in list(logger.handlers): | ||
| handler.close() | ||
| logger.removeHandler(handler) | ||
|
|
||
|
|
||
| def test_forked_handler_recovers_after_inherited_lock_close_failure(tmp_path: Path) -> None: | ||
| handler = module.SecureLogFileHandler(tmp_path / "run.log") | ||
| record = logging.LogRecord("test", logging.INFO, __file__, 1, "message", (), None) | ||
|
|
||
| class FailingStream: | ||
| def close(self) -> None: | ||
| raise OSError("already closed") | ||
|
|
||
| handler._lock_stream = FailingStream() # type: ignore[assignment] | ||
| handler._lock_pid = os.getpid() - 1 | ||
| try: | ||
| handler.emit(record) | ||
| assert handler._lock_pid == os.getpid() | ||
| assert handler._lock_stream is not None | ||
| finally: | ||
| handler.close() | ||
|
|
||
|
|
||
| def test_recursion_errors_follow_stdlib_handler_contract(tmp_path: Path) -> None: | ||
| handler = module.SecureLogFileHandler(tmp_path / "run.log") | ||
| record = logging.LogRecord("test", logging.INFO, __file__, 1, "message", (), None) | ||
| try: | ||
| with patch.object(logging.FileHandler, "emit", side_effect=RecursionError("recursive")): | ||
| with pytest.raises(RecursionError, match="recursive"): | ||
| handler.emit(record) | ||
| finally: | ||
| handler.close() | ||
|
|
||
|
|
||
| def test_logging_lock_failures_do_not_fail_command(tmp_path: Path) -> None: | ||
| app = base_cli.App(name="logging-io-failure") | ||
|
|
||
| @app.command() | ||
| def main(ctx: base_cli.Context) -> None: | ||
| for failure in (PermissionError("unwritable log directory"), OSError("volume full")): | ||
| with patch.object(module, "_lock_log_stream", side_effect=failure): | ||
| ctx.log.info("still completes") | ||
| ctx.log.info("recovers") | ||
|
|
||
| result = invoke(app, [], home=tmp_path) | ||
| assert result.exit_code == 0 | ||
| assert "Logging error" in result.stderr | ||
|
|
||
|
|
||
| def test_formatter_repeated_paths_do_not_resolve_again(tmp_path: Path) -> None: | ||
| app = base_cli.App(name="cached-log-source") | ||
|
|
||
| @app.command() | ||
| def main(ctx: base_cli.Context) -> None: | ||
| ctx.log.info("warm cache") | ||
| with patch.object(Path, "resolve", side_effect=AssertionError("unexpected resolution")): | ||
| ctx.log.info("cached source") | ||
|
|
||
| assert invoke(app, [], home=tmp_path).exit_code == 0 | ||
|
|
||
|
|
||
| def test_timestamp_cache_preserves_seconds_and_timezone_format() -> None: | ||
| for use_utc in (False, True): | ||
| formatter = module.CliFormatter(use_utc=use_utc) | ||
| reference = logging.Formatter(datefmt=formatter.datefmt) | ||
| reference.converter = formatter.converter | ||
| record = logging.LogRecord("test", logging.INFO, __file__, 1, "message", (), None) | ||
| for created in (1000.1, 1000.9, 1001.0, 1002.3): | ||
| record.created = created | ||
| assert formatter.formatTime(record, formatter.datefmt) == reference.formatTime(record, formatter.datefmt) | ||
|
|
||
|
|
||
| def test_opening_lock_does_not_write_an_unlocked_sentinel(tmp_path: Path) -> None: | ||
| path = tmp_path / "append.lock" | ||
| with module._open_log_lock(path) as stream: | ||
| module._lock_log_stream(stream) | ||
| try: | ||
| assert path.stat().st_size == 0 | ||
| finally: | ||
| module._unlock_log_stream(stream) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.