fix: panic stack without sender, log service error frames, refresh account on reconnect
- handler panic log includes the stack trace and no longer the sender number - error and exception frames from the service are logged at Warn level - Listen looks the account up again after a connection loss (re-link) - document that received but unhandled messages are lost at shutdown Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
788dd0b968
commit
b54aab1534
5 files changed
+162
-3
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
@@ -21,7 +22,13 @@ type Handler func(ctx context.Context, m Message)
|
||||
// exponential backoff (1s to 30s), logged at Warn level. Listen returns
|
||||
// early only for errors that retrying cannot fix: ErrNotLinked,
|
||||
// ErrMultipleAccounts, ErrNotJSONRPC, or a 4xx answer (*APIError) to the websocket
|
||||
// handshake. A panic in h is recovered and logged at Error level.
|
||||
// handshake. A panic in h is recovered and logged at Error level, with its
|
||||
// stack trace. Errors reported by the service on the websocket are logged
|
||||
// at Warn level.
|
||||
//
|
||||
// Messages are acknowledged to Signal as soon as the service receives
|
||||
// them. Messages already received but not yet handled when ctx is done
|
||||
// are lost.
|
||||
//
|
||||
// Only one Listen per account should run at a time.
|
||||
func (c *Client) Listen(ctx context.Context, h Handler) error {
|
||||
@@ -37,6 +44,8 @@ func (c *Client) Listen(ctx context.Context, h Handler) error {
|
||||
if connected {
|
||||
backoff = c.minBackoff
|
||||
}
|
||||
// The account may have been re-linked meanwhile: look it up again.
|
||||
c.forgetAccount()
|
||||
c.log.Warn("signalbot: receive connection lost, retrying", "error", err, "retry_in", backoff)
|
||||
t := time.NewTimer(backoff)
|
||||
select {
|
||||
@@ -106,6 +115,10 @@ func (c *Client) listenOnce(ctx context.Context, h Handler) (connected bool, err
|
||||
}()
|
||||
|
||||
for data := range frames {
|
||||
if msg, ok := serviceError(data); ok {
|
||||
c.log.Warn("signalbot: receive error from service", "error", msg)
|
||||
continue
|
||||
}
|
||||
m, ok := parseEnvelope(data, acct)
|
||||
if !ok {
|
||||
continue
|
||||
@@ -123,7 +136,7 @@ func (c *Client) listenOnce(ctx context.Context, h Handler) (connected bool, err
|
||||
func (c *Client) handle(ctx context.Context, h Handler, m Message) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
c.log.Error("signalbot: handler panic", "panic", r, "from", m.From, "group", m.GroupID)
|
||||
c.log.Error("signalbot: handler panic", "panic", r, "group", m.GroupID, "stack", string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
h(ctx, m)
|
||||
|
||||
Reference in new issue
Block a user