Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(loomloom): isolate staging files for concurrent artifact downloads

Rudy Celekli68eb5a68b93c
diff --git a/app/services/loomloom.py b/app/services/loomloom.py
index 48d3493c..e4216b61 100644
--- a/app/services/loomloom.py
+++ b/app/services/loomloom.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import json
import math
import os
+import tempfile
import time
from collections.abc import Callable, Mapping
from dataclasses import dataclass, field
@@ -941,7 +942,7 @@ class LoomLoomVideoBackend(LoomLoomScriptBackend):
return {**matching[0], "accessUrl": access_url}
def _download_video_artifact(self, access_url: str, destination: str) -> None:
- temporary = destination + ".part"
+ temporary = None
downloaded_bytes = 0
response = None
try:
@@ -954,7 +955,11 @@ class LoomLoomVideoBackend(LoomLoomScriptBackend):
content_length = int(response.headers.get("content-length", 0) or 0)
if content_length > MAX_VIDEO_ARTIFACT_BYTES:
raise LoomLoomAPIError("video artifact exceeds the download limit")
- with open(temporary, "wb") as output:
+ with tempfile.NamedTemporaryFile(
+ mode="wb", dir=os.path.dirname(destination) or ".",
+ prefix=os.path.basename(destination) + ".", suffix=".part", delete=False,
+ ) as output:
+ temporary = output.name
for chunk in response.iter_content(chunk_size=1024 * 1024):
if not chunk:
continue
@@ -985,5 +990,5 @@ class LoomLoomVideoBackend(LoomLoomScriptBackend):
"failed to close LoomLoom video download response: "
f"error={type(exc).__name__}"
)
- if os.path.exists(temporary):
+ if temporary is not None and os.path.exists(temporary):
os.remove(temporary)
diff --git a/test/services/test_loom_download_staging_native.py b/test/services/test_loom_download_staging_native.py
new file mode 100644
index 00000000..6f0778d2
--- /dev/null
+++ b/test/services/test_loom_download_staging_native.py
@@ -0,0 +1,86 @@
+import http.server
+import threading
+import requests
+from app.services.loomloom import LoomLoomVideoBackend, LoomLoomSettings
+
+
+def test_native_concurrent_downloads_use_distinct_staging_files(tmp_path):
+ first_chunk = threading.Event()
+ release = threading.Event()
+ size = 1024 * 1024
+ payloads = {
+ "/first": b"A" * size + b"FIRST-END",
+ "/second": b"B" * size + b"SECOND-END",
+ }
+
+ class Handler(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ body = payloads[self.path]
+ self.send_response(200)
+ self.send_header("Content-Length", str(len(body)))
+ self.end_headers()
+ self.wfile.write(body[:size])
+ self.wfile.flush()
+ if self.path == "/first":
+ first_chunk.set()
+ assert release.wait(5)
+ self.wfile.write(body[size:])
+ self.wfile.flush()
+
+ def log_message(self, *args):
+ pass
+
+ server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
+ worker = threading.Thread(target=server.serve_forever, daemon=True)
+ worker.start()
+ destination = tmp_path / "shared.mp4"
+ outcomes = []
+
+ def download(path):
+ try:
+ with requests.Session() as session:
+ backend = LoomLoomVideoBackend(
+ LoomLoomSettings(
+ base_url="http://127.0.0.1",
+ api_token="synthetic",
+ market_listing_id="fixture",
+ ),
+ session=session,
+ )
+ backend._download_video_artifact(
+ f"http://127.0.0.1:{server.server_port}{path}", str(destination)
+ )
+ outcomes.append("complete")
+ except Exception as exc:
+ outcomes.append(type(exc).__name__)
+
+ first = threading.Thread(target=download, args=("/first",))
+ second = threading.Thread(target=download, args=("/second",))
+ try:
+ first.start()
+ assert first_chunk.wait(5)
+ # The first request has already opened its real staging file by the
+ # time it waits for the final response chunk.
+ import time
+
+ deadline = time.monotonic() + 5
+ while not any(tmp_path.glob("*.part")) and time.monotonic() < deadline:
+ time.sleep(0.001)
+ assert any(tmp_path.glob("*.part"))
+ second.start()
+ second.join(5)
+ assert not second.is_alive()
+ release.set()
+ first.join(5)
+ assert not first.is_alive()
+ assert outcomes == ["complete", "complete"]
+ assert destination.read_bytes() in payloads.values()
+ assert not list(tmp_path.glob("*.part"))
+ finally:
+ release.set()
+ first.join(5)
+ if second.ident:
+ second.join(5)
+ server.shutdown()
+ server.server_close()
+ worker.join(2)