refactor: unify protected task completion

This commit is contained in:
jxxghp
2026-08-24 03:20:45 +08:00
parent 10582277e2
commit 22848eeda2
7 changed files with 64 additions and 34 deletions
+3 -2
View File
@@ -13,8 +13,8 @@
"runtime_to_db": [],
"workflow_to_db": []
},
"edge_count": 6531,
"edge_sha256": "86b2ad0b585deba78c5e0e5e7e4b55cd66e05a2d5e988ed28ac7f6a4ae15ff2f",
"edge_count": 6532,
"edge_sha256": "3fdba2c6862f9ea28ab4f059c04dd6bb819b86fa9d9fb74d09a0cfcf69f6b711",
"edges": [
"app -> app.runtime",
"app -> app.runtime.compat",
@@ -2704,6 +2704,7 @@
"app.application.plugin.install -> app.application.plugin",
"app.application.plugin.install -> app.application.plugin.lifecycle",
"app.application.plugin.install -> app.runtime",
"app.application.plugin.install -> app.runtime.execution",
"app.application.plugin.install -> app.runtime.log",
"app.application.plugin.install -> app.schemas",
"app.application.plugin.install -> app.schemas.exception",
+30 -1
View File
@@ -8,7 +8,10 @@ from anyio.to_thread import current_default_thread_limiter
from app.adapters.external import market as market_adapter
from app.adapters.system.plugin import package as plugin_package_adapter
from app.runtime.execution import run_in_threadpool_to_completion
from app.runtime.execution import (
await_task_to_terminal,
run_in_threadpool_to_completion,
)
def test_plugin_file_adapters_share_runtime_completion_contract() -> None:
@@ -20,6 +23,32 @@ def test_plugin_file_adapters_share_runtime_completion_contract() -> None:
)
@pytest.mark.asyncio
async def test_await_task_to_terminal_ignores_repeated_cancellation() -> None:
"""调用方连续取消时,受保护任务仍须结束并返回真实结果。"""
started = asyncio.Event()
release = asyncio.Event()
async def protected_operation() -> str:
"""阻塞到测试释放,用于观察受保护任务的真实终态。"""
started.set()
await release.wait()
return "completed"
protected_task = asyncio.create_task(protected_operation())
waiter = asyncio.create_task(await_task_to_terminal(protected_task))
await started.wait()
waiter.cancel()
await asyncio.sleep(0)
waiter.cancel()
await asyncio.sleep(0)
assert waiter.done() is False
release.set()
assert await waiter == "completed"
@pytest.mark.asyncio
async def test_threadpool_capacity_is_held_until_cancelled_call_finishes() -> None:
"""调用方取消后,执行令牌必须由真实同步调用持有到终态。"""