Repository navigation
Description
AsyncPreFormApi reads the launched PreFormServer's stdout with StreamReader.readline(), both in _wait_until_ready and in the _drain task that keeps the pipe empty after startup. readline() raises ValueError ("Separator is found, but chunk is longer than limit") when a single line is longer than the stream limit, 64 KiB by default.
If the server ever prints a line that long (a big JSON log record, for example):
- The
_draintask dies with thatValueError, so nothing reads stdout anymore. Once the pipe fills, the nextwriteto stdout in PreFormServer blocks. With a stand-in server that logs one line per request before answering, calls start timing out after about 90 requests. aclose()then re-raises theValueErrorwhen it awaits the drain task (it only suppressesCancelledError), so leavingasync with AsyncFormlabsClient()raises.- A line that long before
READY FOR INPUTmakeslaunch()raise a bareValueErrorinstead of aPreFormApiError.
Repro
formlabs-sdk v1.0.0-alpha.post2, Python 3.12, macOS. A stand-in for PreFormServer that answers GET /v1/, logs about 2 KiB per request before replying, and prints one 100 KB line right after READY FOR INPUT:
#!/usr/bin/env python3
# fake_server.py (chmod +x)
import http.server, json, sys
port = int(sys.argv[sys.argv.index("--port") + 1])
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
sys.stdout.write("request " + "x" * 2048 + "\n"); sys.stdout.flush()
body = json.dumps({"version": "3.64.2"}).encode()
self.send_response(200); self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body))); self.end_headers(); self.wfile.write(body)
def log_message(self, *a): pass
srv = http.server.ThreadingHTTPServer(("localhost", port), H)
print("READY FOR INPUT", flush=True)
print('{"log": "' + "y" * 100_000 + '"}', flush=True)
srv.serve_forever()import asyncio
from formlabs_sdk import AsyncFormlabsClient
from formlabs_preform_api.api.server_info import get_server_version
async def main():
async with AsyncFormlabsClient() as client:
preform = await client.preform.launch("./fake_server.py", port=44399, timeout=5)
for i in range(300):
await preform.call(get_server_version)
asyncio.run(main())Without the long line, all 300 calls succeed and the client closes cleanly. With it, the loop stops partway through (around call 95 here) with httpx.ReadTimeout, and the close raises ValueError: Separator is found, but chunk is longer than limit.
Possible fix
readline() has already discarded the oversized line by the time it raises, so the reader can just keep going. This is the change I tested locally; with it the repro above makes all 300 calls and closes cleanly, and a 1 MB line or a long line before READY FOR INPUT no longer breaks _drain or launch(). mypy reports the same output before and after.
diff --git a/formlabs_sdk/preform_api.py b/formlabs_sdk/preform_api.py
index 87f6fa2..8ccc090 100644
--- a/formlabs_sdk/preform_api.py
+++ b/formlabs_sdk/preform_api.py
@@ -111,6 +111,20 @@ def _process_env(
return merged
+async def _readline(stream: asyncio.StreamReader) -> bytes:
+ """``stream.readline()``, but a line longer than the stream's buffer limit
+ (64 KiB by default) does not end the read.
+
+ ``readline()`` raises ``ValueError`` for such a line after discarding it;
+ letting that escape would stop the drain, after which the server blocks
+ as soon as the stdout pipe fills. The line is replaced by a marker.
+ """
+ try:
+ return await stream.readline()
+ except ValueError:
+ return b"[PreFormServer output line longer than the read limit was dropped]\n"
+
+
class AsyncPreFormApi(AsyncPreFormApiFacade):
"""Handle to the PreForm API of a PreFormServer process.
@@ -617,7 +631,7 @@ class AsyncPreFormApi(AsyncPreFormApiFacade):
) -> None:
assert process.stdout is not None
while True:
- raw = await process.stdout.readline()
+ raw = await _readline(process.stdout)
if not raw:
raise PreFormApiError(
"STARTUP_FAILED",
@@ -639,7 +653,7 @@ class AsyncPreFormApi(AsyncPreFormApiFacade):
async def _drain(stream: asyncio.StreamReader, tail: deque[str]) -> None:
# Keep the stdout pipe from filling up; retain a bounded tail.
while True:
- raw = await stream.readline()
+ raw = await _readline(stream)
if not raw:
return
tail.append(raw.decode(errors="replace"))Happy to open this as a PR if that's easier for you, but since the SDK is published from your release pipeline I figured an issue was the better place.
Activity