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]>
This commit is contained in:
1 parent
74389cc8de
commit
4998ab3d45
2 files changed
+97
-8
No files matched your search
+52
-1
@@ -27,7 +27,10 @@
|
||||
// library — so anything can use it.
|
||||
package safemap
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Map is a map[string]T guarded by a read/write mutex.
|
||||
//
|
||||
@@ -97,3 +100,51 @@ func (s *Map[T]) Snapshot() map[string]T {
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user