Commit 1d38d4f
fix(baileys): prevent healthy instances from being killed after stream:error 515 (#2509)
* fix(baileys): prevent healthy instances from being killed after stream:error 515
When WhatsApp sends stream:error code=515 (Connection Replaced), Baileys
handles the reconnect correctly and fires connection.update with state='open'.
However, WhatsApp then sends a 401 (loggedOut) to clean up the old session
slot, which Evolution API incorrectly treated as a real logout, killing the
newly-connected healthy instance.
The fix tracks when a stream:error 515 node arrives via the CB:stream:error
WebSocket event. If a loggedOut (401) close event fires within 30 seconds of
a 515, it is treated as a transient reconnect rather than a real logout.
Fixes #2498
* fix(baileys): name the 515 reconnect grace + tighten stream-error types
Addresses sourcery-ai review feedback on the previous commit:
- Extract the 30 000ms reconnect grace window into a named class constant
STREAM_515_RECONNECT_GRACE_MS so future tuning is self-documenting
rather than a literal scattered through the close handler.
- Extract the magic '515' string into STREAM_ERROR_CODE_RECONNECT.
- Replace the loose 'node: any' on the 'CB:stream:error' handler with a
minimal structural type ({ attrs?: { code?: string | number } }) so
the payload shape is documented and type-checked.
- Compare the code via String(...) so a numeric 515 from the underlying
socket library still triggers the grace window — the original literal
'515' check would have silently broken on a type change.
* fix(baileys): satisfy prettier — collapse 515 reconnect guard onto one line
Check Code Quality lint failed on prettier/prettier (the
recentStream515 expression fits within the project's 120-col
printWidth on a single line, while shouldReconnect still needs to
break across two).
Co-authored-by: Octopus <liyuan851277048@icloud.com>
---------
Co-authored-by: octo-patch <octo-patch@github.com>
Co-authored-by: octo-patch <octo-patch@users.noreply.github.com>1 parent 5624bda commit 1d38d4f
1 file changed
Lines changed: 21 additions & 1 deletion
Lines changed: 21 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
| 267 | + | |
267 | 268 | | |
268 | 269 | | |
269 | 270 | | |
| |||
275 | 276 | | |
276 | 277 | | |
277 | 278 | | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
278 | 286 | | |
279 | 287 | | |
280 | 288 | | |
| |||
506 | 514 | | |
507 | 515 | | |
508 | 516 | | |
509 | | - | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
510 | 523 | | |
511 | 524 | | |
512 | 525 | | |
| |||
515 | 528 | | |
516 | 529 | | |
517 | 530 | | |
| 531 | + | |
518 | 532 | | |
519 | 533 | | |
520 | 534 | | |
| |||
813 | 827 | | |
814 | 828 | | |
815 | 829 | | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
816 | 836 | | |
817 | 837 | | |
818 | 838 | | |
| |||
0 commit comments