Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: tolerate broken stdin pipes during engine cleanup

siddolo773657cd1f99
diff --git a/serve/server.py b/serve/server.py
index a23f7952e..39685f88e 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -1279,7 +1279,10 @@ class StrataEngine:
if self.proc.poll() is not None:
if self.pump is not None:
self.pump.join(timeout=2)
- self.proc.stdin.close()
+ try:
+ self.proc.stdin.close() # buffered data may flush again after the engine has exited
+ except OSError:
+ pass
self.proc.stdout.close()
if self.log not in (None, subprocess.DEVNULL):
self.log.close()
diff --git a/serve/test_lifecycle.py b/serve/test_lifecycle.py
index 1d9f58c46..84847b59d 100644
--- a/serve/test_lifecycle.py
+++ b/serve/test_lifecycle.py
@@ -1,6 +1,7 @@
"""Model discovery, on-demand loading and safe unloading over the actual HTTP API."""
import json
import io
+import os
import subprocess
import time
import unittest
@@ -45,6 +46,41 @@ class ResidentEngine(StrataEngine):
class Lifecycle(unittest.TestCase):
+ def test_close_cleans_up_after_a_broken_stdin_pipe(self):
+ for running in (False, True):
+ with self.subTest(running=running):
+ engine = StrataEngine("missing-executable", [], lazy=True)
+ read_fd, write_fd = os.pipe()
+ stdin = os.fdopen(write_fd, "w")
+ self.addCleanup(stdin.close)
+ os.close(read_fd) # the engine's end of the pipe is already gone
+ stdin.write("pending") # buffered: flush() and close() will hit the broken pipe
+ proc = mock.Mock(stdin=stdin, stdout=io.StringIO())
+ self.addCleanup(proc.stdout.close)
+ proc.poll.side_effect = [None if running else 0, 0]
+ engine.proc, engine.pump, engine.log = proc, mock.Mock(), io.StringIO()
+ self.addCleanup(engine.log.close)
+ engine.ended, engine.progress, engine.last = False, (1, 2), {"generated": 1}
+
+ engine.close()
+
+ self.assertTrue(stdin.closed)
+ self.assertTrue(proc.stdout.closed)
+ self.assertTrue(engine.log.closed)
+ engine.pump.join.assert_called_once_with(timeout=2)
+ self.assertIsNone(engine.proc)
+ self.assertTrue(engine.ended)
+ self.assertIsNone(engine.progress)
+ self.assertEqual(engine.last, {})
+ if running:
+ proc.terminate.assert_called_once()
+ proc.wait.assert_called_once_with(timeout=20)
+ else:
+ proc.terminate.assert_not_called()
+ proc.wait.assert_not_called()
+ proc.kill.assert_not_called()
+ engine.close() # repeated cleanup is harmless
+
def test_close_sends_eof_before_waiting_for_windows_reader(self):
engine = StrataEngine("missing-executable", [], lazy=True)
proc = mock.Mock()