-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(logging): isolate Windows process log files #2542
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
Open
lihailong00
wants to merge
1
commit into
MoonshotAI:main
Choose a base branch
from
lihailong00:codex/issue-2348-windows-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,85 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import subprocess | ||
| import sys | ||
| import time | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from kimi_cli.utils import logging as logging_utils | ||
|
|
||
|
|
||
| def test_get_log_file_path_keeps_shared_log_off_windows(monkeypatch, tmp_path: Path): | ||
| monkeypatch.setenv("KIMI_SHARE_DIR", str(tmp_path)) | ||
| monkeypatch.setattr(logging_utils.sys, "platform", "linux") | ||
|
|
||
| assert logging_utils.get_log_file_path() == tmp_path / "logs" / "kimi.log" | ||
|
|
||
|
|
||
| def test_get_log_file_path_uses_process_log_on_windows(monkeypatch, tmp_path: Path): | ||
| monkeypatch.setenv("KIMI_SHARE_DIR", str(tmp_path)) | ||
| monkeypatch.setattr(logging_utils.sys, "platform", "win32") | ||
| monkeypatch.setattr(logging_utils.os, "getpid", lambda: 12345) | ||
|
|
||
| assert logging_utils.get_log_file_path() == tmp_path / "logs" / "kimi.12345.log" | ||
|
|
||
|
|
||
| @pytest.mark.skipif(sys.platform != "win32", reason="Windows file locking regression") | ||
| def test_windows_processes_can_rotate_logs_concurrently(tmp_path: Path): | ||
| """Exercise Loguru's real Windows file handles in separate processes.""" | ||
| ready = tmp_path / "ready" | ||
| release = tmp_path / "release" | ||
| env = os.environ.copy() | ||
| env["KIMI_SHARE_DIR"] = str(tmp_path) | ||
|
|
||
| holder_code = """ | ||
| import os, time | ||
| from loguru import logger | ||
| from kimi_cli.utils.logging import get_log_file_path | ||
| logger.remove() | ||
| logger.add(get_log_file_path(), rotation='1 B') | ||
| logger.info('holder') | ||
| open(os.environ['READY'], 'w').close() | ||
| while not os.path.exists(os.environ['RELEASE']): | ||
| time.sleep(0.01) | ||
| """ | ||
| env["READY"] = str(ready) | ||
| env["RELEASE"] = str(release) | ||
| holder = subprocess.Popen( | ||
| [sys.executable, "-c", holder_code], | ||
| env=env, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| ) | ||
| try: | ||
| for _ in range(500): | ||
| if ready.exists(): | ||
| break | ||
| if holder.poll() is not None: | ||
| break | ||
| time.sleep(0.01) | ||
| assert ready.exists(), holder.communicate(timeout=1)[1] | ||
|
|
||
| writer_code = """ | ||
| from loguru import logger | ||
| from kimi_cli.utils.logging import get_log_file_path | ||
| logger.remove() | ||
| logger.add(get_log_file_path(), rotation='1 B') | ||
| logger.info('writer') | ||
| """ | ||
| writer = subprocess.run( | ||
| [sys.executable, "-c", writer_code], | ||
| env=env, | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=10, | ||
| ) | ||
| assert writer.returncode == 0 | ||
| assert "PermissionError" not in writer.stderr | ||
| assert len(list((tmp_path / "logs").glob("kimi.*.log"))) >= 2 | ||
| finally: | ||
| release.touch() | ||
| holder.communicate(timeout=10) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Old Windows log files are never deleted, causing disk usage to grow forever
On Windows each run writes to a new per-process log file (
kimi.{os.getpid()}.logatsrc/kimi_cli/utils/logging.py:25), but Loguru'sretention="10 days"only removes rotated files that match the current process's own filename, so log files from previously exited processes are never cleaned up.Impact: On Windows the logs directory grows without bound across CLI invocations, since the retention policy no longer bounds the total set of log files.
Why retention no longer bounds the log directory on Windows
Loguru's retention derives its cleanup glob from the sink's own filename. For a sink named
kimi.<pid>.log, the retention pattern only matches files beginning withkimi.<pid>— it cannot seekimi.<other-pid>.logfiles created by other/previous processes. Before this change all processes sharedkimi.log, soretention="10 days"(configured atsrc/kimi_cli/app.py:74) cleaned all rotated logs. Now every distinct PID leaves behind its own files that no process will ever purge. The export path (src/kimi_cli/cli/export.py:130-168) only reads logs for bundling; it does not delete them, so there is no other mechanism bounding disk usage.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.