Commit c29bcc5
fix(instance.controller): emit remove.instance even when logout fails (zombie cleanup) (#2520)
* fix(instance.controller): emit remove.instance even when logout fails
When a Baileys WebSocket dies but the in-memory `waInstances[name]`
entry still exists (a "zombie" instance), `deleteInstance()` calls
`await this.logout()` which throws "Connection Closed". The throw
causes the outer try/catch to skip the `eventEmitter.emit('remove.instance')`
call — which is the only mechanism that purges the zombie from
`waInstances`.
Result: zombies persist in memory until the entire `evo2_api`
container is restarted, affecting ALL instances on the host (not
just the broken one). Operators have no per-instance recovery path
in v2.3.x — their only option is `docker restart`, which forces
every connected user to re-scan the QR code.
Fix: wrap the inner `logout()` call in its own try/catch. Log a
warning when it fails but continue to the cleanup emit. The
in-memory entry must be removed regardless of whether logout
completed cleanly — `remove.instance` is the canonical way to
purge a stuck instance, and DB/cache cleanup happens in the same
event handler.
This makes `DELETE /instance/:name` idempotent against zombies: a
caller can always recover a single instance without nuking the
whole host.
Refs:
- #693 (instance/restart closes the session)
- #1286 (Connection Closed in v2.2.3)
- #2026 (Sync lost after reboot)
- #2027 (Loss of synchronization on reboot)
Tested in production at Rigarr (14 instances, ~25k msgs/day) by
overlaying this patch on v2.3.7 via Docker. Before: any zombie
forced a full container restart. After: per-instance cleanup
works cleanly while other vendors stay connected.
Signed-off-by: Bruno Cavalcante Sgarbi <bcsgarbi@gmail.com>
* review: address Sourcery feedback — neutral language + log error object
Per #2520 review:
1. Drop vendor-specific markers in code comment and log message
(was '[ZOMBIE-CLEANUP]' and 'RIGARR PATCH'). Comment now describes
the bug in upstream-friendly terms.
2. Pass the full error object to logger.warn instead of toString(),
following the existing convention in monitor.service.ts
('no.connection' handler) where structured object logging is used
to preserve diagnostic detail.
No behavior change.
---------
Signed-off-by: Bruno Cavalcante Sgarbi <bcsgarbi@gmail.com>
Co-authored-by: Bruno Sgarbi <bcsgarbi@gmail.com>1 parent 3f567db commit c29bcc5
1 file changed
Lines changed: 15 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
458 | 458 | | |
459 | 459 | | |
460 | 460 | | |
461 | | - | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
462 | 476 | | |
463 | 477 | | |
464 | 478 | | |
| |||
0 commit comments