serve: tolerate broken stdin pipes during engine cleanup

This commit is contained in:
siddolo authored and Niko1221 committed 2026-10-05 13:16:53 +02:00
1 parent a42a0c0b00
commit 773657cd1f
2 files changed
+40 -1

No files matched your search

+4 -1
View File
@@ -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()
+36
View File
@@ -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()