Repository navigation
fix(sync): wait for 'drain' when pushing to a slow device - #575
Merged
Merged
Conversation
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]>
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.
Bug
Connection.write()returns the connection (this), not the result ofsocket.write(). So inSync._writeDatathe checkif (this.connection.write(chunk, track))was always truthy, and thewaitForDrain()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
_writeDatacheckssocket.writableNeedDrain. If the socket's buffer is full, it waits for'drain'on the socket. The check comes before the read because theSEND/DATAheaders count too.'drain'when it was set up throughconnect().'close'. The transfer then emits'error'instead of hanging.finishedand stops.drainandcloselisteners are removed.'error'listener.Connectionforwards socket errors only while it is the sole'error'listener, and the existingconnErrorListeneralready handles them.'close'always follows an error.Why not make
Connection.write()return the booleanThat's what the original PR hinted at, but it would be a breaking change:
Connectionis exported, andwrite(): thisis in the published typings, so callers may chain on it.MockConnectiondoes.Sync._sendCommandWithLengthandSync._sendCommandWithArgreturn its result typed asConnection.Asking the socket directly fixes the bug without touching the public API. A
Connectionwithout a socket (e.g. a user's mock) keeps the old behaviour.Tests
New
pushStream() to a slow devicecases intest/adb/sync.mtsuse a Duplex with a 16 KiBhighWaterMarkthat holds every write until the test releases it:drain/close/readablelisteners are left.'error'and leaves no listeners.MockConnectionnow exposes its socket assocket, like a real connection, so the existing push tests also run through the new path.All three new tests fail on master:
With the fix,
npm testgives 262 passing, 13 pending (device tests).npm run lintis 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/…'), samplingprocess.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.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-gcand a forced GC before each sample:Master retains the whole file. The fix stays flat. Throughput is unchanged.
In stf
Setup:
npm pack.sync.push) sampled withpsevery 200 ms.In both runs the activity was resumed. With this branch, the installed
base.apkmd5 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