Skip to content

fix(session): return 502 when the WhatsApp unlink is not confirmed - #997

Merged
rmyndharis merged 1 commit into
mainfrom
fix/993-logout-failure-observable
Jul 29, 2026
Merged

fix(session): return 502 when the WhatsApp unlink is not confirmed#997
rmyndharis merged 1 commit into
mainfrom
fix/993-logout-failure-observable

Conversation

@rmyndharis

Copy link
Copy Markdown
Owner

Description

Fixes #993 (found reviewing #984). Both engine adapters caught their own logout() failure and resolved, so POST /sessions/:id/logout returned 200 and wrote a SESSION_LOGGED_OUT audit row even when the unlink never reached WhatsApp — and the engines failed differently: whatsapp-web.js kept the credentials and stayed linked, while Baileys wiped the local auth dir but stayed linked, leaving a Linked Devices entry with no local way to clear it.

  • whatsapp-web.js adapter — after the destroy() fallback (the session must still die locally), the original error is rethrown so the caller learns the unlink was not confirmed.
  • Baileys adapter — on a failed sock.logout() the socket is ended and the error rethrown, but clearAuthState() no longer runs: the stored credentials survive so the operator can start the session and retry the unlink without a QR scan.
  • session.service.logout() — consumes teardownEngineSafely's return value: the session is still torn down locally (engine map reconciled, status set to disconnected), but an unconfirmed unlink throws BadGatewayException, so no SESSION_LOGGED_OUT audit row is written for an unlink that never happened.
  • Route/docs — the route documents the 502; openapi.json, docs/06, and the CHANGELOG entry are updated (the endpoint is still unreleased, so this refines the feat(session): expose POST /sessions/:id/logout to unlink the device #984 contract before it ships rather than changing a published one).

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • Tests added/updated
  • Documentation updated
  • Lint passes
  • Self-reviewed

Related Issues

Closes #993

Both engine adapters caught their own logout failure and resolved, so
POST /sessions/:id/logout returned 200 and wrote a SESSION_LOGGED_OUT
audit row even when the unlink never reached WhatsApp. The engines also
failed differently: whatsapp-web.js kept the credentials and stayed
linked, while Baileys wiped the local auth dir but stayed linked —
leaving a Linked Devices entry with no local way to clear it.

Adapters now rethrow after their local fallback teardown, and Baileys
keeps the on-disk credentials on a failed unlink so the operator can
retry. session.service.logout() uses teardownEngineSafely's return
value: the session is still torn down locally (map reconciled, status
updated), but an unconfirmed unlink throws BadGatewayException, so no
audit row is written for an unlink that never happened.

Closes #993
@rmyndharis
rmyndharis merged commit 1904751 into main Jul 29, 2026
15 checks passed
@rmyndharis
rmyndharis deleted the fix/993-logout-failure-observable branch July 29, 2026 10:32
rmyndharis added a commit that referenced this pull request Jul 29, 2026
…the deadline race (#1001)

PR #999 added two teardown-resilience tests that drive logout() with a
promise hung past the 10s deadline, then await the call assuming it
resolves. PR #997 (which landed first) made logout() throw
BadGatewayException whenever the unlink is unconfirmed — exactly the
outcome of a lost deadline race — so both tests have failed on main since
#999 merged on top of it.

The production behaviour from #997 is correct and already asserted
explicitly (logout() rejects with 502 when the unlink is unconfirmed, but
the local teardown still completes). This updates the two #999 tests to
expect that rejection, while preserving what they actually exercise: the
losing promise stays registered in pendingTeardowns and start() still
waits for it (bounded) before re-creating the profile.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Engine logout() failures are not observable — wwjs keeps the link, Baileys wipes local credentials while staying linked

1 participant