Skip to content

fix: bound memory during device output streaming (Diagnostics/backup logs)#41

Closed
marlonbarreto-git wants to merge 1 commit into
momenbasel:mainfrom
marlonbarreto-git:fix/diagnostics-log-memory
Closed

fix: bound memory during device output streaming (Diagnostics/backup logs)#41
marlonbarreto-git wants to merge 1 commit into
momenbasel:mainfrom
marlonbarreto-git:fix/diagnostics-log-memory

Conversation

@marlonbarreto-git

@marlonbarreto-git marlonbarreto-git commented Jul 5, 2026

Copy link
Copy Markdown

Summary

What changed

A single-in-flight, coalesced main-queue flush backed by two small, bounded, UTF-8-safe buffers:

  • CoalescingOutputBuffer (byte-level) — used in Shell.runStreaming and PyMobileDevice.runStreaming. Preserves raw text, including the \r progress updates emitted by idevicebackup2/pymobiledevice3, so it is safe for every caller (not just \n-based syslog). Bounds buffered bytes and holds back a multi-byte scalar split across two reads (previously String(data:encoding:.utf8) returned nil and dropped the whole chunk).
  • StreamingLineBuffer (line-level) — used in DiagnosticsManager.startSyslog to split coalesced text into whole lines with a bounded ring buffer. Fixes multi-line chunks that counted as a single entry and the 5000-"line" cap that actually bounded 5000 chunks.

Also: retain the fallback idevicesyslog Process so stopSyslog() can terminate it, and clear readability handlers on launch failure.

The key property: the number of queued main-queue blocks is bounded to one in flight, regardless of producer rate.

Flow (before → after)

sequenceDiagram
    participant Dev as Device (fast producer)
    participant BG as readabilityHandler (bg)
    participant Main as Main queue (slow consumer)
    Note over Dev,Main: BEFORE — one main.async per chunk
    Dev->>BG: chunk 1..N (thousands/sec)
    BG->>Main: main.async { onOutput } × N (unbounded backlog)
    Note over Main: backlog grows → memory exhausted
    Note over Dev,Main: AFTER — coalesced single-in-flight flush
    Dev->>BG: chunk 1..N
    BG->>BG: append to bounded buffer
    BG-->>Main: schedule ONE flush (if none pending)
    Main->>Main: drain buffer → deliver (backlog ≤ 1)
Loading

Verification

  • Flood test against the real patched Shell.runStreaming: 3,000,001 lines with a deliberately slow consumer peaks at 13 MB RSS (previously unbounded). All lines observed (no loss).
  • Unit tests for both buffers (line splitting, bounded capacity keeps newest, UTF-8 scalar split across reads, \r preservation, drain/finish) — added under Tests/PhosphorTests/.
  • Scripts/build.sh: all 32 regression checks pass; release build succeeds.

Test Plan

  • Connect a verbose device, open Diagnostics → Logs, confirm memory plateaus over several minutes.
  • Confirm backup progress (idevicebackup2/pymobiledevice3) still updates.
  • swift test (unit tests) and Scripts/build.sh (regression) green.

Live syslog/backup streaming enqueued one DispatchQueue.main.async block
per pipe chunk. When a device produced output faster than the main thread
consumed it (e.g. verbose iPhone syslog), the main-queue backlog of
un-executed blocks grew without bound -- each retaining a String --
exhausting memory (observed ~47 GB before the machine froze).

Add a single-in-flight, coalesced main-queue flush backed by two bounded,
UTF-8-safe buffers:

- CoalescingOutputBuffer: byte-level; preserves raw text including the \r
  progress updates emitted by idevicebackup2 / pymobiledevice3, so it is
  safe for every runStreaming caller. Bounds buffered bytes and holds back
  a multi-byte scalar split across two reads (previously
  String(data:encoding:.utf8) returned nil and dropped the whole chunk).
- StreamingLineBuffer: splits coalesced syslog text into whole lines with a
  bounded ring buffer, fixing multi-line chunks that counted as a single
  entry and the 5000-"line" cap that actually bounded 5000 chunks.

Also retain the fallback idevicesyslog Process so stopSyslog() can
terminate it, and clear readability handlers on launch failure.

Flood test: 3,000,001 lines through the patched Shell.runStreaming with a
deliberately slow consumer peaks at 13 MB RSS (previously unbounded).
@marlonbarreto-git marlonbarreto-git marked this pull request as ready for review July 5, 2026 22:50
@marlonbarreto-git

Copy link
Copy Markdown
Author

Hi @momenbasel @AJV20 — thanks for building Phosphor. I opened a few issues (#36#40) and a draft PR (#41) fixing #36, the unbounded-memory issue in Diagnostics/backup log streaming. #41 follows CONTRIBUTING (regression checks + build pass) and includes a flood-test showing peak RSS drop from unbounded to ~13 MB. Since #33 already reworked the syslog fallback, your eyes there would be especially welcome. Happy to adjust anything. Thanks!

@momenbasel momenbasel closed this Jul 6, 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.

Diagnostics/backup log streaming floods the main queue → unbounded memory growth

2 participants