Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(model-manager): time out stalled model downloads (#10378)

Thejesh1bd3954850c1
diff --git a/invokeai/app/services/download/download_default.py b/invokeai/app/services/download/download_default.py
index 27e9163bef..5e8e93f9b5 100644
--- a/invokeai/app/services/download/download_default.py
+++ b/invokeai/app/services/download/download_default.py
@@ -47,6 +47,10 @@ if TYPE_CHECKING:
# Maximum number of bytes to download during each call to requests.iter_content()
DOWNLOAD_CHUNK_SIZE = 100000
+# (connect, read) seconds for download GETs. The read timeout bounds each socket read, not the whole transfer, so a
+# server that stalls mid-body fails the job instead of holding the worker (and its cancel check) forever.
+DOWNLOAD_TIMEOUT = (15, 60)
+
# How long an idle worker blocks on the queue before rechecking the stop event, which bounds how long stop() waits.
_QUEUE_POLL_SECONDS = 1.0
@@ -461,6 +465,7 @@ class DownloadQueueService(DownloadQueueServiceBase):
str(url),
headers=header,
stream=True,
+ timeout=DOWNLOAD_TIMEOUT,
hooks={"response": self._reject_unsafe_redirect},
proxies=self._request_proxies,
)
diff --git a/tests/app/services/download/test_download_queue.py b/tests/app/services/download/test_download_queue.py
index 4294bb70f0..efd2d8cd77 100644
--- a/tests/app/services/download/test_download_queue.py
+++ b/tests/app/services/download/test_download_queue.py
@@ -1,10 +1,11 @@
"""Test the queued download facility"""
import re
+import socket
import time
from contextlib import contextmanager
from pathlib import Path
-from threading import Event
+from threading import Event, Thread
from typing import Any, Generator, Optional
from unittest.mock import MagicMock, patch
@@ -16,7 +17,13 @@ from requests_testadapter import TestAdapter, TestSession
from invokeai.app.services.config import InvokeAIAppConfig, get_config
from invokeai.app.services.config.config_default import URLRegexTokenPair
-from invokeai.app.services.download import DownloadJob, DownloadJobStatus, DownloadQueueService, MultiFileDownloadJob
+from invokeai.app.services.download import (
+ DownloadJob,
+ DownloadJobStatus,
+ DownloadQueueService,
+ MultiFileDownloadJob,
+ download_default,
+)
from invokeai.app.services.events.events_common import (
DownloadCancelledEvent,
DownloadCompleteEvent,
@@ -817,6 +824,42 @@ def test_head_request_is_sent_like_a_download(monkeypatch: Any) -> None:
guarded.head("http://127.0.0.1:19191/file")
[email protected](timeout=10, method="thread")
+def test_download_from_a_server_that_stalls_mid_body_errors_instead_of_hanging(
+ tmp_path: Path, monkeypatch: Any
+) -> None:
+ """A server that sends part of the body and then goes silent must not hold the worker forever."""
+ monkeypatch.setattr(download_default, "DOWNLOAD_TIMEOUT", (2, 0.5))
+ # Talk to the local server directly: an ambient proxy would fail the request for an unrelated reason.
+ monkeypatch.setenv("NO_PROXY", "*")
+ monkeypatch.setenv("no_proxy", "*")
+ release = Event()
+ listener = socket.create_server(("127.0.0.1", 0))
+ port = listener.getsockname()[1]
+
+ def stall() -> None:
+ conn, _ = listener.accept()
+ with conn:
+ conn.recv(65536)
+ conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 1000000\r\n\r\n" + b"x" * 1000)
+ release.wait(timeout=10)
+
+ server = Thread(target=stall, daemon=True)
+ server.start()
+ queue = DownloadQueueService(app_config=InvokeAIAppConfig(allow_private_download_urls=True))
+ queue.start()
+ try:
+ job = queue.download(source=AnyHttpUrl(f"http://127.0.0.1:{port}/model.safetensors"), dest=tmp_path)
+ queue.wait_for_job(job, timeout=5)
+ assert job.status == DownloadJobStatus.ERROR
+ assert "Read timed out" in (job.error_type or "")
+ finally:
+ release.set()
+ queue.stop()
+ server.join(timeout=5)
+ listener.close()
+
+
@pytest.mark.timeout(timeout=10, method="thread")
def test_download_refuses_non_public_source(tmp_path: Path) -> None:
"""A job whose source points at loopback errors out without issuing the request."""