StartClient called sqlstore.New on every instance start, opening a new
*sql.DB and re-running Upgrade against PostgresAuthDB. A few parallel
QR/reconnects saturated PgBouncer (75 = 60+15) with bad connection.
Reuse the pooled authDB from initPostgresAuthDB via NewWithDB + Upgrade
once (sync.Once on a heap-allocated sharedSQLStore so value-receiver
StartClient is safe). Users/GORM DB stays separate. Do not Close the
shared container on disconnect — main owns authDB.
(cherry picked from commit f2a73c715a286aefa5c10fa8521dccc7efa56581)
clientPointer, myClientPointer and killChannel are created once at startup and
passed by reference into eleven packages. All of them read and wrote those plain
Go maps from different goroutines, with no synchronisation.
Go does not tolerate that: the runtime kills the process with
fatal error: concurrent map writes
and a fatal error is not recoverable — a deferred recover() does not catch it.
Measured in production: the process died inside StartClient while five instances
were connecting at once. The orchestrator restarted it within seconds, but every
number was offline in the meantime, and it repeats on any restart that brings up
more than a couple of instances together.
The three maps become safemap.Map[T], a generic wrapper over a RWMutex. It lives
in its own package because MyClient is defined in pkg/whatsmeow/service, and
putting the type there would create an import cycle with the ten packages that
receive the maps.
98 call sites changed across 12 files, with the compiler as the judge: anything
missed is a build error, not a silent bug.
Tests: with -race, a plain map running the same exercise reports DATA RACE and
fails, while safemap.Map passes. Both halves verified.
(cherry picked from commit 3860fa278dc2b901705ffbddbed5cf59d70a85e4)
The Archive case asserts postMap["data"] to map[string]interface{}, but at that
point it still holds the raw *events.Archive. The assertion panics on every
archive event:
Event handler panicked while handling a *events.Archive:
interface conversion: interface {} is *events.Archive,
not map[string]interface {}
whatsmeow recovers the panic and logs it, so nothing crashes — the event simply
never reaches the webhook, silently.
Every other case in this switch marshals and unmarshals postMap["data"] into a
map before asserting. Archive was the only one missing that step. Six call sites
do the assertion; the other five are already guarded.
(cherry picked from commit 16c4be6e9a0ae8a0bbc41f8b2790a362a7a4e2e2)