Skip to content

The only downloader you need. 下载器的集大成者。

fix(keepalive): 修复取消下载后及 Linux 下无法正常睡眠

XiaoYouChR622071c82548
diff --git a/Ghost-Downloader-3.py b/Ghost-Downloader-3.py
index ad427376..da31efaa 100644
--- a/Ghost-Downloader-3.py
+++ b/Ghost-Downloader-3.py
@@ -109,6 +109,9 @@ def startApp(application: Application, primaryInstance: PrimaryInstance, isSilen
plan = Plan(allCompleted=lambda: taskService.runningCount() == 0)
taskService.tasksAllCompleted.connect(plan.trigger)
+ from app.platform.desktop_keepalive import buildSleepInhibitor
+ taskService.runningChanged.connect(buildSleepInhibitor())
+
application.clipboardListener = ClipboardListener(featureService.matchPassive, parent=application)
cfg.isClipboardListenerEnabled.valueChanged.connect(application.clipboardListener.setEnabled)
application.clipboardListener.setEnabled(cfg.isClipboardListenerEnabled.value)
@@ -240,10 +243,6 @@ def startApp(application: Application, primaryInstance: PrimaryInstance, isSilen
from app.platform.taskbar import setLauncherProgress
speedMeter.speedChanged.connect(lambda _: setLauncherProgress(taskService.overallProgress()))
- from app.platform.desktop_keepalive import hold, release
- taskService.taskStarted.connect(lambda _: hold())
- taskService.tasksAllCompleted.connect(release)
-
from app.platform.desktop_notification import (
PopupNotifier, PopupStack, SystemNotifier, buildClipboardRequest,
buildDiskSpaceInsufficient, buildPairRequest, buildParseFailed, buildTaskCompleted, buildTaskFailed,
diff --git a/app/platform/desktop_keepalive.py b/app/platform/desktop_keepalive.py
index 9360f95e..8abfc71f 100644
--- a/app/platform/desktop_keepalive.py
+++ b/app/platform/desktop_keepalive.py
@@ -1,113 +1,150 @@
from __future__ import annotations
import sys
+from collections.abc import Callable
from loguru import logger
-_held = False
+def buildSleepInhibitor() -> Callable[[bool], None]:
+ release: Callable[[], None] | None = None
-def hold() -> None:
- global _held
- if _held:
- return
- try:
- _inhibit()
- _held = True
- except Exception as e:
- logger.opt(exception=e).warning("Failed to inhibit system sleep")
+ def set(isActive: bool) -> None:
+ nonlocal release
+ if not isActive:
+ if release is None:
+ return
+ fn, release = release, None
+ try:
+ fn()
+ except Exception as e:
+ logger.opt(exception=e).warning("Failed to release sleep inhibit")
+ return
+ if release is not None:
+ return
+ try:
+ release = acquire()
+ except Exception as e:
+ logger.opt(exception=e).warning("Failed to inhibit system sleep")
+ return set
-def release() -> None:
- global _held
- if not _held:
- return
- try:
- _uninhibit()
- except Exception as e:
- logger.opt(exception=e).warning("Failed to release sleep inhibit")
- _held = False
-
-
-# -- platform backends --
if sys.platform == "win32":
import ctypes
- _ES_CONTINUOUS = 0x80000000
- _ES_SYSTEM_REQUIRED = 0x00000001
-
- def _inhibit() -> None:
- ctypes.windll.kernel32.SetThreadExecutionState(
- _ES_CONTINUOUS | _ES_SYSTEM_REQUIRED
- )
-
- def _uninhibit() -> None:
- ctypes.windll.kernel32.SetThreadExecutionState(_ES_CONTINUOUS)
+ ES_CONTINUOUS = 0x80000000
+ ES_SYSTEM_REQUIRED = 0x00000001
+ setThreadExecutionState = ctypes.WinDLL("kernel32").SetThreadExecutionState
+ setThreadExecutionState.argtypes = [ctypes.c_uint32]
+ setThreadExecutionState.restype = ctypes.c_uint32
+
+ def acquire() -> Callable[[], None] | None:
+ try:
+ if not setThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED):
+ logger.warning("SetThreadExecutionState returned 0")
+ return None
+ except OSError as e:
+ logger.opt(exception=e).warning("Failed to inhibit system sleep")
+ return None
+
+ def release() -> None:
+ try:
+ setThreadExecutionState(ES_CONTINUOUS)
+ except OSError as e:
+ logger.opt(exception=e).warning("Failed to release sleep inhibit")
+
+ return release
elif sys.platform == "darwin":
import ctypes
- import ctypes.util
- _iokit = ctypes.cdll.LoadLibrary(ctypes.util.find_library("IOKit"))
- _cf = ctypes.cdll.LoadLibrary(ctypes.util.find_library("CoreFoundation"))
+ iokit = ctypes.cdll.LoadLibrary("/System/Library/Frameworks/IOKit.framework/IOKit")
+ cf = ctypes.cdll.LoadLibrary("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")
- _cf.CFStringCreateWithCString.restype = ctypes.c_void_p
- _cf.CFStringCreateWithCString.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint32]
- _cf.CFRelease.argtypes = [ctypes.c_void_p]
+ cf.CFStringCreateWithCString.restype = ctypes.c_void_p
+ cf.CFStringCreateWithCString.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint32]
+ cf.CFRelease.argtypes = [ctypes.c_void_p]
- _iokit.IOPMAssertionCreateWithName.argtypes = [
+ iokit.IOPMAssertionCreateWithName.argtypes = [
ctypes.c_void_p, ctypes.c_uint32, ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32),
]
- _iokit.IOPMAssertionCreateWithName.restype = ctypes.c_int32
- _iokit.IOPMAssertionRelease.argtypes = [ctypes.c_uint32]
- _iokit.IOPMAssertionRelease.restype = ctypes.c_int32
+ iokit.IOPMAssertionCreateWithName.restype = ctypes.c_uint32
+ iokit.IOPMAssertionRelease.argtypes = [ctypes.c_uint32]
+ iokit.IOPMAssertionRelease.restype = ctypes.c_uint32
- _kCFStringEncodingUTF8 = 0x08000100
- _kIOPMAssertionLevelOn = 255
+ CF_STRING_ENCODING_UTF8 = 0x08000100
+ IOPM_ASSERTION_LEVEL_ON = 255
- def _cfstr(s: str) -> ctypes.c_void_p:
- return _cf.CFStringCreateWithCString(None, s.encode(), _kCFStringEncodingUTF8)
+ def toCfString(text: str) -> ctypes.c_void_p:
+ return cf.CFStringCreateWithCString(None, text.encode(), CF_STRING_ENCODING_UTF8)
- _NO_IDLE_SLEEP = _cfstr("PreventUserIdleSystemSleep")
- _assertionId = ctypes.c_uint32(0)
+ NO_IDLE_SLEEP = toCfString("PreventUserIdleSystemSleep")
- def _inhibit() -> None:
- global _assertionId
- reason = _cfstr("Active download")
- ret = _iokit.IOPMAssertionCreateWithName(
- _NO_IDLE_SLEEP, _kIOPMAssertionLevelOn, reason, ctypes.byref(_assertionId),
+ def acquire() -> Callable[[], None]:
+ assertionId = ctypes.c_uint32(0)
+ reason = toCfString("Active download")
+ ret = iokit.IOPMAssertionCreateWithName(
+ NO_IDLE_SLEEP, IOPM_ASSERTION_LEVEL_ON, reason, ctypes.byref(assertionId),
)
- _cf.CFRelease(reason)
+ cf.CFRelease(reason)
if ret != 0:
raise RuntimeError(f"IOPMAssertionCreateWithName failed: {ret:#x}")
-
- def _uninhibit() -> None:
- global _assertionId
- if _assertionId.value:
- _iokit.IOPMAssertionRelease(_assertionId.value)
- _assertionId.value = 0
+ aid = assertionId.value
+ return lambda: iokit.IOPMAssertionRelease(aid)
else:
- # Linux: systemd-logind D-Bus inhibitor lock
- _inhibitFd = None
+ INHIBIT_SUSPEND = 4
+
+ # logind 的 sleep 锁会拦住合盖和手动睡眠。logind 的 idle 锁会被 GNOME 和 KDE 忽略。
+ # 因此先走桌面会话,无桌面会话时再退回 logind idle。
+ # PySide6 的 QDBusInterface.call 会把 int 封送成有符号 i,必须经 QMetaObject.invokeMethod 按签名发 uint32。
+ def acquire() -> Callable[[], None] | None:
+ from PySide6.QtCore import Q_ARG, Q_RETURN_ARG, QMetaObject, Qt
+ from PySide6.QtDBus import QDBusConnection, QDBusInterface, QDBusUnixFileDescriptor
+
+ from app.config.constants import DESKTOP_ID
+
+ sessionBus = QDBusConnection.sessionBus()
+ busIface = sessionBus.interface()
+ if busIface is not None:
+ for service, path, interface, args, uninhibit in (
+ ("org.gnome.SessionManager", "/org/gnome/SessionManager", "org.gnome.SessionManager",
+ (Q_ARG("QString", DESKTOP_ID), Q_ARG("uint", 0), Q_ARG("QString", "Active download"),
+ Q_ARG("uint", INHIBIT_SUSPEND)), "Uninhibit"),
+ ("org.freedesktop.PowerManagement", "/org/freedesktop/PowerManagement/Inhibit",
+ "org.freedesktop.PowerManagement.Inhibit",
+ (Q_ARG("QString", "Ghost Downloader"), Q_ARG("QString", "Active download")), "UnInhibit"),
+ ):
+ if not busIface.isServiceRegistered(service).value():
+ continue
+ iface = QDBusInterface(service, path, interface, sessionBus)
+ if not iface.isValid():
+ continue
+ try:
+ cookie = QMetaObject.invokeMethod(
+ iface, "Inhibit", Qt.ConnectionType.DirectConnection, Q_RETURN_ARG("uint"), *args,
+ )
+ except RuntimeError as e:
+ logger.debug("Sleep inhibit via {} failed: {}", service, e)
+ continue
+ if not iface.lastError().isValid() and cookie is not None:
+ def release(i=iface, m=uninhibit, c=cookie, s=service) -> None:
+ ok = QMetaObject.invokeMethod(i, m, Qt.ConnectionType.DirectConnection, Q_ARG("uint", c))
+ if not ok or i.lastError().isValid():
+ logger.warning("Failed to release sleep inhibit via {}: {}", s, i.lastError().message())
+ return release
+ logger.debug("Sleep inhibit via {} failed: {}", service, iface.lastError().message())
- def _inhibit() -> None:
- global _inhibitFd
- from PySide6.QtDBus import QDBusConnection, QDBusInterface
-
- bus = QDBusConnection.systemBus()
iface = QDBusInterface(
"org.freedesktop.login1", "/org/freedesktop/login1",
- "org.freedesktop.login1.Manager", bus,
+ "org.freedesktop.login1.Manager", QDBusConnection.systemBus(),
)
- reply = iface.call("Inhibit", "sleep", "Ghost Downloader", "Active download", "block")
+ reply = iface.call("Inhibit", "idle", "Ghost Downloader", "Active download", "block")
if reply.type() == reply.MessageType.ReplyMessage:
- _inhibitFd = reply.arguments()[0]
- else:
- raise RuntimeError(reply.errorMessage())
-
- def _uninhibit() -> None:
- global _inhibitFd
- _inhibitFd = None
+ fd = reply.arguments()[0]
+ if isinstance(fd, QDBusUnixFileDescriptor) and fd.isValid():
+ holder = [fd]
+ return holder.clear
+ raise RuntimeError(reply.errorMessage() or "Invalid logind inhibitor file descriptor")
diff --git a/app/services/task_service.py b/app/services/task_service.py
index 688bc82d..19a3f67f 100644
--- a/app/services/task_service.py
+++ b/app/services/task_service.py
@@ -278,6 +278,7 @@ class TaskService:
taskCompleted = Signal(object)
taskFailed = Signal(object)
tasksAllCompleted = Signal()
+ runningChanged = Signal(bool)
seedingStarted = Signal(object)
seedingStopped = Signal(object)
checksumStarted = Signal(object)
@@ -658,6 +659,7 @@ class TaskService:
if task is not None:
self._dispatch(task)
self._refreshSpeedMeter()
+ self.runningChanged.emit(self._queue.runningCount() > 0)
def _refreshSpeedMeter(self) -> None:
if self._queue.runningCount() or self._seeding:
diff --git a/tests/test_task_service.py b/tests/test_task_service.py
index c53c393f..da4e32c6 100644
--- a/tests/test_task_service.py
+++ b/tests/test_task_service.py
@@ -6,7 +6,7 @@ import pytest
from app.config.cfg import cfg
from app.models.task import Task, TaskError, TaskStatus
-from tests.conftest import FakeStep
+from tests.conftest import FakeSeedingTask, FakeStep
from tests.helpers import waitUntil
@@ -125,3 +125,60 @@ async def test_move_running_to_front_changes_nothing(engine):
engine.taskService.moveToFront([task.taskId for task in tasks])
assert changed == []
+
+
+async def test_pause_and_delete_last_running_announces_not_running(engine):
+ states = []
+ engine.taskService.runningChanged.connect(states.append)
+ task = buildTask()
+
+ engine.taskService.add(task)
+ await waitUntil(lambda: task.status == TaskStatus.RUNNING)
+ engine.taskService.pause(task)
+ engine.taskService.start(task)
+ await waitUntil(lambda: task.status == TaskStatus.RUNNING)
+ engine.taskService.delete(task, shouldDeleteFiles=False)
+
+ assert states == [True, False, True, False]
+
+
+async def test_pause_with_waiting_task_keeps_running(engine):
+ cfg.set(cfg.maxTaskNum, 1)
+ states = []
+ engine.taskService.runningChanged.connect(states.append)
+ first, second = buildTask(), buildTask()
+ engine.taskService.add(first)
+ engine.taskService.add(second)
+ await waitUntil(lambda: first.status == TaskStatus.RUNNING)
+
+ engine.taskService.pause(first)
+
+ await waitUntil(lambda: second.status == TaskStatus.RUNNING)
+ assert states and all(states)
+
+
[email protected]("outcome", ["complete", "fail"])
+async def test_run_announces_not_running_before_all_completed(engine, outcome):
+ events = []
+ engine.taskService.runningChanged.connect(lambda isRunning: events.append(("running", isRunning)))
+ engine.taskService.tasksAllCompleted.connect(lambda: events.append(("allCompleted",)))
+
+ engine.taskService.add(buildTask(outcome))
+
+ await waitUntil(lambda: ("allCompleted",) in events)
+ assert events == [("running", True), ("running", False), ("allCompleted",)]
+
+
+async def test_seeding_does_not_announce_running(engine):
+ states = []
+ engine.taskService.runningChanged.connect(states.append)
+ task = FakeSeedingTask(name="seed.zip", url="http://test/seed.zip", packId="test",
+ steps=[FakeStep(stepIndex=0, outcome="complete")])
+
+ engine.taskService.add(task)
+ await waitUntil(lambda: task.isSeeding)
+ engine.taskService.stopSeeding(task)
+ engine.taskService.startSeeding(task)
+ await waitUntil(lambda: task.isSeeding)
+
+ assert states == [True, False]