Skip to content

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

fix(coroutine): 调度前取消的工作不再照常启动

XiaoYouChRb8c6b08b3067
diff --git a/app/services/coroutine_runner.py b/app/services/coroutine_runner.py
index 72465db8..4887bf7a 100644
--- a/app/services/coroutine_runner.py
+++ b/app/services/coroutine_runner.py
@@ -64,6 +64,9 @@ class CoroutineRunner:
self.post(failed, error, *args, **kwargs)
def schedule():
+ if workId not in self._pending:
+ work.close()
+ return
self._running[workId] = self._loop.create_task(execute())
self._loop.call_soon_threadsafe(schedule)
diff --git a/tests/test_coroutine_runner.py b/tests/test_coroutine_runner.py
new file mode 100644
index 00000000..5604884e
--- /dev/null
+++ b/tests/test_coroutine_runner.py
@@ -0,0 +1,22 @@
+from __future__ import annotations
+
+import asyncio
+
+from app.services.coroutine_runner import CoroutineRunner
+
+
+async def test_work_cancelled_before_it_is_scheduled_never_runs():
+ loop = asyncio.get_running_loop()
+ runner = CoroutineRunner(loop.call_soon, loop=loop)
+ started = []
+ finished = []
+
+ async def work():
+ started.append(True)
+
+ workId = runner.submit(work())
+ runner.cancel(workId, finished=lambda: finished.append(True))
+ await asyncio.sleep(0.05)
+
+ assert started == []
+ assert finished == [True]