Skip to content

Run each output read on its own thread, so held output can't starve new programs - #69

Merged
HalRiveria-Bot merged 2 commits into
mainfrom
fix/reader-threads
Nov 18, 2026
Merged

HalRiveria-Bot merged 2 commits into
mainfrom
fix/reader-threads

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong

Each output read ran on a thread-pool thread and blocked there until the stream closed. A program that leaves something running with its output open (a cmd /c start /b ...) keeps that read, and its pool thread, busy until the leftover lets go. Once those reads used up the pool's minimum (128 by default), a new program's reads queued behind them while the pool added a thread at a time. They hit the stream timeout and came back empty with StreamTimedOut, even for a plain echo. 3.2 behaved the same way.

What changed

  • PkStreamReadTask starts with TaskCreationOptions.LongRunning, so each read gets a thread of its own instead of a pool thread. A held stream still costs one thread until the leftover lets go; it no longer holds up other programs' output.
  • [PkState]::TypesVersion goes to 8, since the C# changed: a session that loaded an older copy has to start a new PowerShell window.
  • Initialize-PkThreadPool still raises the pool's minimum. Only its comment changed, since the reads no longer depend on it.
  • README: the StreamTimedOut paragraph says what a held stream costs.

How it was tested

New harness section AZ runs in a fresh PowerShell, because earlier sections raise the harness's own pool minimum. It starts enough programs that each leave a ping holding stdout and stderr to go past the pool's minimum: 84 at the default 128. Then it runs cmd /c echo plain with a 5 s stream timeout. Afterwards it kills only the pings its own cmds started (found by parent PID) and checks all their reads end.

On the old code (main), full harness on Windows PowerShell 5.1, only the new assertion fails:

[FAIL] a new program still gets its output well inside its stream timeout while others' output is held open (84 programs' output held open (pool minimum 128): '', StreamTimedOut=True, 5.07 s)
1 FAILED, 551 passed

Section AZ alone on the old code, PowerShell 7: the same failure ('', StreamTimedOut=True, 5.07 s).

With this change, on both runtimes:

[PASS] a new program still gets its output well inside its stream timeout while others' output is held open (84 programs' output held open (pool minimum 128): 'plain', StreamTimedOut=False, 0.05 s)
[PASS] once the holders are killed, all their reads end (DRAINED|True|168)

Full harness: Windows PowerShell 5.1 ALL PASSED (552 assertions), PowerShell 7 ALL PASSED (552 assertions).

🤖 Generated with Claude Code

HalRiveria and others added 2 commits November 18, 2026 15:14
A program left holding its output open kept a pool thread blocked per
stream. Once those reached the pool's minimum, a new program's reads
queued behind them while the pool grew a thread at a time, so they hit
the stream timeout and came back empty. PkStreamReadTask now starts with
TaskCreationOptions.LongRunning, which gives each read a dedicated thread.

TypesVersion goes to 8: the C# changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Earlier sections raise the harness's own pool minimum, and lowering it
again doesn't bring back the slow growth the test depends on, so the test
runs in a child PowerShell with the default minimum. Its holders are
found by parent PID and killed, with one overall wait for their reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HalRiveria-Bot
HalRiveria-Bot merged commit fb3de05 into main Nov 18, 2026
2 checks passed
@HalRiveria-Bot
HalRiveria-Bot deleted the fix/reader-threads branch November 18, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants