Repository navigation
fix(sync): emit 'error' then 'close', not 'end', when a pull or push fails - #579
Merged
Merged
Conversation
When adb answered a RECV with FAIL (or with an unexpected reply), the pull transfer was ended first and the error was read and emitted only afterwards. A consumer saw 'end', 'finish' and 'close', as if the file had arrived complete (empty for a missing file), and only then 'error'. stream.promises.finished() and pipeline() resolved before the error. The transfer now reads the error first, emits 'error' and then 'close', and never 'end'. It is emitted, not passed to destroy(err), so a pull nobody listens to still only warns instead of crashing the process. device.pull() now closes the sync connection on 'close', which follows both 'end' and 'error', instead of only on 'end'. Fixes #464 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Karol Wrótniak <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Karol Wrótniak <[email protected]>
A failed push emitted 'error' and then 'end', because the reply chain always ended the transfer in finally. PushTransfer now emits 'error' and then 'close' on a failure, and 'end' and then 'close' on success or cancel. device.push() closes the sync connection on 'close', so it also runs after a failure. pushFile() closes the file it opened when the push stops before reading it to the end; a failed 20 MB push left it open. When the consumer destroyed a pull transfer before it finished, the read loop then failed and its error was emitted with nobody listening, so warnUnhandled printed a warning for an abort the consumer asked for. A destroy now stops the read loop and only cleans up. On a destroy or a cancel, the connection is also drained after it is ended: with unread data (from /dev/urandom, say) the socket never saw the end and stayed open. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Karol Wrótniak <[email protected]>
The pull example piped the transfer into a file with pipe(), which leaves the file open when the pull fails. It now uses stream/promises' pipeline(), which rejects and closes the file. The pull and push examples call device.pull()/push(), as the client has no pull()/push() of its own, and the push example's log line is a template string again. PushTransfer's events list 'close', and both transfers say that a cancel ends them and a failure doesn't. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Karol Wrótniak <[email protected]>
After reading a FAIL, _readError() ends the socket in a finally(). A push that is still sending data then writes to the ended socket, the socket fails with 'write after end', parser.end() rejects with that error, and the rejection from finally() replaces the FailError. On API 36, pushing into a read-only directory reported 'write after end' in 17 of 40 runs. Ignore parser.end()'s error there: the connection is given up anyway, and the FAIL says what went wrong. 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.
Problem
(Pull first, then push; see the review follow-up below.)
When adb answers a pull (
RECV) withFAIL,Sync._readData()ended the transfer first and only then read and emitted the error. A consumer gotend,finishandclose, as if the file had arrived complete (empty, for a missing or unreadable file), and only after thaterror.stream.promises.finished()andpipeline()resolved before the error arrived, so a failed pull looked like a successful empty one. An unexpected reply had the same problem.Fix
FAIL(or an unexpected reply), the transfer reads the error, emitserrorand thenclose, and neverendorfinish.DONEandcancel()still end the transfer as before. The README already documents that a cancelled pull ends.destroy(err). A pull that nobody listens to forerrorstill only gets a warning (throughwarnUnhandled), as on master. Withdestroy(err), the error would have been emitted on the next tick with no listener, and the process would have crashed.device.pull()closed the sync connection only onend. It now closes it onclose, which follows bothendanderror.erroris followed byclose, not byend.Behavior change: code that waits only for
end(e.g.new Promise((r) => t.on('end', r))) used to resolve on a missing file and now doesn't. It has to listen forerrortoo, or usefinished()/pipeline(). That's the point of the fix, but it's visible.The Bluebird "a promise was created in a handler" warning mentioned in the issue comments no longer applies: 4.0 dropped Bluebird.
Fixes #464
Verification
Unit tests (new
pull() without a devicesuite intest/adb/sync.mts, two intest/adb/DeviceClient.mts): FAIL with no data, DATA then FAIL (the data is delivered, thenerror, noend), unexpected reply,finished()rejecting, DONE still ending, FAIL without anerrorlistener only warning, anddevice.pull()ending the sync connection on success and on failure. On master, the first four fail with events['end', 'finish', 'close', 'error'](or['error', 'end', 'finish', 'close']for the unexpected reply), andfinished()resolves.npm test: 292 passing.npm run lintis clean.Emulators (API 30
emulator-5620and API 36emulator-5622, non-root):device.stat()and thendevice.pull(), with the transfer's events in order.finished()is attached before any data arrives.fec53db)/system/build.prop(stat OK, mode 600)end -> finish -> close -> error(open failed: Permission denied),finished()resolvederror(open failed: Permission denied) -> close,finished()rejected with that error/non-existing-pathend -> finish -> close -> error(open failed: No such file or directory), resolvederror(...) -> close, rejected/data/local/tmp(a directory)end -> finish -> close -> error(read failed: Is a directory), resolvederror(...) -> close, rejected/system/etc/hostsend -> finish -> close, 56 bytes, resolvedThe results are the same on both API levels. 20 failing and 20 successful
device.pull()calls in one process left 0 sockets open to the adb server, on master and on this branch.A FAIL after some DATA is covered by the unit test only. I couldn't make the emulator fail partway through a read: its
dmctlhas noerrororzerotarget.stf (DeviceFarmer/stf master,
stf local emulator-5620, adbkit installed from this branch'snpm pack, File Explorer driven with Playwright). This is a no-regression check, not a repro: stf already turned these failures into a fail reply on master. It callsstatfirst, and its upload is still open when the lateerrorarrives./system/build.propand/proc/self/mem(stat size 0): "Failed to download file: form-data: Failure: 'open failed: Permission denied'" in the UI. Thetx.donehassuccess: false, the same as on master./system/etc/hosts: downloaded, 56 bytes, the same as on master.Review follow-up (commits
cf25434,8908943)Push has the same fix now. A failed push emitted
errorand thenend, because the reply chain always calledtransfer.end()infinally.PushTransfernow emits:errorthencloseon a failure, with noend;endthencloseon success or cancel, as before plus the newclose.device.push()closes the sync connection onclose, so the cleanup also runs after a failure.device.install()still rejects onerror.Cleanup fixes found while verifying. These were already broken on master:
pushFile()now closes the file it opened when the push stops before reading it to the end. A failed 20 MB push to/systemleft the local file open: 2 fds on the previous head, 0 now.cancel()or destroy, the connection is drained after it is ended. Without that, unread data from/dev/urandomkept the socket to the adb server open: 3 of 3 cancels left one open on master, 0 now. Cancel still ends the transfer, as before.An early destroy is no longer reported. If the consumer calls
transfer.destroy()before the pull finishes, the read loop stops and cleans up without emitting. Before, it emitted aPrematureEOFError, which became a warning when nobody listened forerror.README:
stream/promises'pipeline();pipe()left the file open on a failed pull.device.pull()/device.push();client.pull(serial, …)doesn't exist.close.The comment on
'end'now says it means the whole file arrived or the transfer was cancelled.Verification of the follow-up
Unit tests:
npm test300 passing, lint clean. They cover:['end', 'close'];['error', 'close'];['error', 'close'];['end', 'close'];pushFile()closes withoutendand only warns;pushFile()closes its file on a FAIL (this test fails without the fix);device.push()ends the sync connection on success and on failure;Emulators (API 30
emulator-5620and API 36emulator-5622):import { Adb }, devices filtered to mine):/system/build.prop, mode 600: "Something went wrong: FailError: Failure: 'open failed: Permission denied'";/system/etc/hosts: 56 bytes each;pipe()form left the write stream open (1 fd) after the failure./system/adbkitfu.bin:error(couldn't create file: Read-only file system) -> close;/data/local/tmp:end -> close;error -> endfor the failure./data/local/tmp, rejects to/system. 0 files or sockets are left open, including with a 20 MB file./dev/urandomwith noerrorlistener: on the previous head, a warningPrematureEOFError: Premature end of stream. Now no warning, noerror, and 0 sockets left open. The same holds for/system/framework/framework.jar.stf local emulator-5620, this branch'snpm pack):/system/build.propand/proc/self/memshow "Failed to download file";/system/etc/hostsdownloads 56 bytes.minicap.apkinstalled, andpm list packagesshowsio.devicefarmer.minicap. stf's install plugin usesadb.push()with a stream.Also fixed (bedccc9): on API 36, a push into a missing directory sometimes reported
write after endinstead of adbd'ssecure_mkdirs() failed: Read-only file system. After reading a FAIL,_readError()ends the socket in afinally(); a push still writing then fails thatend(), and the rejection fromfinally()replaced theFailError. It happened on master too. Before the fix, 17 of 40 runs (100 B and 4 MiB files) reportedwrite after end; after it, 60 of 60 report the device's error. A new unit test covers it.🤖 Generated with Claude Code