Skip to content

fix(sync): wait for 'drain' when pushing to a slow device - #575

Merged
koral-- merged 1 commit into
masterfrom
fix/sync-push-backpressure
Oct 30, 2026
Merged

koral-- merged 1 commit into
masterfrom
fix/sync-push-backpressure

Conversation

@koral--

@koral-- koral-- commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Bug

Connection.write() returns the connection (this), not the result of socket.write(). So in Sync._writeData the check if (this.connection.write(chunk, track)) was always truthy, and the waitForDrain() branch has been dead code since the CoffeeScript days. push() had no backpressure. The whole source stream was read as fast as it could be read and buffered in the socket's write buffer.

stf installs every APK with sync.push(), streaming it from its storage unit. So a large APK pushed to a slow device grew the device worker's memory by about the APK's size, per concurrent install.

Fix

  • Before it reads each chunk from the source, _writeData checks socket.writableNeedDrain. If the socket's buffer is full, it waits for 'drain' on the socket. The check comes before the read because the SEND/DATA headers count too.
  • The drain wait listens on the socket itself, not on the connection. The connection only forwards 'drain' when it was set up through connect().
  • The wait also ends when:
    • the socket emits 'close'. The transfer then emits 'error' instead of hanging.
    • the transfer fails or is cancelled. The wait settles, so the writer chain sees finished and stops.
  • In every case the drain and close listeners are removed.
  • It deliberately doesn't add a socket 'error' listener. Connection forwards socket errors only while it is the sole 'error' listener, and the existing connErrorListener already handles them. 'close' always follows an error.

Why not make Connection.write() return the boolean

That's what the original PR hinted at, but it would be a breaking change:

  • Connection is exported, and write(): this is in the published typings, so callers may chain on it.
  • Subclasses override it with that signature; the test MockConnection does.
  • Internally, Sync._sendCommandWithLength and Sync._sendCommandWithArg return its result typed as Connection.

Asking the socket directly fixes the bug without touching the public API. A Connection without a socket (e.g. a user's mock) keeps the old behaviour.

Tests

New pushStream() to a slow device cases in test/adb/sync.mts use a Duplex with a 16 KiB highWaterMark that holds every write until the test releases it:

  • waits for 'drain': pushes 1 MiB. While the device is stuck, at most one 64 KiB chunk plus headers is buffered. After release, the transfer completes with all bytes and leaves no listeners.
  • cancel while waiting for 'drain': the transfer ends, and no drain/close/readable listeners are left.
  • socket closes while waiting for 'drain': the transfer emits 'error' and leaves no listeners.

MockConnection now exposes its socket as socket, like a real connection, so the existing push tests also run through the new path.

All three new tests fail on master:

AssertionError: expected 1048730 to be below 131072      (whole 1 MiB buffered)
AssertionError: expected +0 to be above +0               (never waits for drain) x2

With the fix, npm test gives 262 passing, 13 pending (device tests). npm run lint is clean.

End-to-end on an emulator

Setup: API 31 arm64 emulator. A 400 MB random file pushed with client.getDevice(serial).push(file, '/data/local/tmp/…'), sampling process.memoryUsage() every 20 ms. "master" is a build of 2860ed9, "fix" is this branch. The md5 on the device (adb shell md5sum) matched the source on every run.

run build time peak RSS peak ArrayBuffers
unthrottled master 1.24 s / 1.14 s 417 MB / 416 MB 384 MB / 382 MB
unthrottled fix 1.15 s / 1.20 s 92 MB / 92 MB 56 MB / 53 MB
20 MB/s TCP proxy before adb server master 23.6 s 435 MB 400 MB
20 MB/s TCP proxy before adb server fix 22.5 s 98 MB 57 MB

The fix's ~55 MB is the same whether throttled or not, which looks like 64 KiB chunks waiting for GC rather than retained buffers. To check, I re-ran with --expose-gc and a forced GC before each sample:

file build peak RSS peak ArrayBuffers
100 MB master 123 MB 95 MB
100 MB fix 37 MB 0.3 MB
400 MB master 414 MB 380 MB
400 MB fix 37 MB 6.6 MB

Master retains the whole file. The fix stays flat. Throughput is unchanged.

In stf

Setup:

  • Fresh local stf master with this branch installed via npm pack.
  • A 300 MB launchable test APK (a code-less app plus a 300 MB stored asset, debug-signed).
  • Uploaded through the control page's App Upload box with Playwright.
  • RSS of the device worker process (the one calling sync.push) sampled with ps every 200 ms.
adbkit device worker RSS (idle → peak) result
4.0.0 from npm 168 MB → 465 MB installed and launched
this branch 139 MB → 175 MB installed and launched

In both runs the activity was resumed. With this branch, the installed base.apk md5 on the device matched the uploaded APK.

There's no Unreleased section in CHANGELOG.md (it's generated at release), so I didn't add an entry.

Supersedes #542, thanks @harlentan.

🤖 Generated with Claude Code

Connection.write() returns the connection, so the check in
Sync._writeData was always truthy and its waitForDrain() branch never
ran. A push had no backpressure: the whole source stream ended up in the
socket's write buffer, so pushing a large APK to a slow device grew the
process's memory by about the file size.

Sync now checks the socket's writableNeedDrain before it reads each
chunk and waits for 'drain' on the socket. The wait also ends when the
socket closes, or when the transfer fails or is cancelled, and removes
its listeners. Connection.write() keeps returning the connection so the
public API doesn't change.

Co-authored-by: Harlen Tan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
@koral--
koral-- merged commit 398b53f into master Oct 30, 2026
6 checks passed
@koral--
koral-- deleted the fix/sync-push-backpressure branch October 30, 2026 23:40
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