Skip to content

login: don't report dropped connections as login timeouts - #1087

Open
iFixRobots wants to merge 2 commits into
mainfrom
iFixRobots/whatsapp-login-disconnect-error
Open

iFixRobots wants to merge 2 commits into
mainfrom
iFixRobots/whatsapp-login-disconnect-error

Conversation

@iFixRobots

Copy link
Copy Markdown
Contributor

Every events.Disconnected during login was reported as FI.MAU.WHATSAPP.LOGIN_TIMEOUT. On phones that drop sockets of backgrounded apps, the connection often dies a few seconds after the pairing code or QR is shown, and the user is told they ran out of time.

WhatsApp closes the login socket after the QR refs run out: a fresh client that never pairs gets <xmlstreamend/> about 210s after the refs arrive. Since Disconnected carries no reason, this uses the existing getQRIndex() schedule. A disconnect while QR codes are still valid now returns FI.MAU.WHATSAPP.LOGIN_DISCONNECTED ("Connection to WhatsApp was interrupted while waiting for login. Please try again."). Anything after that stays a timeout. The log line now includes the time since the first QR.

Before / after, against the real WhatsApp server with the TCP socket closed mid-login:

Case Before After
Pairing code, socket dropped at 20s LOGIN_TIMEOUT LOGIN_DISCONNECTED
QR, socket dropped at 70s LOGIN_TIMEOUT LOGIN_DISCONNECTED
Pairing code, server closes at 3m30s LOGIN_TIMEOUT LOGIN_TIMEOUT
QR, codes run out at 2m40s LOGIN_TIMEOUT LOGIN_TIMEOUT

Linear: https://linear.app/beeper/issue/PLAT-41010

View in Indent
Tag @indent to continue the conversation here.

@indent
indent Bot marked this pull request as ready for review November 22, 2026 15:02
@indent

indent Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Issues

No issues found.

CI Checks

All required CI checks passed on 9e8b4bb.

Review agents

Select any unchecked box below to run or rerun that agent.

Passed (1)
  • Indent Review Agent · Real timeouts near the deadline may report as drops
Full results

Indent Review Agent

  • Summary: Real timeouts near the deadline may report as drops
  • Last ran on commit: 9e8b4bbe
  • Latest result
    {
      "summary": "Real timeouts near the deadline may report as drops",
      "findings": [
        {
          "fix": "Classify a disconnect as dropped only when it is clearly before the expected end. Compute the index, require currentIndex >= 0 && currentIndex < len(wl.QRs), and also require at least a few seconds of grace before the end of the last code. One way: keep timeUntilNext and treat currentIndex == min(len(wl.QRs), len(qrIntervals))-1 with timeUntilNext under ~5-10s as a timeout. For the pairing-code flow (wl.PhoneCode), consider keeping ErrLoginTimeout for disconnects near the end of the schedule, or check the server's real close timing (the new since_first_qr log will show it) before shipping the new code there.",
          "line": 326,
          "path": "pkg/connector/login.go",
          "issue": "The Disconnected branch picks ErrLoginDisconnected whenever getQRIndex() >= 0, meaning less than 160s (sum of qrIntervals) has passed on the bridge's clock since wl.StartTime. StartTime is set when the bridge handles events.QR. That happens after WhatsApp sent pair-device, after one-way latency and after whatsmeow sends its ack, so the bridge's elapsed time is always a bit less than the server's. WhatsApp ends an unfinished pairing by closing the socket when its refs expire (whatsmeow's qrchan.go treats a server-side Disconnected as QRChannelTimeout). If the server closes at or near its own 160s mark, the bridge sees about 159.x s, gets index 5, and returns FI.MAU.WHATSAPP.LOGIN_DISCONNECTED (500) for what is really a timeout.\n\nThis matters most in the pairing-code flow. There Wait() has no client-side timer and just waits on LoginComplete, so the server's disconnect is the only way an expired code ends. StartTime is set there too, from the QR event SubmitUserInput waits on. So every expired pairing code can now be reported as a 500 dropped connection instead of LOGIN_TIMEOUT, which is the opposite of what the PR intends.\n\nThe check also ignores len(wl.QRs). If the server sends fewer than 6 refs and closes once they run out, the schedule still shows index >= 0 and the disconnect is reported as dropped. Wait() itself bounds by len(wl.QRs).",
          "title": "Real end-of-login timeouts can be reported as LOGIN_DISCONNECTED because the cutoff has no margin",
          "severity": "bug"
        }
      ]
    }

@iFixRobots
iFixRobots requested a review from tulir November 22, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant