Fix Android session never loading for long conversations#214
Merged
Conversation
Two issues caused sessions with long histories to show 'Loading conversation...' forever on Android (remote mode): 1. Thread-safety: SendSessionHistoryToClient iterated session.History (a plain List<ChatMessage>) without synchronization. SDK event handlers modify this list from background threads, causing InvalidOperationException during enumeration. The exception was silently caught by the generic handler, so no history was ever sent to the mobile client. Fixed by taking a defensive snapshot via ToArray() with retry on concurrent modification. 2. Unbounded history request on turn end: RequestHistoryAsync was called with limit: null after each turn, serializing ALL messages for long conversations into a single massive JSON payload. Capped to 200 messages to keep payloads manageable while still providing good context. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- adb install -r consistently drops for ~98MB APKs over WiFi ADB - adb push + pm install is reliable (push streams, install runs on-device) - Add invariant: never hardcode ports in proxy script, always use CLI args - Update all references from adb install to push+pm install workflow Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ailure - ToArray() uses Array.Copy, not enumerator — won't throw InvalidOperationException. Remove dead retry/catch block. - On snapshot failure, log and return without sending — never send an empty authoritative payload (TotalCount=0, HasMore=false) that permanently breaks the session on the client. - Catch Exception (not bare catch) to handle ArgumentOutOfRangeException from concurrent list resize. - Extract TurnEndHistoryLimit constant (200). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Feb 26, 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.
Problem
On Android (remote mode), sessions with long conversation histories (like FixUITestFromFreezing) show "Loading conversation…" forever and never display messages.
Root Causes
1. Thread-safety in SendSessionHistoryToClient
session.Historyis a plainList<ChatMessage>modified concurrently by SDK event handlers on background threads. WhenSendSessionHistoryToClientiterates the list via.Skip().ToList(), it can throwInvalidOperationException: Collection was modified. The exception is silently caught by the generic handler — so no history is ever sent to the mobile client.2. Unbounded history payload on turn end
When a turn ends,
RequestHistoryAsync(s)was called withlimit: null, requesting ALL messages. For long conversations with thousands of messages and tool results, this serializes to a massive JSON payload that can block or fail the WebSocket channel.Fix
SendSessionHistoryToClientnow takes an array snapshot ofHistorywith retry onInvalidOperationException, preventing concurrent modification crashesAlso included
android-wifi-deployskill:adb push+pm installinstead ofadb install -r(the ~98MB APK consistently drops WiFi ADB connections mid-transfer)