Skip to content

fix(tcpip): resolve tcpip() and usb() unless adbd replies with an error - #583

Merged
koral-- merged 2 commits into
masterfrom
fix/tcpip-empty-output
Nov 7, 2026
Merged

koral-- merged 2 commits into
masterfrom
fix/tcpip-empty-output

Conversation

@koral--

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

Copy link
Copy Markdown
Member

Problem

device.tcpip() resolved only if adbd's output after OKAY matched restarting in; anything else became new Error(output.trim()). adbd writes its message and restarts right away, and sometimes the message doesn't reach the host, even though the switch happened. tcpip() then rejected with an empty Error. device.usb() has the same code and the same bug.

Raw tcpip:5555 and usb: replies on the emulators, read straight from the adb server socket:

  • API 30: tcpip:5555 3 of 3 times OKAY + "restarting in TCP mode port: 5555\n"; usb: OKAY + empty payload, although service.adb.tcp.port went to 0.
  • API 36: tcpip:5555 3 of 3 times the message; usb: OKAY + "restarting in USB mode\n".

master device.tcpip(5555) then device.usb(), 4 rounds on each emulator, checking getprop service.adb.tcp.port after each:

emulator-5620 #2: tcpip() resolved 5555, tcp.port=5555; usb() rejected Error(""), tcp.port=0
emulator-5620 #3: tcpip() rejected Error(""), tcp.port=5555; usb() resolved true, tcp.port=0
emulator-5620 #4: tcpip() rejected Error(""), tcp.port=5555; usb() rejected Error(""), tcp.port=0
emulator-5622 #3: tcpip() resolved 5555, tcp.port=5555; usb() rejected Error(""), tcp.port=0
emulator-5622 #4: tcpip() resolved 5555, tcp.port=5555; usb() rejected Error(""), tcp.port=0

That's 6 failures in 16 calls (tcpip() 2 of 8, usb() 4 of 8), each time after the switch had actually happened.

Fix

Any output counts as success, matching adb tcpip/adb usb, which ignore the output, unless it's one of adbd's own error replies. adbd writes its message and restarts, so the message can be cut off anywhere ("restar") or be missing entirely. tcpip.ts and usb.ts share the check in restartreply.ts.

Which outputs are errors comes from adbd's source:

  • daemon/restart_service.cpp (main): tcpip: with a port <= 0 writes invalid port %d.
  • Android 4.4's services.c: the same case writes invalid port.
  • usb: has no error reply.
  • services.cpp: a port that isn't a number makes the service fail to start, which arrives as a FAIL and is already handled.

adbd has no error: … reply for these services, so only ^invalid port is matched.

Fixes #527

Verification

  • Unit tests: empty output resolves (tcpip → the port, usb → true), and adbd's invalid port 0 rejects with that message. npm test: 287 passing. npm run lint is clean.
  • The same 4-round loop with this branch: 16 of 16 calls resolved, and service.adb.tcp.port was 5555 after each tcpip() and 0 after each usb(), on both emulators.
  • device.tcpip(0) rejects with "invalid port 0" on both API levels.
  • Both emulators were restored to USB mode (tcp.port=0) afterwards.

stf doesn't use tcpip() or usb().

Review follow-up (commit 57ecac9)

  • Cut-off output such as restar or restarting in TCP mode po now resolves instead of rejecting. Only invalid port … rejects. Reasoning above.
  • One shared helper, restartreply.ts.
  • Tests: truncated messages, unrelated output and the old invalid port reply for tcpip; a cut-off restarting in U and invalid port for usb. npm test 291 passing, lint clean.
  • Emulators: 3 tcpip()/usb() round trips on each of API 30 and 36 all resolved, with service.adb.tcp.port 5555/0 after each. tcpip(0) rejects with "invalid port 0", and both are back in USB mode.

🤖 Generated with Claude Code

koral-- and others added 2 commits November 7, 2026 01:16
device.tcpip() and device.usb() expected adbd's "restarting in ..."
message after OKAY and otherwise threw an Error with the output as its
message. adbd restarts right after writing that message, and sometimes
it doesn't reach the host, although the switch happened. Both methods
then rejected with an empty Error. On Android 30 and 36 emulators this
happened in 6 of 16 calls on master.

No output now counts as success, as it does for adb itself, which
ignores the output. Any other message, such as "invalid port 0", is
still an error with that message. usb() had the same code and gets the
same fix.

Fixes #527

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
adbd writes its "restarting in ..." line and restarts, so the line can
be cut off anywhere, and a reply such as "restar" still rejected. Any
output now counts as success except adbd's own error reply: in adbd's
restart_service.cpp, tcpip: with a port <= 0 answers
"invalid port <port>" ("invalid port" before Android 5), and usb: has
no error reply. A port that isn't a number makes the service fail,
which arrives as a FAIL and is already handled.

tcpip.ts and usb.ts now share the check in restartreply.ts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Karol Wrótniak <[email protected]>
@koral-- koral-- changed the title fix(tcpip): resolve tcpip() and usb() when adbd's reply is empty fix(tcpip): resolve tcpip() and usb() unless adbd replies with an error Nov 7, 2026
@koral--
koral-- merged commit 071c335 into master Nov 7, 2026
6 checks passed
@koral--
koral-- deleted the fix/tcpip-empty-output 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.

TcpIP sometimes throws an empty error

1 participant