Skip to content

Handle stale ADB stream frames during session reuse#207

Open
ducminh-phan wants to merge 1 commit into
cocool97:mainfrom
ducminh-phan:main
Open

Handle stale ADB stream frames during session reuse#207
ducminh-phan wants to merge 1 commit into
cocool97:mainfrom
ducminh-phan:main

Conversation

@ducminh-phan

Copy link
Copy Markdown

What went wrong

adb_client assumed the next frame read from a reused transport belonged to the command that was just sent.

That is not always true. Over TCP/wireless debugging, old stream frames can arrive late after the client has already sent a new OPEN. When that happened, the next session could consume a stale OKAY, CLSE, or WRTE as if it were the response for the new stream, causing failures like:

  • Open session failed: responses used <x> for our local_id instead of <y>
  • Open session failed: got CLSE in response instead of OKAY
  • Open session failed: got WRTE in response instead of OKAY

This was also reported in #175.

Shell handling also acknowledged every received frame before checking it, so it sent OKAY for CLSE. CLSE is terminal and should not be acknowledged.

Reference: https://android.googlesource.com/platform/packages/modules/adb/+/1cf2f017d312f73b3dc53bda85ef2610e35a80e9/docs/dev/protocol.md

The fix

open_session() now waits for the OKAY that matches the new stream's local_id instead of trusting the next frame on the socket.

While opening a session:

  • stale OKAY and CLSE frames are ignored
  • stale WRTE frames are acknowledged with their original stream ids, then discarded
  • CLSE or early WRTE for the new stream still fails the open

Shell command handling now reads frames first and only sends OKAY for WRTE.

For shell sessions:

  • CLSE is no longer acknowledged
  • stale WRTE payload is discarded instead of being written to the current command output
  • stale CLSE is ignored
  • the session closes only on CLSE for the active stream

I debugged and wrote the fix with the help of GPT-5.5. I also verified the behaviour manually.

Match OPEN responses by local stream id instead of assuming the next
socket frame belongs to the new session. Stale OKAY/CLSE frames are
ignored, and stale WRTE frames are acknowledged with their original
stream ids then discarded.

Update shell command handling so CLSE is no longer acknowledged with
OKAY. Shell readers now only ACK WRTE frames, discard stale stream
payloads, and close only on CLSE for the active stream.

Add regression tests for stale OPEN frames, stale shell WRTE handling,
and avoiding OKAY replies to CLSE.
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