Commit f89b505
Fix crash in RCTWebSocketModule when delegate callbacks fire after module invalidation (facebook#55858)
Summary:
Guard against nil socketID in all SRWebSocketDelegate callbacks in RCTWebSocketModule.
When the module is invalidated during teardown, invalidate nils the delegate and closes each socket. However, SocketRocket's network thread may have already dispatched a delegate callback to the main queue via setDelegateDispatchQueue:. By the time the block executes, the SRWebSocket has been deallocated and reactTag (an associated object) returns nil, causing either an objc_retain crash on a dangling pointer or an NSInvalidArgumentException when inserting nil into an NSDictionary literal.
The didFailWithError: callback already had a partial nil guard (socketID ?: @(-1)), but didCloseWithCode:, webSocketDidOpen:, and didReceiveMessage: did not. This adds consistent early-return nil checks to all four callbacks.
Previously reported in facebook#28278, facebook#29525, and facebook#31048 — all closed by the stale bot without a fix being merged.
## Changelog:
<!-- Help reviewers and the release process by writing your own changelog entry.
Pick one each for the category and type tags:
[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message
For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[IOS] [FIXED] - Fix crash in RCTWebSocketModule when delegate callbacks fire after module invalidation
Pull Request resolved: facebook#55858
Test Plan: This is a race condition during module teardown that is difficult to reproduce deterministically — it surfaces in production Crashlytics reports (< 0.1% of sessions). The fix is a nil guard matching the defensive pattern already present in didFailWithError:.
Reviewed By: javache
Differential Revision: D95056045
Pulled By: cipolleschi
fbshipit-source-id: 02d715dfdf1816b5366ec077849d560133a857c81 parent fdc8f40 commit f89b505
File tree
1 file changed
+15
-2
lines changed- packages/react-native/React/CoreModules
1 file changed
+15
-2
lines changedLines changed: 15 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
169 | 172 | | |
170 | 173 | | |
171 | 174 | | |
| |||
178 | 181 | | |
179 | 182 | | |
180 | 183 | | |
181 | | - | |
| 184 | + | |
182 | 185 | | |
183 | 186 | | |
184 | 187 | | |
185 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
186 | 193 | | |
187 | | - | |
| 194 | + | |
188 | 195 | | |
189 | 196 | | |
190 | 197 | | |
191 | 198 | | |
192 | 199 | | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
193 | 203 | | |
194 | 204 | | |
195 | 205 | | |
| |||
203 | 213 | | |
204 | 214 | | |
205 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
206 | 219 | | |
207 | 220 | | |
208 | 221 | | |
| |||
0 commit comments