mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-06 07:56:52 +08:00
fix: 保持搜索线程任务的请求上下文 (#6451)
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
import asyncio
|
||||
import ast
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
@@ -41,6 +44,42 @@ def test_host_uses_canonical_threadpool_boundary() -> None:
|
||||
assert violations == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("inherit_context", [0, 1])
|
||||
def test_submit_with_context_is_independent_of_thread_inheritance(
|
||||
inherit_context: int,
|
||||
) -> None:
|
||||
"""线程继承开关不得改变逐任务快照,worker 也不得保留首个请求状态。"""
|
||||
script = """
|
||||
import json
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextvars import ContextVar
|
||||
|
||||
from app.runtime.execution import submit_with_context
|
||||
|
||||
request_id = ContextVar("request_id", default=None)
|
||||
executor = ThreadPoolExecutor(max_workers=1)
|
||||
observed = []
|
||||
for value in ("first", "second"):
|
||||
token = request_id.set(value)
|
||||
try:
|
||||
observed.append(submit_with_context(executor, request_id.get).result())
|
||||
finally:
|
||||
request_id.reset(token)
|
||||
observed.append(executor.submit(request_id.get).result())
|
||||
executor.shutdown()
|
||||
print(json.dumps(observed))
|
||||
"""
|
||||
|
||||
completed = subprocess.run(
|
||||
[sys.executable, "-X", f"thread_inherit_context={inherit_context}", "-c", script],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
assert json.loads(completed.stdout) == ["first", "second", None]
|
||||
|
||||
|
||||
def test_plugin_file_adapters_share_runtime_completion_contract() -> None:
|
||||
"""市场与插件包适配器不得各自维护另一套线程取消实现。"""
|
||||
assert market_adapter._await_thread_operation is run_in_threadpool_to_completion
|
||||
|
||||
Reference in New Issue
Block a user