Skip to content

fix(tcpusb): flow control on client writes (reverse acks, write limit, early writes) - #601

Merged
koral-- merged 3 commits into
masterfrom
fix/tcpusb-reverse-write-backpressure
Nov 26, 2026
Merged

koral-- merged 3 commits into
masterfrom
fix/tcpusb-reverse-write-backpressure

Conversation

@koral--

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

Copy link
Copy Markdown
Member

Follow-up to #600. Three commits, one per fix.

1. Ack reverse A_WRTE only after the local reader took the bytes

#600 fixed Service, but ReverseStream._write still acked right away for clients without delayed_ack. On an adb reverse stream, a client sending faster than the device app reads piled the data up in the bridge. Same fix as #600: A_OKAY goes out from the write callback, and a failed write ends the stream.

2. Close a reverse stream written to before it was open

ReverseStream.handle() passed an A_WRTE on even before the client accepted our A_OPEN, and acked it to remote id 0. The stream is registered as soon as it's created (socket.ts), so this was reachable. Such a write now closes the stream.

3. Close a stream whose client writes past its window

Neither Service nor ReverseStream limited what a client could have waiting for the device side. A client that ignored our A_OKAY, or sent past its delayed_ack credit, could make the bridge buffer without limit. In stf that is one bridge per device, all on one host.

The new ReceiveWindow counts, per stream, what was taken but not yet written:

  • without delayed_ack: one A_WRTE, since the client must wait for each A_OKAY;
  • with it: the credit we gave (INITIAL_WINDOW, topped up by each ack) plus one payload, because adb sends while any credit is left, so its last packet can go past it.

A client that goes past that breaks the protocol, so that stream is closed. Pausing the client socket instead would also stop every other stream on the connection.

Tests

npm test: 420 passing. npm run lint: clean.

  • reversestream.mts (both modes): A_OKAY held until the write callback; a failed write sends A_CLSE; no A_OKAY for a write completing after the client's A_CLSE; a write before the stream is open closes it; the window limit.
  • service.mts: the delayed_ack write path; the window picked from a real A_OPEN (delayed only when the client has it and gives credit); the window limit, including after an A_OPEN that turned delayed acks off.

I checked each new test by breaking the code it covers; each break makes it fail.

Checked on an emulator

Bridge over an emulator, connected with adb 37.0.3. I ran this once as is (no delayed_ack) and once with ADB_BURST_MODE=1 (the bridge negotiated delayed_ack). In both: adb push of 256 MiB through the bridge matched its md5, adb shell worked, and 128 MiB sent through adb reverse to a slow device-side reader arrived in full. No stream was closed by the new limit.

Commit 1 on its own, without delayed_ack, same reverse transfer:

Sender finished in Bridge RSS peak Bytes received on device
Before 0.17 s (not held back) 24 → 220 MB 134,217,728
After 34 s (device's read rate) 24 → 112 MB 134,217,728

Risk

A client that pipelines writes without waiting for acks, which it shouldn't, now gets that stream closed instead of having the bridge buffer for it. adb's own client waits for acks, as verified above in both modes.

🤖 Generated with Claude Code

koral-- and others added 3 commits November 26, 2026 10:48
…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]>
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]>
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]>
@koral-- koral-- changed the title fix(tcpusb): ack reverse A_WRTE only after the local reader took the bytes fix(tcpusb): flow control on client writes (reverse acks, write limit, early writes) Nov 26, 2026
@koral--
koral-- merged commit 02f4365 into master Nov 26, 2026
6 checks passed
@koral--
koral-- deleted the fix/tcpusb-reverse-write-backpressure branch November 26, 2026 10:24
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