Skip to content

Harden WebSocket reconnection logic in Unity plugin#22

Merged
Niaobu merged 1 commit intomainfrom
fix/websocket-reconnection-hardening
Feb 20, 2026
Merged

Harden WebSocket reconnection logic in Unity plugin#22
Niaobu merged 1 commit intomainfrom
fix/websocket-reconnection-hardening

Conversation

@Niaobu
Copy link
Contributor

@Niaobu Niaobu commented Feb 20, 2026

Summary

Defensive fixes to the Unity plugin's WebSocket reconnection logic to prevent stale state from blocking reconnection:

  • Concurrency guard: Add _connecting flag so overlapping ConnectAsync calls are skipped rather than racing each other
  • Stale-state identity checks: Both the ConnectAsync catch block and ReceiveLoopAsync finally block now verify _webSocket == ownSocket before clearing _isConnected, preventing a stale/cancelled attempt from clobbering a newer successful connection
  • Connect timeout: 10s timeout on ConnectAsync via linked CancellationTokenSource so hanging TCP connections can't block reconnection indefinitely

Test plan

  • 163 unit/integration tests pass
  • 10/10 stop/start reconnection cycles pass (3s each)
  • Reconnection works after dev-install (force-kill + domain reload)
  • Reconnection works with main thread blocked during bridge restart

Three defensive fixes to prevent stale state from blocking reconnection:

- Add _connecting guard to prevent overlapping ConnectAsync calls
- Check socket identity before clobbering _isConnected in catch/finally
  blocks, so a stale receive loop or failed attempt can't reset state
  that a newer successful connection already set
- Add 10s timeout on ConnectAsync so hanging TCP connections don't block
  reconnection indefinitely
@Niaobu Niaobu merged commit 7b960fc into main Feb 20, 2026
1 check passed
@Niaobu Niaobu deleted the fix/websocket-reconnection-hardening branch February 20, 2026 07:40
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