Skip to content

fix(instance): do not clear event subscriptions on disconnect - #187

Open
guipratiko wants to merge 1 commit into
evolution-foundation:developfrom
guipratiko:fix/disconnect-preserve-events
Open

fix(instance): do not clear event subscriptions on disconnect#187
guipratiko wants to merge 1 commit into
evolution-foundation:developfrom
guipratiko:fix/disconnect-preserve-events

Conversation

@guipratiko

@guipratiko guipratiko commented Sep 4, 2026

Copy link
Copy Markdown

Summary

  • Disconnect was wiping instance.Events before persisting the instance.
  • After reconnect/connect, the instance looked connected but CallWebhook built an empty subscription list (strings.Split("", ",") yields [""], which fails IsEventType), so every webhook was dropped.
  • Keep event subscriptions on disconnect and only mark the instance as disconnected via UpdateConnected.

Reproduction

  1. Instance connected with events=MESSAGE,CONNECTION,... and a webhook URL.
  2. POST /instance/disconnect
  3. POST /instance/reconnect (or connect again)
  4. Instance reports Connected/LoggedIn, but events is empty and inbound WhatsApp messages never reach the webhook.

Test plan

  • Disconnect an instance with non-empty events
  • Confirm GET /instance/info/{id} still shows the previous events string
  • Reconnect and send a test message → webhook delivers Message
  • Confirm connected=false / disconnect reason after disconnect

Made with Cursor

Summary by Sourcery

Preserve event subscriptions when disconnecting instances so reconnects continue to route webhook events correctly.

Bug Fixes:

  • Preserve instance event subscriptions across disconnects so webhooks continue delivering events after reconnecting.

Enhancements:

  • Update only the instance connection state and disconnect reason when disconnecting.

Disconnect was wiping Events before persisting. After reconnect the
instance looked connected but CallWebhook built an empty subscription
list (strings.Split("", ",") yields [""]), so every webhook was dropped.
Keep subscriptions and only mark the instance disconnected.

Co-authored-by: Cursor <cursoragent@cursor.com>
@sourcery-ai

sourcery-ai Bot commented Sep 4, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Disconnect now preserves the instance’s event subscription configuration and updates only its connected state and disconnect reason, preventing reconnects from silently dropping webhook deliveries.

Sequence diagram for preserving webhook subscriptions on disconnect and reconnect

sequenceDiagram
    participant API as Instance API
    participant Service as Instance Service
    participant Repo as Instance Repository
    participant Instance as Instance
    participant Webhook as Webhook

    API->>Service: Disconnect(instance)
    Service->>Instance: UpdateConnected(false, Disconnected by API)
    Service->>Repo: UpdateConnected(instance.Id, false, DisconnectReason)
    Note over Instance,Repo: Events remains unchanged

    API->>Service: Reconnect(instance)
    Service->>Repo: Read instance with Events
    Service->>Webhook: CallWebhook with preserved event subscriptions
    Webhook-->>API: Message webhook delivered
Loading

File-Level Changes

Change Details Files
Preserve configured event subscriptions when disconnecting while updating only the instance connection state.
  • Remove the assignment that clears the events string.
  • Persist disconnected status and the API disconnect reason through the targeted UpdateConnected operation.
  • Retain error propagation if the connection-state update fails.
pkg/instance/service/instance_service.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If this behavior is wrong, retained subscriptions can cause webhooks to be sent after an instance reconnects when they previously would have been dropped. Reverting prevents future sends, but it cannot undo notifications already delivered externally.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

1 participant