Skip to content

feat(forward): resolve tcp:0 ports, remove forwards, and close leaked sockets - #574

Merged
koral-- merged 4 commits into
masterfrom
fix/forward-port-and-remove
Oct 30, 2026
Merged

koral-- merged 4 commits into
masterfrom
fix/forward-port-and-remove

Conversation

@koral--

@koral-- koral-- commented Oct 30, 2026

Copy link
Copy Markdown
Member

What

Port reply for forwards (#456). When the ADB server opens a new tcp: listener for a forward, it sends OKAY OKAY, then the bound port as a length-prefixed string, then closes the connection. forward() never read that tail. That has two effects:

  • You can't use tcp:0, because the port never reaches you.
  • The socket never sees its end, so it stays open. 50 forward('tcp:0', …) calls left 50 sockets to :5037 open.

Changes:

  • forward(local, remote) keeps its Promise<boolean> signature. It now reads the reply to the end, so the socket closes.
  • New device.forwardTcp(local, remote): Promise<number> takes a tcp:<port> local and resolves with the port the server listens on.
    • The server sends no port when it only rebinds a listener that already exists (install_listener returns before setting resolved_tcp_port). In that case forwardTcp() falls back to the requested port.
    • Any other local endpoint is rejected.

Removing forwards (from #60).

  • device.removeForward(local) sends host-serial:<serial>:killforward:<local>, like adb forward --remove.
  • device.removeAllForwards() does not send killforward-all. In handle_forward_request (adb.cpp), killforward-all is handled before the transport is looked up, and remove_all_listeners() has no transport filter. So host-serial:<serial>:killforward-all, and adb -s <serial> forward --remove-all with it, removes the forwards of every device on the server. On a shared server, such as an stf provider, that is a trap. Instead, removeAllForwards() lists the forwards, keeps the ones whose serial is this device's, and removes them one by one. A forward that is gone by the time it's removed is skipped.

Same leak, two more commands. Each fix is in its own commit.

Nothing on Client changes: forward, listForwards and reverse live only on DeviceClient, so the new methods do too. The README documents the new methods. I didn't touch CHANGELOG.md because it has no Unreleased section and prepare-release generates it from the PR titles.

Supersedes #457, thanks @yotamN. Supersedes #60, thanks @mingyuan-xia.
Fixes #456
Fixes #526

Verification

Unit tests. npm test: 281 passing. npm run lint: clean.

  • New tests cover the reply cases: port tail present, absent (rebind or non-tcp local), arriving in pieces, and malformed.
  • They also cover the rejections: tcp:0 with no reply, a non-tcp local, killforward FAIL (listener … not found), and removeAllForwards() with a mixed-serial list.
  • Every success test asserts that the socket was read to its end (readableEnded, readableLength === 0). Against the old forward.js, those tests fail.

End to end on an Android 12 emulator (emulator-5610, adb 37.0.1).

  • forwardTcp('tcp:0', 'tcp:9123') resolved with 60947.
  • adb forward --list showed emulator-5610 tcp:60947 tcp:9123.
  • Connecting to 127.0.0.1:60947 read hello-from-device from toybox nc -l -p 9123 running on the device.
  • forwardTcp on the now-existing tcp:60947 (a rebind, so no reply) resolved with 60947.
  • forwardTcp('tcp:56110', …) resolved with 56110.
  • forward('tcp:0', …) still resolves with true.
  • removeForward('tcp:60947') removed that entry. A second call rejected with FailError: listener 'tcp:60947' not found.
  • removeAllForwards() removed this emulator's 3 remaining forwards. The forwards of two other emulators on the same adb server (emulator-5620, emulator-5622) were untouched.

Socket leak. I ran 50 calls in a loop, then counted with lsof -a -p <node pid> -iTCP:5037 after 1 s:

call (x50) before after
forward('tcp:0', …) 50 0
forward('tcp:56100', …): 1 new listener, then 49 rebinds 1 0
isInstalled('com.android.settings') 50 0
reverse('tcp:0', …) 50 0
forwardTcp('tcp:0', …) (new) n/a 0
forward + removeForward (new) n/a 0

listForwards() and isInstalled() of a missing package didn't leak before or after.

stf. stf calls none of these methods. With this branch's npm pack installed, node build.mts typecheck passes. A probe file that assigns the results of forward, forwardTcp, removeForward and removeAllForwards type-checked. Its one deliberately wrong line (forwardTcp to a string) failed as expected.

🤖 Generated with Claude Code

koral-- and others added 4 commits October 31, 2026 00:09
When the ADB server opens a new tcp: listener for a forward, it sends the
bound port after OKAY OKAY and closes the connection. forward() never read
that tail, so the socket never saw its end and stayed open: 50 calls with
tcp:0 left 50 sockets to the server open.

forward() now reads the reply to the end and still resolves with true.
The new device.forwardTcp(local, remote) resolves with the port, so a
tcp:0 forward can be used. When the server only rebinds an existing
listener it sends no port, and forwardTcp() falls back to the requested
one.

Fixes #456

Co-authored-by: Yotam <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
removeForward(local) sends host-serial:<serial>:killforward:<local>, like
adb forward --remove.

removeAllForwards() doesn't send killforward-all. The server handles that
request before it looks at the serial and removes the forwards of every
device, so adb -s <serial> forward --remove-all wipes other devices'
forwards too. removeAllForwards() lists the forwards, keeps this device's,
and removes them one by one. A forward that's gone by the time it's
removed is skipped.

Co-authored-by: Mingyuan Xia <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
isInstalled() read 'package:' and stopped, so the rest of the pm path
output stayed unread and the socket never closed: 50 calls for an
installed package left 50 sockets to the ADB server open. It now reads
the output to the end, like install() does.

Fixes #526

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
Like the ADB server for forward, adbd sends the bound port after OKAY OKAY
when a reverse opens a new tcp: listener. reverse() never read it, so the
socket stayed open: 50 calls with tcp:0 left 50 sockets to the ADB server
open. It now reads the reply to the end and still resolves with true.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
@koral--
koral-- merged commit 77d630b into master Oct 30, 2026
6 checks passed
@koral--
koral-- deleted the fix/forward-port-and-remove branch October 30, 2026 23:38
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.

Socket is not closed after running IsInstalledCommand Forward command doesn't return the resolved tcp port

1 participant