Skip to content

fix: close leaked sqlstore container on client restart/reconnect - #200

Open
alexmagnoreis wants to merge 1 commit into
evolution-foundation:mainfrom
alexmagnoreis:fix/close-container-on-reconnect
Open

alexmagnoreis wants to merge 1 commit into
evolution-foundation:mainfrom
alexmagnoreis:fix/close-container-on-reconnect

Conversation

@alexmagnoreis

@alexmagnoreis alexmagnoreis commented Sep 17, 2026

Copy link
Copy Markdown

Problem

StartClient() creates a brand new sqlstore.Container (and therefore a new *sql.DB connection pool) on every call, but the previous container is never closed.

StartClient is re-invoked on every WhatsApp websocket reconnect — events.DisconnectedReconnectClient()StartInstance()StartClient(), and also on LoggedOut/restart paths. ReconnectClient() carefully tears down the websocket, event handler and in-memory maps, but never closes the underlying DB pool.

In production this showed up as a slow, unbounded growth of idle Postgres connections from the evolution-go process — eventually exhausting max_connections and causing FATAL: sorry, too many clients already for every other service sharing that Postgres instance. Restarting the process temporarily fixes it (all leaked pools go away), but it comes back as reconnects accumulate over days.

Fix

  • Track the active *sqlstore.Container per instance in a new containerPointer map on whatsmeowService.
  • In StartClient, close the previous container for that instance (if any) before opening a new one.
  • In ClearInstanceCache, close and clear the container when an instance is fully torn down.

Minimal, targeted change — no behavior change for the happy path, just closes pools that were previously leaked.

Testing

No Go toolchain was available in the environment used to prepare this patch, so this has not been compiled/run locally. The change is small and mechanical (store a pointer, call .Close(), matching the existing Container.Close() API), but please run it through CI/your usual test suite before merging.

Summary by Sourcery

Prevent SQL connection pool leaks by closing instance containers during client replacement and teardown.

Bug Fixes:

  • Close previously active SQL store containers when clients restart or reconnect, preventing leaked database connection pools and PostgreSQL connection exhaustion.

Enhancements:

  • Track SQL store containers per instance and release them when instances are fully cleared.

StartClient() created a brand new sqlstore.Container (and therefore a
new *sql.DB connection pool) on every call, but the previous container
was never closed. Since StartClient is re-invoked on every websocket
reconnect (events.Disconnected, LoggedOut, ReconnectClient), each
reconnect leaked a full connection pool that lived for the process
lifetime, eventually exhausting Postgres max_connections after enough
reconnects accumulate over days.

Track the active container per instance and close the previous one
before opening a new one in StartClient, and also close it when the
instance is fully torn down in ClearInstanceCache.
@sourcery-ai

sourcery-ai Bot commented Sep 17, 2026

Copy link
Copy Markdown

Reviewer's Guide

Prevents database connection-pool leaks across WhatsApp reconnects and instance teardown by tracking each instance’s sqlstore container, closing replaced containers before reopening, and cleaning up the active container during full cache clearance.

Sequence diagram for closing the SQL container during client reconnect

sequenceDiagram
    participant Reconnect as ReconnectClient
    participant Service as whatsmeowService
    participant Store as sqlstore.Container
    participant DB as SQL connection pool

    Reconnect->>Service: StartClient(cd)
    Service->>Store: Close()
    Store->>DB: Close connections
    Service->>Store: Open new container
    Service->>Service: containerPointer[instanceId] = container
Loading

Sequence diagram for instance teardown cleanup

sequenceDiagram
    participant Teardown as ClearInstanceCache
    participant Service as whatsmeowService
    participant Store as sqlstore.Container
    participant DB as SQL connection pool

    Teardown->>Service: ClearInstanceCache(instanceId, token)
    Service->>Store: Close()
    Store->>DB: Close connections
    Service->>Service: delete containerPointer[instanceId]
Loading

File-Level Changes

Change Details Files
Track and lifecycle-manage the active SQL store container for each instance.
  • Add a per-instance container pointer map and initialize it with the service.
  • Close and remove any previous container before creating a replacement during client startup.
  • Store the newly created container for later cleanup.
  • Close and remove the container when an instance cache is fully cleared.
pkg/whatsmeow/service/whatsmeow.go

Possibly linked issues

  • #Postgres connection pool leak on every StartClient/reconnect cycle: The PR directly fixes the reported leak by tracking containers and closing them during restart and instance teardown.
  • #Postgres connection leak: The PR directly fixes the issue by tracking containers and closing them before reconnects or instance teardown.
  • #PostgreSQL connection leak: The PR directly fixes the issue by tracking and closing sqlstore containers during reconnects and teardown.

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 the old container is still being used during a concurrent restart or reconnect, closing it can terminate database connections and cause failed stores or a service outage. Reverting stops future closures, but requests and operations that already failed during the interruption cannot be undone.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

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