Skip to content

Stream CR-only progress ticks in run() instead of buffering them into one message - #2731

Open
Junker der Provinz (junkerderprovinz) wants to merge 1 commit into
unraid:masterfrom
junkerderprovinz:fix/plugin-install-progress-cr-buffering
Open

Stream CR-only progress ticks in run() instead of buffering them into one message#2731
Junker der Provinz (junkerderprovinz) wants to merge 1 commit into
unraid:masterfrom
junkerderprovinz:fix/plugin-install-progress-cr-buffering

Conversation

@junkerderprovinz

@junkerderprovinz Junker der Provinz (junkerderprovinz) commented Aug 23, 2026

Copy link
Copy Markdown

Fixes the plugin-install progress garbling reported on the Unraid forum, confirmed by Jorge (@jorgeb).

The bug

emhttp/plugins/dynamix.plugin.manager/scripts/plugin's run() read subprocess output with fgets(), which only splits on \n. A command whose output uses \r-only progress ticks (no \n between them, the classic in-place progress bar) gets every tick buffered into one fgets() read that doesn't return until an actual \n shows up or the process exits. That single, already-clumped blob then goes out as one nchan message. The client's own per-message \r handling in renderMessage() (emhttp/plugins/dynamix/include/DefaultPageLayout/BodyInlineJS.php) is correct, it operates on whole received messages, but by the time a clumped message arrives there's nothing left for it to split. Reproduced live: a real install produced a single 3486-character message with 41 embedded \r.

The fix

Ports the pattern this repo already uses correctly in the sibling script multiplugin: read one character at a time with fgetc() and flush on \r or \n, so each tick becomes its own message.

 function run($command) {
   $run = popen($command,'r');
-  while (!feof($run)) write(fgets($run));
+  $line = '';
+  while (!feof($run)) {
+    $char = fgetc($run);
+    if ($char === false) break;
+    $line .= $char;
+    if (in_array($char,["\r","\n"])) {write($line); $line = '';}
+  }
+  if ($line !== '') write($line);
   return pclose($run);
 }

Only run() changes; multiplugin is untouched.

Two small, deliberate differences from multiplugin's version, both strictly better and both verified against real output:

  • An explicit $char === false break on end-of-stream, instead of relying on fgets-style implicit end-of-stream handling. This also means a stream that errors without ever setting EOF can't spin the loop forever, which the old code was theoretically exposed to.
  • A trailing if ($line !== '') write($line) after the loop, so output with no final terminator (or output that's just the literal string "0") still reaches the client. multiplugin's own !empty($line) guard would drop a bare "0", since empty("0") is true in PHP; this uses !== '' instead.

Testing

Emitted bytes are unchanged in every case tested (only the message boundaries differ): \r-only progress ticks, normal \n-terminated lines, a mix of both, output with no trailing terminator, a bare "0" with no terminator, and no output at all. Verified against the five call sites of run() in this file (pre_hooks, post_hooks, and the three run variants) that none of them depend on the old message granularity, only on the return value. php -l passes.

One pre-existing property, unchanged by this PR and already present in multiplugin: a stream that mixes \n-only lines with later \r\n-terminated lines can lose one line at the transition, because the client's renderer treats a \r-terminated message as "replace the previous line." Not introduced here, and unlikely on the Linux run-scripts this path executes (wget/curl-style progress is \r-only, not CRLF).

Summary by CodeRabbit

  • Bug Fixes
    • Improved live command output handling so progress updates display correctly when refreshed on the same line.
    • Ensured remaining output is shown even when a command finishes without a trailing newline.

During a plugin install or update the download progress line clumps into
one very long line instead of updating in place. A captured example was a
single 3486 character message holding 41 carriage returns and one trailing
line feed.

Root cause is in run(): the subprocess pipe was read with fgets(), which
only breaks on LF. A command that emits classic CR-only progress ticks
therefore keeps filling the same fgets() read until a LF finally arrives
(or EOF), and the whole progress sequence leaves run() as one message.
The receiving side splits on CR per received message, so once the ticks
are already glued together inside a single message it can no longer
render them in place.

Read the pipe character by character and flush on CR or LF, so every tick
is passed on as its own message. 'multiplugin' in the same directory
already reads its child output this way and is the reference for the
pattern. Two details are handled beyond that reference: a final chunk
without a terminator is still flushed before returning, and fgetc()
returning false at end of stream ends the loop explicitly rather than
relying on the silent false to string cast.

Return value (pclose) and the emitted bytes are unchanged; only the
message boundaries differ. Ordinary LF-only output is unaffected.
@coderabbitai

coderabbitai Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cc1596fb-cbeb-4065-9d2c-ffe699f1bb14

📥 Commits

Reviewing files that changed from the base of the PR and between 995cc85 and 85aefb0.

📒 Files selected for processing (1)
  • emhttp/plugins/dynamix.plugin.manager/scripts/plugin

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


Walkthrough

run() now reads command output character by character. It flushes output on carriage returns and newlines, then flushes any remaining output at end of stream.

Changes

Plugin output handling

Layer / File(s) Summary
Character-based output flushing
emhttp/plugins/dynamix.plugin.manager/scripts/plugin
run() buffers command output and emits it on carriage-return or newline boundaries. It also emits remaining unterminated output before closing the process.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 85aef

This localized change streams carriage-return progress updates as separate messages while preserving emitted bytes and existing return behavior; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: elibosley

Poem

A rabbit watched the progress stream,
With carrots bright and updates keen.
CR and LF made messages flow,
No trailing text was left below.
“Hop!” said Bun, “the output’s clean!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: streaming carriage-return progress updates in run().
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (1 skipped: 1 unsupported.)
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Warning

⚠️ This pull request shows signs of AI-generated slop (redundant_comments). It has been flagged by CodeRabbit slop detection and should be reviewed carefully.

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.

1 participant