Skip to content

fix(tcpusb): ack A_WRTE only after the device side took the bytes - #600

Merged
koral-- merged 1 commit into
DeviceFarmer:masterfrom
dlstjd0237:fix/tcpusb-write-backpressure
Nov 26, 2026
Merged

koral-- merged 1 commit into
DeviceFarmer:masterfrom
dlstjd0237:fix/tcpusb-write-backpressure

Conversation

@dlstjd0237

Copy link
Copy Markdown

Bug

Without delayed_ack, Service._handleWritePacket writes the client's data to the device transport and sends A_OKAY right away:

if (!this.window.delayed) {
  this.transport.write(data);
  return this.socket.write(Packet.assemble(Packet.A_OKAY, this.localId, this.remoteId, null));
}

A_OKAY is what lets the client send its next A_WRTE. So the client is never held back: when it sends faster than the device takes the data, everything the device has not taken yet piles up in the bridge process, and the memory is not handed back to the OS afterwards.

The delayed_ack path right below already waits for the write callback ("so a slow device pushes back"), and #575 fixed the same kind of problem for sync.push(). This is the same thing on the TcpUsb bridge, for clients without delayed_ack (older adb clients).

How it shows up

stf runs one bridge per device for "Remote debug" (adb connect). Pushing a 2 GB file through it from a client on a fast network to a device on USB:

Bridge process RSS Transfer rate
Before 245 MB → 1,299 MB, stays there after the push 49.4 MB/s (faster than the device takes it)
After 108 MB → 133 MB at most, flat during the push 29.9 MB/s (the device's own rate)

The file arrived intact in both cases. With large repeated uploads (a load test pushing several GB), single bridge processes reached ~4 GB, and several at once took a host into the OOM killer.

I measured this on the 3.3.x bridge, whose non-delayed_ack write path is the same code as on master. The unit tests below exercise master.

Fix

Use the delayed_ack path for both cases. SendWindow.ack() returns no payload without delayed_ack, so the A_OKAY the client gets is unchanged; it is only sent once the device side has taken the bytes. A failed write ends the stream, as on the delayed_ack path.

Tests

New test/adb/tcpusb/service.mts:

  • should hold the A_OKAY until the device side has taken the bytes: no A_OKAY before the transport's write callback, then one plain A_OKAY with the right ids.
  • should close the stream instead of acking when the write fails.

Both fail without the fix. npm test: 399 passing. npm run lint: clean.

Without delayed_ack, the bridge wrote the client's data to the device
transport and sent A_OKAY right away. A_OKAY is what lets the client send
its next A_WRTE, so a client faster than the device could keep sending
and everything the device had not taken yet piled up in the bridge.

The delayed_ack path already waits for the write callback. Use the same
path for both: SendWindow.ack() returns no payload without delayed_ack,
so the plain A_OKAY is unchanged.

Signed-off-by: dlstjd0237 <[email protected]>
@dlstjd0237
dlstjd0237 force-pushed the fix/tcpusb-write-backpressure branch from f92578d to ec283ba Compare November 26, 2026 08:02
@koral--
koral-- merged commit ad489d2 into DeviceFarmer:master Nov 26, 2026
3 checks passed
koral-- added a commit that referenced this pull request Nov 26, 2026
…, early writes) (#601)

* fix(tcpusb): ack reverse A_WRTE only after the local reader took the bytes

Follow-up to #600. ReverseStream still acked an A_WRTE right away when
delayed_ack is off, so a client sending into an `adb reverse` stream
faster than the device read it piled the data up in the bridge process.
Use the write-callback path for both cases, as Service now does.

Also cover Service's delayed_ack write path, which #600's tests left out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(tcpusb): close a reverse stream written to before it was open

The client may only write once it has accepted our A_OPEN with an A_OKAY;
before that we have no remote id to ack to, so close the stream instead
of passing the data to the local reader.

Tests: a reverse write that completes after the client's A_CLSE sends no
A_OKAY, and Service's window is picked from a real A_OPEN (delayed acks
only when the client has them and gives credit).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(tcpusb): close a stream whose client writes past its window

Service and ReverseStream passed every A_WRTE on to the device side,
however many were still waiting there. A client that did not wait for
our A_OKAY, or sent past its delayed_ack credit, to a device side that
reads slowly made the bridge buffer without limit.

Count what each stream has taken but not yet written: one A_WRTE without
delayed_ack, the credit we gave plus one payload with it (adb sends while
any credit is left). A client past that breaks the protocol, so close
that stream. Pausing the client socket instead would also stop every
other stream on the connection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

2 participants