fix(instance): prevent duplicate runtimes and harden QR lifecycle - #145
Open
joldmarfilho wants to merge 2 commits into
Open
fix(instance): prevent duplicate runtimes and harden QR lifecycle #145joldmarfilho wants to merge 2 commits into
joldmarfilho wants to merge 2 commits into
Conversation
A QR request could spawn a runtime for an instance that already had one: GetQr started a client whenever clientPointer was nil or the client was logged in. Two clients on the same instance share one killChannel and one database row, so when the spare one exhausted its QR cycle it tore the runtime down and flagged the instance as disconnected while the paired session stayed online. Callers polling for a QR then kept feeding the loop. StartClient now admits a single runtime per instance, guarded by a token so a late cleanup cannot free the reservation of its replacement, and GetQr refuses to restart a logged-in session instead of racing it.
Reviewer's GuideIntroduces a more robust instance runtime and reconnect lifecycle for WhatsApp clients: enforces a single runtime per instance with token-based reservations, adds a deduplicated auto-reconnect scheduler with backoff and jitter, separates QR teardown from automatic reconnection for unpaired sessions, and adds tests around runtime/reconnect coordination and QR runtime decisions. Sequence diagram for controlled reconnect lifecyclesequenceDiagram
participant MyClient
participant WhatsmeowService as whatsmeowService
participant WAClient
MyClient->>WAClient: myEventHandler(events.Disconnected)
MyClient->>WhatsmeowService: ScheduleReconnect(userID)
activate WhatsmeowService
WhatsmeowService->>WhatsmeowService: GetInstanceByID(instanceId)
WhatsmeowService->>WhatsmeowService: canAutoReconnect(instance.Jid)
WhatsmeowService->>WhatsmeowService: reserveReconnect(instanceId)
deactivate WhatsmeowService
Note over WhatsmeowService: goroutine with delay from reconnectDelay
WhatsmeowService->>WhatsmeowService: ReconnectClient(instanceId)
activate WhatsmeowService
WhatsmeowService->>WhatsmeowService: reserveRuntime(instanceId)
WhatsmeowService->>WhatsmeowService: deviceContainer(dbLog)
WhatsmeowService->>WhatsmeowService: StartClient(ClientData)
deactivate WhatsmeowService
WAClient-->>MyClient: events.Connected
MyClient->>WhatsmeowService: markReconnectSuccess(userID)
WhatsmeowService->>WhatsmeowService: finishReconnect(instanceId, connected=true)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Related Issue
Closes #(issue_number)
Type of Change
Testing
Screenshots (if applicable)
Checklist
Additional Notes
Summary by Sourcery
Improve WhatsApp instance runtime lifecycle to prevent concurrent clients and uncontrolled reconnect loops while making QR-based sessions safer.
Bug Fixes:
Enhancements:
Tests: