Repository navigation
feat(forward): resolve tcp:0 ports, remove forwards, and close leaked sockets - #574
Merged
Merged
Conversation
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]>
This was referenced Oct 30, 2026
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.
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:tcp:0, because the port never reaches you.forward('tcp:0', …)calls left 50 sockets to:5037open.Changes:
forward(local, remote)keeps itsPromise<boolean>signature. It now reads the reply to the end, so the socket closes.device.forwardTcp(local, remote): Promise<number>takes atcp:<port>local and resolves with the port the server listens on.install_listenerreturns before settingresolved_tcp_port). In that caseforwardTcp()falls back to the requested port.Removing forwards (from #60).
device.removeForward(local)sendshost-serial:<serial>:killforward:<local>, likeadb forward --remove.device.removeAllForwards()does not sendkillforward-all. Inhandle_forward_request(adb.cpp),killforward-allis handled before the transport is looked up, andremove_all_listeners()has no transport filter. Sohost-serial:<serial>:killforward-all, andadb -s <serial> forward --remove-allwith 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.
isInstalled()readpackage:and left the rest of thepm pathoutput unread. It now reads it to the end, likeinstall()does. Fixes Socket is not closed after running IsInstalledCommand #526.tcp:reverse.reverse()now reads it to the end.Nothing on
Clientchanges:forward,listForwardsandreverselive only onDeviceClient, 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.tcp:0with no reply, a non-tcp local,killforwardFAIL (listener … not found), andremoveAllForwards()with a mixed-serial list.readableEnded,readableLength === 0). Against the oldforward.js, those tests fail.End to end on an Android 12 emulator (
emulator-5610, adb 37.0.1).forwardTcp('tcp:0', 'tcp:9123')resolved with60947.adb forward --listshowedemulator-5610 tcp:60947 tcp:9123.127.0.0.1:60947readhello-from-devicefromtoybox nc -l -p 9123running on the device.forwardTcpon the now-existingtcp:60947(a rebind, so no reply) resolved with60947.forwardTcp('tcp:56110', …)resolved with56110.forward('tcp:0', …)still resolves withtrue.removeForward('tcp:60947')removed that entry. A second call rejected withFailError: 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:5037after 1 s:forward('tcp:0', …)forward('tcp:56100', …): 1 new listener, then 49 rebindsisInstalled('com.android.settings')reverse('tcp:0', …)forwardTcp('tcp:0', …)(new)forward+removeForward(new)listForwards()andisInstalled()of a missing package didn't leak before or after.stf. stf calls none of these methods. With this branch's
npm packinstalled,node build.mts typecheckpasses. A probe file that assigns the results offorward,forwardTcp,removeForwardandremoveAllForwardstype-checked. Its one deliberately wrong line (forwardTcpto astring) failed as expected.🤖 Generated with Claude Code