[PR #2481] [Engine] decouple request lifecycle from blocked console output #4066

Open
opened 2026-11-07 15:32:57 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/2481
Author: @KeyanHu-git
Created: 10/14/2026
Status: 🔄 Open

Base: main ← Head: fix/2432-console-state-locks


📝 Commits (2)

  • 0dc828a [Engine] decouple request lifecycle from blocked console output
  • fa31622 [Engine] simplify diagnostic ownership and preserve planning output

📊 Changes

19 files changed (+1471 additions, -200 deletions)

View changed files

➕ docs/CONSOLE_OUTPUT.md (+39 -0)
📝 serve/cache_drop.py (+3 -1)
➕ serve/console.py (+190 -0)
📝 serve/frontend.py (+4 -2)
📝 serve/images.py (+6 -4)
📝 serve/mcp.py (+8 -7)
📝 serve/replicas.py (+6 -4)
📝 serve/server.py (+176 -154)
📝 serve/session_save_retry.py (+4 -2)
📝 serve/structured.py (+5 -4)
📝 serve/telemetry.py (+3 -1)
➕ serve/test_console.py (+509 -0)
➕ serve/test_console_handover.py (+141 -0)
➕ serve/test_console_requests.py (+299 -0)
➕ serve/test_console_support.py (+31 -0)
📝 serve/test_server.py (+17 -17)
📝 serve/test_tool_call_envelope.py (+2 -2)
📝 sycl/serve/server_intel.py (+2 -2)
➕ tests/terminal/test_console_capture.py (+26 -0)

📄 Description

Problem and result

Pausing a terminal or filling its output pipe can prevent a cancelled request from handing the engine to the next request. Completion diagnostics can also keep an HTTP admission reservation occupied after generation finishes. These writes are still inside the lifetime of request resources, even when moved outside a state lock.

This change publishes runtime diagnostics to one bounded background writer so cancellation, engine cleanup and HTTP completion can proceed while output remains blocked.

Fixes #2432. Fixes #4479. Base: 46c152a1.

Approach

flowchart LR
    R[Request execution] --> Q[Bounded diagnostic queue]
    R --> C[Engine cleanup and HTTP completion]
    Q --> W[One process-owned writer]
    W --> S[Owned output destination]

The queue keeps at most 128 pending records and 64 KiB of text, with a 4 KiB limit per record. A single progress slot replaces stale samples; request cleanup removes its pending sample by identity.

Each destination owns an unbuffered, non-inheritable descriptor. Records retain that owner until delivery or discard. Two caches reuse the current stdout/stderr destinations, so stable output does not duplicate a descriptor for each record. Closing the caller's stream or reusing its descriptor cannot redirect a delayed write. A failure affects that destination's write, without permanently disabling future stdout or stderr destinations.

Configuration helpers keep synchronous output: the CLI captures their notes immediately. Runtime restart, recovery and shutdown diagnostics use the writer. Engine traffic, model tokens, HTTP responses, ready files and structured lifecycle events retain their existing paths. Server errors retain the request peer and traceback.

before_load stdout/stderr go to a temporary file, with the last 3072 bytes published when the hook ends. Capture failures do not replace its exit status, timeout or strict-mode decision. The telemetry error diagnostic uses the same writer: a synchronous daemon print can otherwise retain Python's buffered stdout lock during interpreter shutdown.

Validation

Candidate: fa316228. Python 3.12 on macOS and Windows 11 unless noted. Service tests use actual handlers and mock engines; they load no model.

Passed:

  • Final macOS: python -m unittest serve.test_console serve.test_console_requests serve.test_console_handover tests.terminal.test_console_capture tests.terminal.test_runtime.RuntimeTests.test_foreground_jsonl_emits_ready_and_stop_without_human_stdout tests.terminal.test_ready -v — 30 run, 29 passed, one Windows-only skip; 9.943 s. Includes real CLI JSONL/quiet/signal subcases and ready-file lifecycle.
  • Final Windows: python -m unittest serve.test_console serve.test_console_requests serve.test_console_handover -v — 26 run, 25 passed, one POSIX-only skip; 5.994 s. Includes native Unicode console readback at CP437/CP936, newline behavior, and a real unread output pipe. Transferred sources were hash-checked against the submitted candidate.
  • Request tests cover cancellation during prompt/decode, seven completion/error/disconnect exits, HTTP admission release, source-stream close/FD reuse during partial writes, stable-destination descriptor reuse, and old-destination reclamation before writer shutdown.
  • The unread-pipe child completes two HTTP requests, stops a sampler whose sample method raises, and exits before its parent consumes stdout.
  • Earlier broader run: python -m unittest serve.test_console_requests serve.test_console_handover serve.test_server serve.test_tool_call_envelope — 374 passed. After the progress interface correction, the affected request/handover/ThinkingBudget subset passed 26 tests. Hook capture create/read/close failures are crossed with successful, failing and timed-out commands.
  • Complementary module regression covered MCP, structured output, frontend, images, replicas, cache release and session-save retry. In the combined 128-method run, 127 methods passed; the foreground CLI method's environment failure was resolved and rerun successfully above.
  • Independent shutdown negative control on macOS/Python 3.9: actual Telemetry._loop with synchronous output to a full pipe aborted with _enter_buffered_busy; routing the same diagnostic asynchronously exited normally.
  • python -m compileall -q serve sycl/serve/server_intel.py and git diff --check passed. The SYCL Python image-warning path was invoked separately to check the new diagnostic API.

Failed: the first foreground CLI run exited before server startup because the test virtualenv lacked yaml, psutil and numpy. After installing the repository-pinned dependencies in that test environment, the CLI/ready tests passed. No unresolved final focused-test failures.

Not run: manual QuickEdit selection, live-model execution and native CUDA/HIP/SYCL/Metal builds. No native engine source is changed. The PR remains draft for expanded-scope review and remaining platform acceptance.

Limits

Diagnostics are best-effort: long messages are truncated, overflow can drop records, and shutdown waits at most 0.2 seconds for the writer. A timeout does not close a destination still being written. Cached destinations can remain open until replaced or the writer exits. Hook output can grow its temporary file. External logging handlers and commands opening their own console are separate paths. See console diagnostics.


🔄 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/2481 **Author:** [@KeyanHu-git](https://github.com/KeyanHu-git) **Created:** 10/14/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/2432-console-state-locks` --- ### 📝 Commits (2) - [`0dc828a`](https://github.com/Niko1221/Strata/commit/0dc828a8013c6659342356bcfeb9fb4a53a96901) [Engine] decouple request lifecycle from blocked console output - [`fa31622`](https://github.com/Niko1221/Strata/commit/fa3162285faecd1902c8cb12696482bda3db4279) [Engine] simplify diagnostic ownership and preserve planning output ### 📊 Changes **19 files changed** (+1471 additions, -200 deletions) <details> <summary>View changed files</summary> ➕ `docs/CONSOLE_OUTPUT.md` (+39 -0) 📝 `serve/cache_drop.py` (+3 -1) ➕ `serve/console.py` (+190 -0) 📝 `serve/frontend.py` (+4 -2) 📝 `serve/images.py` (+6 -4) 📝 `serve/mcp.py` (+8 -7) 📝 `serve/replicas.py` (+6 -4) 📝 `serve/server.py` (+176 -154) 📝 `serve/session_save_retry.py` (+4 -2) 📝 `serve/structured.py` (+5 -4) 📝 `serve/telemetry.py` (+3 -1) ➕ `serve/test_console.py` (+509 -0) ➕ `serve/test_console_handover.py` (+141 -0) ➕ `serve/test_console_requests.py` (+299 -0) ➕ `serve/test_console_support.py` (+31 -0) 📝 `serve/test_server.py` (+17 -17) 📝 `serve/test_tool_call_envelope.py` (+2 -2) 📝 `sycl/serve/server_intel.py` (+2 -2) ➕ `tests/terminal/test_console_capture.py` (+26 -0) </details> ### 📄 Description ## Problem and result Pausing a terminal or filling its output pipe can prevent a cancelled request from handing the engine to the next request. Completion diagnostics can also keep an HTTP admission reservation occupied after generation finishes. These writes are still inside the lifetime of request resources, even when moved outside a state lock. This change publishes runtime diagnostics to one bounded background writer so cancellation, engine cleanup and HTTP completion can proceed while output remains blocked. ## Related discussion Fixes #2432. Fixes #4479. Base: `46c152a1`. ## Approach ```mermaid flowchart LR R[Request execution] --> Q[Bounded diagnostic queue] R --> C[Engine cleanup and HTTP completion] Q --> W[One process-owned writer] W --> S[Owned output destination] ``` The queue keeps at most 128 pending records and 64 KiB of text, with a 4 KiB limit per record. A single progress slot replaces stale samples; request cleanup removes its pending sample by identity. Each destination owns an unbuffered, non-inheritable descriptor. Records retain that owner until delivery or discard. Two caches reuse the current stdout/stderr destinations, so stable output does not duplicate a descriptor for each record. Closing the caller's stream or reusing its descriptor cannot redirect a delayed write. A failure affects that destination's write, without permanently disabling future stdout or stderr destinations. Configuration helpers keep synchronous output: the CLI captures their notes immediately. Runtime restart, recovery and shutdown diagnostics use the writer. Engine traffic, model tokens, HTTP responses, ready files and structured lifecycle events retain their existing paths. Server errors retain the request peer and traceback. `before_load` stdout/stderr go to a temporary file, with the last 3072 bytes published when the hook ends. Capture failures do not replace its exit status, timeout or strict-mode decision. The telemetry error diagnostic uses the same writer: a synchronous daemon print can otherwise retain Python's buffered stdout lock during interpreter shutdown. ## Validation Candidate: `fa316228`. Python 3.12 on macOS and Windows 11 unless noted. Service tests use actual handlers and mock engines; they load no model. **Passed:** - Final macOS: `python -m unittest serve.test_console serve.test_console_requests serve.test_console_handover tests.terminal.test_console_capture tests.terminal.test_runtime.RuntimeTests.test_foreground_jsonl_emits_ready_and_stop_without_human_stdout tests.terminal.test_ready -v` — **30 run, 29 passed, one Windows-only skip; 9.943 s**. Includes real CLI JSONL/quiet/signal subcases and ready-file lifecycle. - Final Windows: `python -m unittest serve.test_console serve.test_console_requests serve.test_console_handover -v` — **26 run, 25 passed, one POSIX-only skip; 5.994 s**. Includes native Unicode console readback at CP437/CP936, newline behavior, and a real unread output pipe. Transferred sources were hash-checked against the submitted candidate. - Request tests cover cancellation during prompt/decode, seven completion/error/disconnect exits, HTTP admission release, source-stream close/FD reuse during partial writes, stable-destination descriptor reuse, and old-destination reclamation before writer shutdown. - The unread-pipe child completes two HTTP requests, stops a sampler whose sample method raises, and exits before its parent consumes stdout. - Earlier broader run: `python -m unittest serve.test_console_requests serve.test_console_handover serve.test_server serve.test_tool_call_envelope` — **374 passed**. After the progress interface correction, the affected request/handover/ThinkingBudget subset passed **26 tests**. Hook capture create/read/close failures are crossed with successful, failing and timed-out commands. - Complementary module regression covered MCP, structured output, frontend, images, replicas, cache release and session-save retry. In the combined 128-method run, 127 methods passed; the foreground CLI method's environment failure was resolved and rerun successfully above. - Independent shutdown negative control on macOS/Python 3.9: actual `Telemetry._loop` with synchronous output to a full pipe aborted with `_enter_buffered_busy`; routing the same diagnostic asynchronously exited normally. - `python -m compileall -q serve sycl/serve/server_intel.py` and `git diff --check` passed. The SYCL Python image-warning path was invoked separately to check the new diagnostic API. **Failed:** the first foreground CLI run exited before server startup because the test virtualenv lacked yaml, psutil and numpy. After installing the repository-pinned dependencies in that test environment, the CLI/ready tests passed. No unresolved final focused-test failures. **Not run:** manual QuickEdit selection, live-model execution and native CUDA/HIP/SYCL/Metal builds. No native engine source is changed. The PR remains draft for expanded-scope review and remaining platform acceptance. ## Limits Diagnostics are best-effort: long messages are truncated, overflow can drop records, and shutdown waits at most 0.2 seconds for the writer. A timeout does not close a destination still being written. Cached destinations can remain open until replaced or the writer exits. Hook output can grow its temporary file. External logging handlers and commands opening their own console are separate paths. See [console diagnostics](docs/CONSOLE_OUTPUT.md). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#4066
No description provided.