fix(cli): set SPT_NOENV only on Linux
SPT_NOENV only keeps the title out of /proc/PID/environ on Linux. On macOS it confines the title to the argv area, where setproctitle drops the last byte, and its import-time getproctitle() then fails to decode a command line ending in a multibyte character, so every such CLI call crashed. Fixes #6231
This commit is contained in:
1 parent
12e869d047
commit
a148eabc9f
2 files changed
+29
-4
No files matched your search
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
@@ -11,9 +12,8 @@ _ROLES: Final = {"agent", "gateway", "webui"}
|
||||
|
||||
|
||||
def _set_process_title(title: str) -> None:
|
||||
# Process titles are short; do not trade Linux /proc environment visibility for
|
||||
# extra title storage. setproctitle reads this switch when it is imported.
|
||||
os.environ.setdefault("SPT_NOENV", "1")
|
||||
if sys.platform == "linux":
|
||||
os.environ.setdefault("SPT_NOENV", "1")
|
||||
from setproctitle import setproctitle
|
||||
|
||||
setproctitle(title)
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import types
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from nanobot.cli.commands import app
|
||||
from nanobot.cli.process_identity import named_executable, set_cli_process_identity
|
||||
from nanobot.cli.process_identity import (
|
||||
_set_process_title,
|
||||
named_executable,
|
||||
set_cli_process_identity,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -46,6 +52,25 @@ def test_cli_process_identity_keeps_windows_launcher_name(
|
||||
assert titles == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("platform", "noenv"), [("linux", "1"), ("darwin", None)])
|
||||
def test_process_title_keeps_environment_only_on_linux(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
platform: str,
|
||||
noenv: str | None,
|
||||
) -> None:
|
||||
titles: list[str] = []
|
||||
monkeypatch.setattr("nanobot.cli.process_identity.sys.platform", platform)
|
||||
monkeypatch.delenv("SPT_NOENV", raising=False)
|
||||
monkeypatch.setitem(
|
||||
sys.modules, "setproctitle", types.SimpleNamespace(setproctitle=titles.append)
|
||||
)
|
||||
|
||||
_set_process_title("nanobot-agent")
|
||||
|
||||
assert os.environ.get("SPT_NOENV") == noenv
|
||||
assert titles == ["nanobot-agent"]
|
||||
|
||||
|
||||
def test_legacy_console_entrypoint_still_sets_subcommand_identity(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
|
||||
Reference in new issue
Block a user