Skip to content

Commit 3b41a87

Browse files
fix: updated reboot logic (#9073)
* Update updator.py * test: cover Windows reboot process spawning --------- Co-authored-by: 邹永赫 <1259085392@qq.com>
1 parent b673cb3 commit 3b41a87

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

astrbot/core/updator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import subprocess
23
import sys
34
import time
45
import zipfile
@@ -139,6 +140,11 @@ def _exec_reboot(executable: str, argv: list[str]) -> None:
139140
quoted_args = [f'"{arg}"' if " " in arg else arg for arg in argv[1:]]
140141
os.execl(executable, quoted_executable, *quoted_args)
141142
return
143+
elif os.name == "nt":
144+
subprocess.Popen(
145+
[executable] + argv[1:], creationflags=subprocess.CREATE_NEW_CONSOLE
146+
)
147+
os._exit(0)
142148
os.execv(executable, argv)
143149

144150
def _reboot(self, delay: int = 3) -> None:

tests/test_updator_socks.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import httpx
1111
import pytest
1212

13+
from astrbot.core import updator as core_updator
1314
from astrbot.core.star.updator import PluginUpdator
1415
from astrbot.core.updator import AstrBotUpdator
1516
from astrbot.core.utils import io as io_utils
@@ -80,6 +81,60 @@ def raise_for_status(self) -> None:
8081
)
8182

8283

84+
def test_astrbot_updator_exec_reboot_spawns_new_console_on_windows(
85+
monkeypatch: pytest.MonkeyPatch,
86+
):
87+
popen_calls = []
88+
exit_codes = []
89+
execv_calls = []
90+
91+
def fake_popen(args, creationflags=0):
92+
popen_calls.append((args, creationflags))
93+
return SimpleNamespace(pid=1234)
94+
95+
def fake_exit(code):
96+
exit_codes.append(code)
97+
raise SystemExit(code)
98+
99+
def fake_execv(*args):
100+
execv_calls.append(args)
101+
102+
monkeypatch.setattr(core_updator.os, "name", "nt")
103+
monkeypatch.setattr(core_updator.sys, "frozen", False, raising=False)
104+
monkeypatch.setattr(
105+
core_updator.subprocess, "CREATE_NEW_CONSOLE", 0x00000010, raising=False
106+
)
107+
monkeypatch.setattr(core_updator.subprocess, "Popen", fake_popen)
108+
monkeypatch.setattr(core_updator.os, "_exit", fake_exit)
109+
monkeypatch.setattr(core_updator.os, "execv", fake_execv)
110+
111+
with pytest.raises(SystemExit) as exc_info:
112+
AstrBotUpdator._exec_reboot(
113+
r"C:\Python312\python.exe",
114+
[
115+
r"C:\Python312\python.exe",
116+
"main.py",
117+
"--webui-dir",
118+
r"C:\AstrBot WebUI\dist",
119+
],
120+
)
121+
122+
assert exc_info.value.code == 0
123+
assert popen_calls == [
124+
(
125+
[
126+
r"C:\Python312\python.exe",
127+
"main.py",
128+
"--webui-dir",
129+
r"C:\AstrBot WebUI\dist",
130+
],
131+
core_updator.subprocess.CREATE_NEW_CONSOLE,
132+
)
133+
]
134+
assert exit_codes == [0]
135+
assert execv_calls == []
136+
137+
83138
@dataclass
84139
class _FakeAsyncClientState:
85140
json_payload: object = field(default_factory=list)

0 commit comments

Comments
 (0)