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
6 changes: 6 additions & 0 deletions astrbot/core/updator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess
import sys
import time
import zipfile
Expand Down Expand Up @@ -135,6 +136,11 @@ def _exec_reboot(executable: str, argv: list[str]) -> None:
quoted_args = [f'"{arg}"' if " " in arg else arg for arg in argv[1:]]
os.execl(executable, quoted_executable, *quoted_args)
return
elif os.name == "nt":
subprocess.Popen(
[executable] + argv[1:], creationflags=subprocess.CREATE_NEW_CONSOLE
)
Comment on lines +140 to +142

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The expression [executable] + argv[1:] is redundant because argv is already constructed with executable as its first element (see _build_reboot_argv). You can pass argv directly to subprocess.Popen to simplify the code and avoid unnecessary list slicing and concatenation.

Suggested change
subprocess.Popen(
[executable] + argv[1:], creationflags=subprocess.CREATE_NEW_CONSOLE
)
subprocess.Popen(
argv, creationflags=subprocess.CREATE_NEW_CONSOLE
)

os._exit(0)
os.execv(executable, argv)

def _reboot(self, delay: int = 3) -> None:
Expand Down
55 changes: 55 additions & 0 deletions tests/test_updator_socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import httpx
import pytest

from astrbot.core import updator as core_updator
from astrbot.core.star.updator import PluginUpdator
from astrbot.core.updator import AstrBotUpdator
from astrbot.core.utils import io as io_utils
Expand Down Expand Up @@ -80,6 +81,60 @@ def raise_for_status(self) -> None:
)


def test_astrbot_updator_exec_reboot_spawns_new_console_on_windows(
monkeypatch: pytest.MonkeyPatch,
):
popen_calls = []
exit_codes = []
execv_calls = []

def fake_popen(args, creationflags=0):
popen_calls.append((args, creationflags))
return SimpleNamespace(pid=1234)

def fake_exit(code):
exit_codes.append(code)
raise SystemExit(code)

def fake_execv(*args):
execv_calls.append(args)

monkeypatch.setattr(core_updator.os, "name", "nt")
monkeypatch.setattr(core_updator.sys, "frozen", False, raising=False)
monkeypatch.setattr(
core_updator.subprocess, "CREATE_NEW_CONSOLE", 0x00000010, raising=False
)
monkeypatch.setattr(core_updator.subprocess, "Popen", fake_popen)
monkeypatch.setattr(core_updator.os, "_exit", fake_exit)
monkeypatch.setattr(core_updator.os, "execv", fake_execv)

with pytest.raises(SystemExit) as exc_info:
AstrBotUpdator._exec_reboot(
r"C:\Python312\python.exe",
[
r"C:\Python312\python.exe",
"main.py",
"--webui-dir",
r"C:\AstrBot WebUI\dist",
],
)

assert exc_info.value.code == 0
assert popen_calls == [
(
[
r"C:\Python312\python.exe",
"main.py",
"--webui-dir",
r"C:\AstrBot WebUI\dist",
],
core_updator.subprocess.CREATE_NEW_CONSOLE,
)
]
assert exit_codes == [0]
assert execv_calls == []


@dataclass
class _FakeAsyncClientState:
json_payload: object = field(default_factory=list)
Expand Down
Loading