fix: bound memory during device output streaming (Diagnostics/backup logs)#41
Closed
marlonbarreto-git wants to merge 1 commit into
Closed
Conversation
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).
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes Diagnostics/backup log streaming floods the main queue → unbounded memory growth #36. Live syslog/backup streaming enqueued one
DispatchQueue.main.asyncblock per pipe chunk from a backgroundreadabilityHandler. 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 aString— exhausting memory (observed tens of GB before the machine froze).What changed
A single-in-flight, coalesced main-queue flush backed by two small, bounded, UTF-8-safe buffers:
CoalescingOutputBuffer(byte-level) — used inShell.runStreamingandPyMobileDevice.runStreaming. Preserves raw text, including the\rprogress updates emitted byidevicebackup2/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 (previouslyString(data:encoding:.utf8)returned nil and dropped the whole chunk).StreamingLineBuffer(line-level) — used inDiagnosticsManager.startSyslogto split coalesced text into whole lines with a bounded ring buffer. Fixes multi-line chunks that counted as a single entry and the5000-"line" cap that actually bounded 5000 chunks.Also: retain the fallback
idevicesyslogProcesssostopSyslog()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)Verification
Shell.runStreaming: 3,000,001 lines with a deliberately slow consumer peaks at 13 MB RSS (previously unbounded). All lines observed (no loss).\rpreservation, drain/finish) — added underTests/PhosphorTests/.Scripts/build.sh: all 32 regression checks pass; release build succeeds.Test Plan
idevicebackup2/pymobiledevice3) still updates.swift test(unit tests) andScripts/build.sh(regression) green.