Files
EvoGO/pkg/safemap/safemap.go
T
jfengandClaude Opus 5.5 4998ab3d45 fix(whatsmeow): harden client lifecycle against duplicate starts and races
- Skip a second StartClient while one is starting (adapted from upstream PR #239).
- Do not restart a client without a paired device; it would cycle QR codes
  forever (adapted from upstream PR #240).
- Serialize ReconnectClient per instance and nil-check the client: two
  Disconnected events in the same second dereferenced a removed client and
  killed the whole process (upstream issue #188).
- safemap.TrySend / TrySendTimeout: kill-channel sends no longer panic with
  "send on closed channel" when the instance is deleted during QR handling
  (upstream issue #222).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-09 12:04:18 -03:00

151 lines
4.0 KiB
Go

// Package safemap holds the process-wide maps that are shared across goroutines.
//
// WHY THIS EXISTS
//
// The three state maps of the process (clientPointer, myClientPointer and
// killChannel) are created once at startup and passed BY REFERENCE into eleven
// packages. Every one of them read and wrote those maps directly, from different
// goroutines, with no synchronisation at all.
//
// Go does not tolerate that. Concurrent writes do not corrupt silently: 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: a process died at StartClient while five instances
// were connecting at the same time. The container orchestrator brought it back
// within seconds, but every number was offline in the meantime, and it repeats
// on every restart that brings up more than a couple of instances at once.
//
// WHY A SEPARATE PACKAGE, AND WHY GENERICS
//
// MyClient lives in pkg/whatsmeow/service. If the safe type lived there, the
// other ten packages would have to import that package and an import cycle
// would appear. This package has no internal dependencies — only the standard
// library — so anything can use it.
package safemap
import (
"sync"
"time"
)
// Map is a map[string]T guarded by a read/write mutex.
//
// Always use the pointer (*Map[T]): copying the struct would copy the mutex,
// and a copied mutex stops protecting the original — two owners, one lock each,
// and the race comes back silently.
type Map[T any] struct {
mu sync.RWMutex
m map[string]T
}
// New returns an empty map, ready to use.
func New[T any]() *Map[T] {
return &Map[T]{m: make(map[string]T)}
}
// Get returns the value, or the zero value of T when the key is absent —
// exactly like the m[k] it replaces, so swapping call sites changes no
// behaviour.
func (s *Map[T]) Get(k string) T {
s.mu.RLock()
defer s.mu.RUnlock()
return s.m[k]
}
// Lookup is the v, ok := m[k] form.
func (s *Map[T]) Lookup(k string) (T, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
v, ok := s.m[k]
return v, ok
}
// Set is the m[k] = v form.
func (s *Map[T]) Set(k string, v T) {
s.mu.Lock()
defer s.mu.Unlock()
s.m[k] = v
}
// Delete is the delete(m, k) form.
func (s *Map[T]) Delete(k string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.m, k)
}
// Len is the len(m) form.
func (s *Map[T]) Len() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.m)
}
// Snapshot returns a copy for range loops.
//
// Ranging over the map from the inside would hold the lock for the whole loop,
// and the loop bodies here talk to WhatsApp — the lock would be held across
// network calls. The snapshot costs one copy of the pointers and releases the
// lock immediately.
func (s *Map[T]) Snapshot() map[string]T {
s.mu.RLock()
defer s.mu.RUnlock()
out := make(map[string]T, len(s.m))
for k, v := range s.m {
out[k] = v
}
return out
}
// TrySend delivers v on ch and reports whether it was delivered.
//
// The kill channels are closed when an instance is deleted, and a goroutine
// that still holds a reference (QR timeout, Disconnected handler) used to
// panic with "send on closed channel", taking the whole process down
// (upstream issue #222). A nil channel (instance already removed from the map)
// would block forever, so it is reported as not delivered instead.
func TrySend[T any](ch chan T, v T) (ok bool) {
if ch == nil {
return false
}
defer func() {
if recover() != nil {
ok = false
}
}()
ch <- v
return true
}
// TrySendTimeout is TrySend bounded by d. A d of zero makes it non-blocking.
func TrySendTimeout[T any](ch chan T, v T, d time.Duration) (ok bool) {
if ch == nil {
return false
}
defer func() {
if recover() != nil {
ok = false
}
}()
if d <= 0 {
select {
case ch <- v:
return true
default:
return false
}
}
timer := time.NewTimer(d)
defer timer.Stop()
select {
case ch <- v:
return true
case <-timer.C:
return false
}
}