Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(desktop): 卸载助手清场先按镜像名筛进程,只读同名进程的路径

longmeidaoc0b8a04db3e5
diff --git a/src/peach/desktop_uninstall.py b/src/peach/desktop_uninstall.py
index 6ce4fef50..528fe7940 100644
--- a/src/peach/desktop_uninstall.py
+++ b/src/peach/desktop_uninstall.py
@@ -112,9 +112,14 @@ function Stop-PeachProgramProcesses {
# 托盘退出时被硬杀的服务会留下自己的子进程:扫描、转码用的可执行文件可能就在
# `_internal` 里。按镜像路径清场,只匹配程序目录前缀加一个分隔符,不误伤名字
# 相近的其它解压目录。
+ # 读 `Path` 要打开目标进程、枚举它的全部模块,七百多个进程逐个读一遍要七秒以上,
+ # 删除重试时还要再清场。进程名就是镜像文件名去掉 `.exe`,先拿 `$peachImageNames`
+ # 筛出与程序目录里某个 `.exe` 同名的进程,只读这几个的路径。
# `Path` 每读一次都现查一次进程:判空和比前缀各读一次的话,进程恰好在两次之间退出,
# 第二次读到的就是 Null。只读一次。
$peachStrays = @(Get-Process -ErrorAction SilentlyContinue | Where-Object {
+ $peachImageNames.Contains($_.ProcessName)
+ } | Where-Object {
$peachImage = $_.Path
$peachImage -and $peachImage.StartsWith($peachProgram + [IO.Path]::DirectorySeparatorChar, [StringComparison]::OrdinalIgnoreCase)
})
@@ -133,10 +138,15 @@ try {
foreach ($peachChild in Get-ChildItem -LiteralPath $peachNode -Force) { Test-PeachTree $peachChild.FullName }
}
}
- Stop-PeachProgramProcesses
foreach ($peachPath in @($peachJob.directories) + @($peachJob.files) + @($peachProgram)) {
if (Test-Path -LiteralPath $peachPath) { Test-PeachTree $peachPath }
}
+ # 名单在链接检查之后收:程序树里有链接的话上一步已经抛出,递归不会走出程序目录。
+ $peachImageNames = [Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
+ foreach ($peachExecutable in Get-ChildItem -LiteralPath $peachProgram -Recurse -Force -File -Filter '*.exe') {
+ [void]$peachImageNames.Add($peachExecutable.BaseName)
+ }
+ Stop-PeachProgramProcesses
foreach ($peachPath in @($peachJob.directories) + @($peachJob.files) + @($peachProgram)) {
for ($peachAttempt = 3; $peachAttempt -gt 0; $peachAttempt--) {
if (-not (Test-Path -LiteralPath $peachPath)) { break }
diff --git a/tests/test_desktop_settings.py b/tests/test_desktop_settings.py
index 9f7b3dfbf..ac31f50dd 100644
--- a/tests/test_desktop_settings.py
+++ b/tests/test_desktop_settings.py
@@ -379,12 +379,13 @@ class DesktopSettingsTests(unittest.TestCase):
payload = dict(job, pid=2147483647, quiet=True)
if log is not None:
payload['log'] = str(log)
- # 助手每轮重试都要枚举一遍全部进程的路径;本机七百多个进程时一遍约 9 秒,
- # 删不掉文件的用例要走满三轮,30 秒的上限正好卡在它的正常耗时上。
+ # 删不掉文件的用例清场三轮、等两次各 2 秒的重试,本机分片并行时约 6 秒。
+ # 30 秒的上限同时守着清场只读同名进程的路径:逐个读七百多个进程的路径一遍
+ # 就要七秒以上,三轮加等待会超时。
return subprocess.run([str(shell), '-NoProfile', '-NonInteractive', '-EncodedCommand',
base64.b64encode(desktop_uninstall._SCRIPT.encode('utf-16-le')).decode('ascii')],
input=json.dumps(payload).encode('utf-8'), capture_output=True,
- timeout=120, creationflags=subprocess.CREATE_NO_WINDOW)
+ timeout=30, creationflags=subprocess.CREATE_NO_WINDOW)
@unittest.skipUnless(os.name == 'nt', 'Windows 系统卸载助手')
def test_native_uninstall_kills_strays_running_from_the_program(self):