Skip to content

fix(sync): emit 'error' then 'close', not 'end', when a pull or push fails - #579

Merged
koral-- merged 5 commits into
masterfrom
fix/pull-error-order
Nov 7, 2026
Merged

koral-- merged 5 commits into
masterfrom
fix/pull-error-order

Conversation

@koral--

@koral-- koral-- commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Problem

(Pull first, then push; see the review follow-up below.)

When adb answers a pull (RECV) with FAIL, Sync._readData() ended the transfer first and only then read and emitted the error. A consumer got end, finish and close, as if the file had arrived complete (empty, for a missing or unreadable file), and only after that error. stream.promises.finished() and pipeline() resolved before the error arrived, so a failed pull looked like a successful empty one. An unexpected reply had the same problem.

Fix

  • On FAIL (or an unexpected reply), the transfer reads the error, emits error and then close, and never end or finish. DONE and cancel() still end the transfer as before. The README already documents that a cancelled pull ends.
  • The error is emitted and the transfer is then destroyed without an error, instead of calling destroy(err). A pull that nobody listens to for error still only gets a warning (through warnUnhandled), as on master. With destroy(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 on end. It now closes it on close, which follows both end and error.
  • The README's PullTransfer events now say that error is followed by close, not by end.

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 for error too, or use finished()/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 device suite in test/adb/sync.mts, two in test/adb/DeviceClient.mts): FAIL with no data, DATA then FAIL (the data is delivered, then error, no end), unexpected reply, finished() rejecting, DONE still ending, FAIL without an error listener only warning, and device.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), and finished() resolves. npm test: 292 passing. npm run lint is clean.

Emulators (API 30 emulator-5620 and API 36 emulator-5622, non-root): device.stat() and then device.pull(), with the transfer's events in order. finished() is attached before any data arrives.

path master (fec53db) this branch
/system/build.prop (stat OK, mode 600) end -> finish -> close -> error(open failed: Permission denied), finished() resolved error(open failed: Permission denied) -> close, finished() rejected with that error
/non-existing-path end -> finish -> close -> error(open failed: No such file or directory), resolved error(...) -> close, rejected
/data/local/tmp (a directory) end -> finish -> close -> error(read failed: Is a directory), resolved error(...) -> close, rejected
/system/etc/hosts end -> finish -> close, 56 bytes, resolved same

The 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 dmctl has no error or zero target.

stf (DeviceFarmer/stf master, stf local emulator-5620, adbkit installed from this branch's npm 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 calls stat first, and its upload is still open when the late error arrives.

  • /system/build.prop and /proc/self/mem (stat size 0): "Failed to download file: form-data: Failure: 'open failed: Permission denied'" in the UI. The tx.done has success: false, the same as on master.
  • /system/etc/hosts: downloaded, 56 bytes, the same as on master.
  • Screenshot (stf pulls the minicap JPEG): stf's log shows "Capturing screenshot" and then "Uploaded to /s/image/…". My Playwright selector for the image in the UI didn't match, so only the log confirms this one.

Review follow-up (commits cf25434, 8908943)

Push has the same fix now. A failed push emitted error and then end, because the reply chain always called transfer.end() in finally. PushTransfer now emits:

  • error then close on a failure, with no end;
  • end then close on success or cancel, as before plus the new close.

device.push() closes the sync connection on close, so the cleanup also runs after a failure. device.install() still rejects on error.

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 /system left the local file open: 2 fds on the previous head, 0 now.
  • On a pull cancel() or destroy, the connection is drained after it is ended. Without that, unread data from /dev/urandom kept 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 a PrematureEOFError, which became a warning when nobody listened for error.

README:

  • The pull example uses stream/promises' pipeline(); pipe() left the file open on a failed pull.
  • The pull and push examples call device.pull()/device.push(); client.pull(serial, …) doesn't exist.
  • The PushTransfer events list close.
  • Both transfers say that a cancel ends them and a failure doesn't.

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 test 300 passing, lint clean. They cover:

  • push success ['end', 'close'];
  • device FAIL ['error', 'close'];
  • source stream error ['error', 'close'];
  • cancel ['end', 'close'];
  • a missing pushFile() closes without end and 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;
  • an early-destroyed pull neither emits nor warns, even with a FAIL right behind the first chunk;
  • the fix(sync): wait for 'drain' when pushing to a slow device #575 drain tests still pass.

Emulators (API 30 emulator-5620 and API 36 emulator-5622):

  • README pull example, run as written (import { Adb }, devices filtered to mine):
    • failing on /system/build.prop, mode 600: "Something went wrong: FailError: Failure: 'open failed: Permission denied'";
    • succeeding with /system/etc/hosts: 56 bytes each;
    • both runs leave 0 pulled files or adb sockets open and print no warnings.
    • The old pipe() form left the write stream open (1 fd) after the failure.
  • Push events:
    • to /system/adbkitfu.bin: error(couldn't create file: Read-only file system) -> close;
    • to /data/local/tmp: end -> close;
    • master gave error -> end for the failure.
    • A 100 MiB push grew peak RSS by 29 MiB (API 30) and 47 MiB (API 36), so fix(sync): wait for 'drain' when pushing to a slow device #575's backpressure still works.
  • README push example: succeeds to /data/local/tmp, rejects to /system. 0 files or sockets are left open, including with a 20 MB file.
  • Early-destroyed pull of /dev/urandom with no error listener: on the previous head, a warning PrematureEOFError: Premature end of stream. Now no warning, no error, and 0 sockets left open. The same holds for /system/framework/framework.jar.
  • stf (master, stf local emulator-5620, this branch's npm pack):
    • File Explorer: /system/build.prop and /proc/self/mem show "Failed to download file"; /system/etc/hosts downloads 56 bytes.
    • APK upload in the dashboard: minicap.apk installed, and pm list packages shows io.devicefarmer.minicap. stf's install plugin uses adb.push() with a stream.

Also fixed (bedccc9): on API 36, a push into a missing directory sometimes reported write after end instead of adbd's secure_mkdirs() failed: Read-only file system. After reading a FAIL, _readError() ends the socket in a finally(); a push still writing then fails that end(), and the rejection from finally() replaced the FailError. It happened on master too. Before the fix, 17 of 40 runs (100 B and 4 MiB files) reported write after end; after it, 60 of 60 report the device's error. A new unit test covers it.

🤖 Generated with Claude Code

koral-- and others added 4 commits November 7, 2026 00:25
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]>
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]>
@koral-- koral-- changed the title fix(sync): emit pull errors instead of 'end' when adb reports FAIL fix(sync): emit 'error' then 'close', not 'end', when a pull or push fails Nov 7, 2026
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]>
@koral--
koral-- merged commit 807faf9 into master Nov 7, 2026
6 checks passed
@koral--
koral-- deleted the fix/pull-error-order branch November 7, 2026 12:07
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.

errors in PullTransfers are issued after end (or close)

1 participant