#2770·Baileys

Serialize pairing-code state through the complete pairing lifecycle

Author: coderabbitai[bot]Created Aug 12, 2026Updated Sep 13, 2026
LabelsStale

Summary

Concurrent requestPairingCode calls can corrupt shared pairing state before the device pairing response is processed.

src/Socket/messages-recv.ts function decipherLinkPublicKey derives its decryption key from authState.creds.pairingCode. src/Socket/socket.ts function requestPairingCode also writes creds.pairingCode and creds.me. Therefore, this state remains relevant after the companion-registration IQ resolves.

A second request can replace the first request's pairing code. When the user completes the first pairing attempt, the response can then be undecryptable.

Scope

Affected areas:

  • src/Socket/socket.ts: requestPairingCode and pairing state persistence.
  • src/Socket/messages-recv.ts: decipherLinkPublicKey and dependent pairing-response handling.

The change must serialize the complete pending pairing lifecycle, or carry pairing code and device identity as per-request state through registration and response decryption.

Do not release a request guard when the registration IQ resolves. The pairing response can arrive later, after the user enters the pairing code.

Design consideration

Rejecting a second request while a pairing request is pending is the smaller implementation. However, if the client abandons pairing without closing the socket, that approach can block new pairing requests until a timeout.

Per-request pairing state avoids that block but requires a wider lifecycle change. Select and document the behavior for abandoned pairing attempts.

Acceptance criteria

  • A second requestPairingCode cannot overwrite state required to decrypt an earlier pairing response.
  • A pairing response remains decryptable after overlapping pairing-code requests are attempted.
  • The implementation covers registration, delayed user completion, response decryption, success, failure, timeout, and socket teardown.
  • If a pending-request guard is used, it remains active until the complete pairing lifecycle ends and has defined cleanup for timeout and socket teardown.
  • Tests cover the selected concurrency behavior and a delayed pairing response.

Backlinks