Skip to content

fix: crash on app resume from stale WebSocket reader#195

Merged
barrydeen merged 1 commit intomainfrom
fix/websocket-disconnect-crash
Mar 10, 2026
Merged

fix: crash on app resume from stale WebSocket reader#195
barrydeen merged 1 commit intomainfrom
fix/websocket-disconnect-crash

Conversation

@barrydeen
Copy link
Copy Markdown
Owner

Summary

  • Use ws.cancel() instead of ws.close(1000, "Bye") in Relay.disconnect() to prevent ArrayIndexOutOfBoundsException on app resume
  • Graceful close left OkHttp's reader thread alive waiting for the server's close frame — when reconnectAll()/forceReconnectAll() called disconnect() then immediately connect(), the stale reader thread would crash in Okio's buffer (size=0, offset=0, byteCount=8192)
  • Safe because onFailure already checks isCurrent and drops stale socket callbacks

Test plan

  • Minimize the app, wait 30+ seconds, reopen — no crash
  • Minimize briefly (<30s) and reopen — no crash
  • Switch networks (wifi ↔ mobile) while app is backgrounded — reconnects cleanly

Graceful ws.close(1000) left OkHttp's reader thread alive waiting for
the server's close frame. When reconnectAll/forceReconnectAll called
disconnect() then immediately connect(), the stale reader thread would
crash with ArrayIndexOutOfBoundsException in Okio's buffer
(size=0, offset=0, byteCount=8192) on app resume.
@barrydeen barrydeen merged commit b50ea83 into main Mar 10, 2026
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