mcp: a server that stops quotes its last log line reliably (the stdout reader waited no time for the stderr reader, so the reason was sometimes missing - the flaky test_a_server_that_fails_is_left_out)

This commit is contained in:
Niko1221 committed 2026-09-30 22:13:46 +02:00
1 parent 912a931f0f
commit 7dc1fae80b
1 file changed
+5 -1
+5 -1
View File
@@ -108,8 +108,9 @@ class StdioTransport:
except OSError as e:
raise McpError(f"could not start {command!r}: {e}") from None
contain(self.proc) # ends with the server, however it ends (Windows)
self._err_reader = threading.Thread(target=self._read_stderr, daemon=True)
self._err_reader.start()
threading.Thread(target=self._read, daemon=True).start()
threading.Thread(target=self._read_stderr, daemon=True).start()
def alive(self) -> bool:
return self.proc is not None and self.proc.poll() is None and not getattr(self, "ended", False)
@@ -144,6 +145,9 @@ class StdioTransport:
if isinstance(m, dict):
self._dispatch(m)
self.ended = True
# the server's last log line says why it stopped: let the stderr reader take it first (the pipe closes with
# the process), or the error raced it and said only "the server stopped"
self._err_reader.join(1.0)
code = self.proc.poll()
err = McpError(f"the server stopped{f' (exit code {code})' if code is not None else ''}{self._tail()}")
with self.lock: