fix(instance): do not clear event subscriptions on disconnect
Adapted from upstream PR #187 (guipratiko). Clearing instance.Events left the instance with an empty events string after reconnecting, so CallWebhook dropped every webhook. Also send kill signals through safemap.TrySend/TrySendTimeout so a channel closed by a concurrent instance delete no longer panics the process (upstream issue #222). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c33b4d98fd
commit
74389cc8de
1 file changed
+10
-17
@@ -318,12 +318,14 @@ func (i instances) Disconnect(instance *instance_model.Instance) (*instance_mode
|
||||
if client.IsConnected() {
|
||||
if client.IsLoggedIn() {
|
||||
i.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Disconnection successful", instance.Id)
|
||||
i.killChannel.Get(instance.Id) <- true
|
||||
safemap.TrySend(i.killChannel.Get(instance.Id), true)
|
||||
|
||||
instance.Events = ""
|
||||
|
||||
err := i.instanceRepository.Update(instance)
|
||||
if err != nil {
|
||||
// Do not clear instance.Events on disconnect (upstream PR #187): an
|
||||
// empty events string makes CallWebhook drop every webhook after the
|
||||
// instance reconnects.
|
||||
instance.Connected = false
|
||||
instance.DisconnectReason = "Disconnected by API"
|
||||
if err := i.instanceRepository.UpdateConnected(instance.Id, false, instance.DisconnectReason); err != nil {
|
||||
return instance, err
|
||||
}
|
||||
|
||||
@@ -353,10 +355,7 @@ func (i instances) Logout(instance *instance_model.Instance) (*instance_model.In
|
||||
return instance, err
|
||||
}
|
||||
|
||||
select {
|
||||
case i.killChannel.Get(instance.Id) <- true:
|
||||
case <-time.After(5 * time.Second):
|
||||
}
|
||||
safemap.TrySendTimeout(i.killChannel.Get(instance.Id), true, 5*time.Second)
|
||||
|
||||
i.clientPointer.Delete(instance.Id)
|
||||
i.killChannel.Delete(instance.Id)
|
||||
@@ -368,10 +367,7 @@ func (i instances) Logout(instance *instance_model.Instance) (*instance_model.In
|
||||
if client.IsConnected() {
|
||||
client.Disconnect()
|
||||
|
||||
select {
|
||||
case i.killChannel.Get(instance.Id) <- true:
|
||||
case <-time.After(5 * time.Second):
|
||||
}
|
||||
safemap.TrySendTimeout(i.killChannel.Get(instance.Id), true, 5*time.Second)
|
||||
|
||||
i.clientPointer.Delete(instance.Id)
|
||||
i.killChannel.Delete(instance.Id)
|
||||
@@ -740,10 +736,7 @@ func (i instances) ForceReconnect(instanceId string, number string) error {
|
||||
client := i.clientPointer.Get(instance.Id)
|
||||
client.Disconnect()
|
||||
|
||||
select {
|
||||
case i.killChannel.Get(instance.Id) <- true:
|
||||
case <-time.After(5 * time.Second):
|
||||
}
|
||||
safemap.TrySendTimeout(i.killChannel.Get(instance.Id), true, 5*time.Second)
|
||||
|
||||
i.clientPointer.Delete(instance.Id)
|
||||
i.killChannel.Delete(instance.Id)
|
||||
|
||||
Reference in new issue
Block a user