[PR #788] [CLOSED] serve: tolerate broken stdin pipes during engine cleanup #2828

Closed
opened 2026-11-04 02:03:24 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/788
Author: @siddolo
Created: 10/4/2026
Status: ❌ Closed

Base: main ← Head: fix/serve-broken-pipe-shutdown


📝 Commits (1)

  • 2a8e5a4 serve: tolerate broken stdin pipes during engine cleanup

📊 Changes

2 files changed (+40 additions, -1 deletions)

View changed files

📝 serve/server.py (+4 -1)
📝 serve/test_lifecycle.py (+36 -0)

📄 Description

Problem

StrataEngine.close() can raise BrokenPipeError from its finally block when closing buffered stdin after the native engine has exited. Closing the stream may flush pending data again, even if an earlier flush() failure was already handled.

This was observed on Linux when stopping a custom systemd unit with KillMode=control-group, where the Python server and native engine receive SIGTERM together. The exception interrupts cleanup before stdout and the log are closed and the engine state is reset, causing an otherwise completed stop to exit with an error.

Changes

  • Handle OSError from the final stdin close once the process has exited, allowing the remaining cleanup to finish.
  • Add a regression test using a real buffered OS pipe whose reader has closed, covering both an already-exited process and a pipe failure while sending QUIT.
  • Check that stdout and the log are closed, engine state is cleared, and repeated cleanup is harmless.

The existing QUIT -> terminate -> kill sequence, EOF-before-wait behavior, and EngineStuck handling are preserved.

Validation

Both regression subcases fail with BrokenPipeError on the unmodified upstream code and pass with this fix. Validation was run on Linux with Python 3.12.3; the optional jsonschema>=4.23,<5 dependency was installed for the schema tests.

python -m unittest serve.test_lifecycle -v
# 9 tests passed

python -m unittest discover -s serve -p 'test_*.py' -q
# 269 tests: 262 passed, 7 skipped (no pack tokenizer / Windows-only)

git diff --check

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/788 **Author:** [@siddolo](https://github.com/siddolo) **Created:** 10/4/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/serve-broken-pipe-shutdown` --- ### 📝 Commits (1) - [`2a8e5a4`](https://github.com/Niko1221/Strata/commit/2a8e5a45bf0147f6a34859b7d356def0402474a0) serve: tolerate broken stdin pipes during engine cleanup ### 📊 Changes **2 files changed** (+40 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+4 -1) 📝 `serve/test_lifecycle.py` (+36 -0) </details> ### 📄 Description ## Problem `StrataEngine.close()` can raise `BrokenPipeError` from its `finally` block when closing buffered stdin after the native engine has exited. Closing the stream may flush pending data again, even if an earlier `flush()` failure was already handled. This was observed on Linux when stopping a custom systemd unit with `KillMode=control-group`, where the Python server and native engine receive SIGTERM together. The exception interrupts cleanup before stdout and the log are closed and the engine state is reset, causing an otherwise completed stop to exit with an error. ## Changes - Handle `OSError` from the final stdin close once the process has exited, allowing the remaining cleanup to finish. - Add a regression test using a real buffered OS pipe whose reader has closed, covering both an already-exited process and a pipe failure while sending QUIT. - Check that stdout and the log are closed, engine state is cleared, and repeated cleanup is harmless. The existing QUIT -> terminate -> kill sequence, EOF-before-wait behavior, and `EngineStuck` handling are preserved. ## Validation Both regression subcases fail with `BrokenPipeError` on the unmodified upstream code and pass with this fix. Validation was run on Linux with Python 3.12.3; the optional `jsonschema>=4.23,<5` dependency was installed for the schema tests. ```bash python -m unittest serve.test_lifecycle -v # 9 tests passed python -m unittest discover -s serve -p 'test_*.py' -q # 269 tests: 262 passed, 7 skipped (no pack tokenizer / Windows-only) git diff --check ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:03:24 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2828
No description provided.