mattermost-mobile-upstream/app/managers
Guillermo Vayá 64bc557d2d
[MM-67999] fix Websocket crash (#9639)
* fix(ios): prevent WebSocket EXC_BAD_ACCESS crash during background teardown (Sentry XHW+YQG, 53K crashes)

Remove immediate invalidate() from closeAll() since it destroys native
WebSocket state while Starscream callbacks are still queued on the main
dispatch queue. closeAll() is used for temporary lifecycle transitions
(backgrounding, network changes) where openAll() later reuses clients.

Make invalidateClient() two-phase for legitimate destruction paths
(logout, server removal): close first, wait for the close event or a
timeout, then invalidate. This ensures queued native callbacks have
drained before the delegate is nilled.

Fixes MATTERMOST-MOBILE-IOS-XHW
Fixes MATTERMOST-MOBILE-IOS-YQG

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ios): harden WebSocket teardown against concurrent access and add tests

Move client eviction and timer cleanup before the async waitForClose()
in invalidateClient(), preventing concurrent openAll/initializeClient
from reaching a client mid-teardown. Wrap both NetworkManager and
WebsocketManager invalidation in safeExecute in terminateSession so a
throw doesn't abort remaining cleanup.

Add tests for waitForClose() (immediate resolve, onClose, timeout) and
for closeAll/invalidateClient behavior (no invalidate on close, correct
call ordering, missing client handling).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ios): track invalidation errors and guard against stale close callbacks

Address PR review feedback:
- Make client invalidation critical in terminateSession so failures are
  tracked in the errors array and visible to callers like SecurityManager.
- Guard onWebsocketClose against stale callbacks from an invalidated
  client whose late native onClose event could clobber a replacement
  client's connected state and periodic status updates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ios): wait for onClose handler and stop status updates on invalidation

Address remaining PR review feedback:

- waitForClose() no longer short-circuits on readyState CLOSED, only on
  conn being undefined. This ensures the onClose handler runs its cleanup
  (clearing conn, resetting state) before invalidate() proceeds, since
  readyState can be CLOSED before the onClose callback has fired.

- invalidateClient() now explicitly calls stopPeriodicStatusUpdates()
  during eviction. Previously this only happened via the onWebsocketClose
  callback, which may not fire if waitForClose() resolves via timeout or
  if the stale callback guard drops it.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ios): guard initializeClient and use instance-based stale callback check

- Add early return in initializeClient when the client has been evicted
  from this.clients, preventing a TypeError if a queued reconnect timer
  fires after invalidateClient deleted the entry.

- Replace the presence-only guard in onWebsocketClose with an
  instance-based isCurrentClient() check on all callbacks set in
  createClient. This prevents a late callback from an old client from
  clobbering a replacement client's state even after the new client is
  installed at this.clients[serverUrl].

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(ios): abort initialize() if teardown started during async gaps

Check this.stop after each await point in initialize() (Promise.all for
config values and getOrCreateWebSocketClient). If close(true) set the
stop flag while initialize() was suspended, bail immediately instead of
assigning this.conn and calling open() on a client that is mid-teardown.

When bailing after obtaining a native client, invalidate it to prevent
leaking the native WebSocket resource.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 17:29:04 +02:00
..
draft_upload_manager [MM-66908] Fix error display for file upload in low bandwidth (#9418) 2026-03-25 12:44:36 -04:00
intune_manager add support on iOS for Intune Conditional Access (#9660) 2026-04-09 17:46:52 +03:00
performance_metrics_manager feat: AI rewrite (#9280) 2026-02-16 16:48:57 +01:00
security_manager add support on iOS for Intune Conditional Access (#9660) 2026-04-09 17:46:52 +03:00
apps_manager.test.ts Enforce names for singleton imports (#8639) 2025-02-27 12:46:17 +01:00
apps_manager.ts Enforce names for singleton imports (#8639) 2025-02-27 12:46:17 +01:00
global_event_handler.ts [MM-67507] Fix threads not being refetch when switching translations (#9512) 2026-02-17 16:07:10 +01:00
integrations_manager.ts Add DialogRouter for Interactive Dialog to Apps Form migration (#9114) 2026-01-26 08:57:56 -07:00
network_manager.ts feat(MM-65145): low connectivity manager (#9283) 2025-12-04 10:44:33 -07:00
network_performance_manager.test.ts feat(MM-65145): low connectivity manager (#9283) 2025-12-04 10:44:33 -07:00
network_performance_manager.ts feat(MM-65145): low connectivity manager (#9283) 2025-12-04 10:44:33 -07:00
session_manager.test.ts add support on iOS for Intune Conditional Access (#9660) 2026-04-09 17:46:52 +03:00
session_manager.ts Fix android logout (#9398) 2026-01-08 13:20:10 +02:00
websocket_manager.test.ts [MM-67999] fix Websocket crash (#9639) 2026-04-09 17:29:04 +02:00
websocket_manager.ts [MM-67999] fix Websocket crash (#9639) 2026-04-09 17:29:04 +02:00