From 5bec0461b6bd013d9b00c0f4711ee169e16d5da7 Mon Sep 17 00:00:00 2001 From: Christian Schmidbauer Date: Fri, 10 Jul 2026 12:18:09 +0200 Subject: [PATCH] fix: stream package files instead of reading them into memory The server read every requested artifact fully into memory via path.read_bytes() before writing it to the client. With multi-gigabyte distributions (e.g. torch or CUDA wheels) and the threading server, concurrent downloads could easily exhaust memory. Stream the file to the client in chunks and send a Content-Length header so clients can show download progress. Co-Authored-By: Claude Fable 5 --- morgan/server.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/morgan/server.py b/morgan/server.py index f47ca00..b447956 100644 --- a/morgan/server.py +++ b/morgan/server.py @@ -14,6 +14,7 @@ import os import pathlib import re +import shutil import urllib.parse from typing import Any @@ -201,8 +202,12 @@ def _serve_file(self, project, filename): self.send_response(200) self.send_header("Content-Type", ct) + self.send_header("Content-Length", str(path.stat().st_size)) self.end_headers() - self.wfile.write(path.read_bytes()) + # stream the file instead of reading it into memory, package + # distributions can be several gigabytes in size + with path.open("rb") as fh: + shutil.copyfileobj(fh, self.wfile) def run(