Commit 376a8fc
fix(realtime): resolve critical race conditions and connection bugs
This commit addresses multiple critical bugs in the Realtime implementation
that caused connection instability, resource leaks, and race conditions.
**Critical Race Conditions Fixed:**
1. **Connection Race Condition**
- Added atomic check for connection state to prevent multiple simultaneous
WebSocket connections
- Now validates both status and connectionTask existence before creating
new connections
2. **Heartbeat Timeout Logic**
- Fixed inverted logic that caused false timeout detections
- Now correctly identifies when previous heartbeat wasn't acknowledged
- Clears pending heartbeat ref before reconnecting
3. **Channel Removal**
- Fixed missing channel removal from state (critical bug!)
- Made isEmpty check atomic with removal to prevent race conditions
**Resource Leak Fixes:**
4. **Reconnect Task Management**
- Added reconnectTask tracking to prevent zombie reconnection loops
- Cancels previous reconnect before starting new one
5. **Complete State Cleanup**
- disconnect() now clears pendingHeartbeatRef to prevent stale state
- Clears sendBuffer to prevent stale messages on reconnect
- Enhanced deinit cleanup for all tasks and connections
6. **Task Lifecycle**
- Removed weak self from long-running tasks (messageTask, heartbeatTask)
- Tasks now use strong references and rely on explicit cancellation
- Ensures proper WebSocket lifecycle management
**Edge Case Fixes:**
7. **Channel Subscription Verification**
- Re-checks connection status after socket.connect() await
- Prevents subscription attempts on failed connections
8. **Atomic Status Updates**
- onConnected() now sets status AFTER listeners are started
- Prevents race where error handlers trigger before setup completes
9. **Safe Connection Access**
- Captures conn reference inside lock before creating messageTask
- Prevents nil access during concurrent disconnect operations
**Impact:**
- Eliminates multiple WebSocket connection leaks
- Prevents false heartbeat timeout disconnects
- Fixes memory leaks from unreleased channels
- Stops reconnection loops and zombie tasks
- Resolves race conditions during connection state transitions
- Handles edge cases in channel subscription during network issues
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>1 parent bab4ec0 commit 376a8fc
File tree
2 files changed
+138
-73
lines changed- Sources/Realtime
2 files changed
+138
-73
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
199 | 205 | | |
200 | 206 | | |
201 | 207 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| |||
170 | 171 | | |
171 | 172 | | |
172 | 173 | | |
| 174 | + | |
| 175 | + | |
173 | 176 | | |
| 177 | + | |
174 | 178 | | |
175 | 179 | | |
176 | 180 | | |
| |||
182 | 186 | | |
183 | 187 | | |
184 | 188 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
189 | 195 | | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
195 | 200 | | |
196 | | - | |
197 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
198 | 216 | | |
199 | 217 | | |
| 218 | + | |
200 | 219 | | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
217 | 223 | | |
218 | 224 | | |
219 | | - | |
220 | | - | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
221 | 240 | | |
222 | 241 | | |
223 | 242 | | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
224 | 247 | | |
225 | 248 | | |
226 | 249 | | |
227 | 250 | | |
228 | | - | |
229 | 251 | | |
| 252 | + | |
| 253 | + | |
230 | 254 | | |
231 | 255 | | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
232 | 260 | | |
233 | 261 | | |
234 | 262 | | |
| |||
261 | 289 | | |
262 | 290 | | |
263 | 291 | | |
264 | | - | |
265 | | - | |
266 | | - | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
267 | 300 | | |
268 | 301 | | |
269 | 302 | | |
| |||
325 | 358 | | |
326 | 359 | | |
327 | 360 | | |
328 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
329 | 368 | | |
330 | 369 | | |
331 | 370 | | |
| |||
364 | 403 | | |
365 | 404 | | |
366 | 405 | | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
391 | 430 | | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
392 | 434 | | |
393 | | - | |
394 | | - | |
395 | 435 | | |
| 436 | + | |
| 437 | + | |
396 | 438 | | |
397 | 439 | | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
398 | 444 | | |
399 | 445 | | |
400 | 446 | | |
401 | | - | |
402 | | - | |
403 | | - | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
404 | 451 | | |
405 | 452 | | |
406 | 453 | | |
407 | 454 | | |
408 | 455 | | |
409 | | - | |
| 456 | + | |
410 | 457 | | |
411 | 458 | | |
412 | 459 | | |
| |||
418 | 465 | | |
419 | 466 | | |
420 | 467 | | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
425 | 473 | | |
426 | 474 | | |
| 475 | + | |
427 | 476 | | |
428 | | - | |
429 | | - | |
| 477 | + | |
| 478 | + | |
430 | 479 | | |
431 | 480 | | |
432 | | - | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
433 | 485 | | |
434 | 486 | | |
435 | 487 | | |
436 | | - | |
| 488 | + | |
437 | 489 | | |
438 | 490 | | |
439 | 491 | | |
| |||
442 | 494 | | |
443 | 495 | | |
444 | 496 | | |
445 | | - | |
| 497 | + | |
| 498 | + | |
446 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
447 | 504 | | |
448 | 505 | | |
449 | 506 | | |
| |||
462 | 519 | | |
463 | 520 | | |
464 | 521 | | |
| 522 | + | |
| 523 | + | |
465 | 524 | | |
466 | 525 | | |
467 | 526 | | |
| |||
0 commit comments