Skip to content

fix: prevent SSHTransport busy-loop on partial packets#178

Closed
vicajilau wants to merge 1 commit into
TerminalStudio:masterfrom
vicajilau:fix/transport-partial-packet-busyloop
Closed

fix: prevent SSHTransport busy-loop on partial packets#178
vicajilau wants to merge 1 commit into
TerminalStudio:masterfrom
vicajilau:fix/transport-partial-packet-busyloop

Conversation

@vicajilau

Copy link
Copy Markdown
Collaborator

Fixes #177.

SSHTransport._scheduleProcessData busy-loops at 100% CPU whenever a partial packet is left in the read buffer, permanently starving the event loop (and the socket reads that would complete the packet). On Flutter this pegs the platform thread and produces an "Input dispatching timed out" ANR / frozen UI.

Root Cause

_processPackets returns as soon as _consumePacket() cannot form a complete packet. But whenComplete reschedules whenever _buffer.isNotEmpty, even when the pass consumed nothing. The reschedule runs as a microtask; microtasks have priority over I/O events, so the loop starves _onSocketData — the additional bytes needed to complete the packet are never delivered, and it spins forever.

Fix

Only reschedule when the pass actually made progress (a packet was consumed) or new bytes arrived while processing (tracked by a new _hasNewData flag).

Also added a unit test to verify that the event loop is not starved when receiving a partial packet.

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 54.84%. Comparing base (1d3ed4a) to head (1c59654).

Files with missing lines Patch % Lines
lib/src/ssh_transport.dart 80.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #178      +/-   ##
==========================================
+ Coverage   54.67%   54.84%   +0.16%     
==========================================
  Files          66       66              
  Lines        5419     5423       +4     
==========================================
+ Hits         2963     2974      +11     
+ Misses       2456     2449       -7     
Flag Coverage Δ
unittests 54.84% <80.00%> (+0.16%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
lib/src/ssh_transport.dart 52.42% <80.00%> (+1.49%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vicajilau vicajilau closed this Jul 2, 2026
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.

SSHTransport busy-loops (100% CPU / ANR) on a partial packet in the read buffer

1 participant