Skip to content

fix: bound the MJPEG frames queued for a client that does not read them - #11

Merged
azarouski merged 1 commit into
developfrom
fix/mjpeg-stalled-clients
Oct 19, 2026
Merged

azarouski merged 1 commit into
developfrom
fix/mjpeg-stalled-clients

Conversation

@azarouski

Copy link
Copy Markdown

Problem

FBMjpegServer writes every frame to every client with writeData:withTimeout:-1. GCDAsyncSocket keeps in memory every write the socket cannot take yet, with no limit. A client that stops reading but keeps its connection open makes WDA grow by the whole stream.

This took iOS devices of MCloud offline. STF left its MJPEG request open after a user released the device (fixed in zebrunner/stf#1057). On an iPhone XR (iOS 18.7.10) WDA grew from 50 MB to 1.77 GB in 40 minutes with 2 such connections. iOS then killed most processes of the device (testmanagerd, the tunnel), and WDA could not start again until a reboot.

Fix

The same approach as upstream appium#1227, adapted to the GCDAsyncSocket server of this fork:

  • at most 4 frames in flight per client; frames above that are dropped for that client only
  • a client that has not read any frame for 60 s is disconnected and logged
  • FBTCPSocket forwards socket:didWriteDataWithTag: to its delegate (didClientWriteData:withTag:); the stream header and the frames use different tags

Verification

iOS 18.5 simulator, an animated page in Safari so that every frame changes. One client reads the stream, a second one sends the request and never reads (SO_RCVBUF 4 KB). phys_footprint of WebDriverAgentRunner-Runner:

t, s develop this PR
0 41 MB 40 MB
60 78 MB 41 MB
75 87 MB 40 MB (stalled client disconnected)
136 125 MB 40 MB

The reading client got the same number of frames in both runs (~770 frames / 79 MB in 136 s).

Log of this PR:

Disconnecting the screenshots broadcast client at 127.0.0.1:51767, it has not read any frame for 60 seconds (336 frames dropped in total)

Built with Xcode 27.1 (IPHONEOS_DEPLOYMENT_TARGET=15.0 override, the project's 13.0 is no longer accepted by that Xcode); no new warnings.

Every frame was written to every MJPEG client with no timeout and no limit.
GCDAsyncSocket keeps the writes that the socket cannot take in memory, so a
client that stopped reading without closing its connection made WDA grow by the
size of the stream (20-40 MB a minute) until iOS killed it and most processes
of the device.

- keep at most 4 frames in flight per client, drop the frames above it
- disconnect a client that has not read any frame for 60 seconds
- FBTCPSocket reports the completed writes to its delegate

The same approach as appium#1227, for the GCDAsyncSocket server
of this fork.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@azarouski
azarouski merged commit fe6dbd2 into develop Oct 19, 2026
39 of 44 checks passed
@azarouski
azarouski deleted the fix/mjpeg-stalled-clients branch October 19, 2026 13:15
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