Record Remote connection failure diagnostics
This commit is contained in:
1 parent
623011fc87
commit
74830fa676
4 files changed
+147
-1
No files matched your search
@@ -89,6 +89,7 @@ func (c *coordinator) read_events() {
|
||||
return
|
||||
case event, ok := <-c.client.Events():
|
||||
if !ok {
|
||||
diagnostic(0, "event-stream", errClosed, 0)
|
||||
return
|
||||
}
|
||||
c.mu.Lock()
|
||||
@@ -203,6 +204,7 @@ func (c *coordinator) connect(action sdk.SessionEvent, e endpoint, key string) e
|
||||
func (c *coordinator) stop_locked(err error) {
|
||||
if c.active != nil {
|
||||
if c.active.ctx.Err() == nil {
|
||||
diagnostic(c.active.epoch, "stopping", err, 0)
|
||||
c.active.err = err
|
||||
}
|
||||
c.active.cancel() // Socket cancellation never waits for reader RPC cleanup.
|
||||
@@ -263,6 +265,9 @@ func (c *coordinator) update(a *connectionAttempt, s *session) bool {
|
||||
if a.ctx.Err() != nil {
|
||||
return false
|
||||
}
|
||||
if c.status.state != s.state {
|
||||
diagnostic(a.epoch, s.state, nil, 0)
|
||||
}
|
||||
c.status.state, c.status.peer, c.status.ignored = s.state, s.peer.ID, s.ignored
|
||||
return true
|
||||
}
|
||||
@@ -273,13 +278,17 @@ func (c *coordinator) update(a *connectionAttempt, s *session) bool {
|
||||
func (c *coordinator) call(method string, params, result any) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if c.client.Call(ctx, method, params, result) != nil {
|
||||
started := time.Now()
|
||||
if err := c.client.Call(ctx, method, params, result); err != nil {
|
||||
diagnostic(0, "reader-call", err, time.Since(started), method)
|
||||
return errLease
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *coordinator) run(a *connectionAttempt, approval sdk.SessionEvent, e endpoint, key string) {
|
||||
started := time.Now()
|
||||
diagnostic(a.epoch, "opening", nil, 0)
|
||||
var lease sdk.RemoteLease
|
||||
var conn *relayConn
|
||||
var stream *speechStream
|
||||
@@ -318,6 +327,7 @@ func (c *coordinator) run(a *connectionAttempt, approval sdk.SessionEvent, e end
|
||||
if failure != nil {
|
||||
c.status.state = "failed"
|
||||
}
|
||||
diagnostic(a.epoch, c.status.state, failure, time.Since(started))
|
||||
c.active = nil
|
||||
close(a.done)
|
||||
c.mu.Unlock()
|
||||
@@ -351,6 +361,7 @@ func (c *coordinator) run(a *connectionAttempt, approval sdk.SessionEvent, e end
|
||||
c.mu.Lock()
|
||||
if a.ctx.Err() == nil {
|
||||
c.status.state = "connecting"
|
||||
diagnostic(a.epoch, "connecting", nil, time.Since(started))
|
||||
}
|
||||
c.mu.Unlock()
|
||||
stream = newSpeechStream(lease, approval)
|
||||
@@ -404,11 +415,13 @@ func (c *coordinator) run(a *connectionAttempt, approval sdk.SessionEvent, e end
|
||||
if err != nil {
|
||||
if a.ctx.Err() == nil {
|
||||
failure = err
|
||||
diagnostic(a.epoch, "relay-read", err, time.Since(started))
|
||||
}
|
||||
return
|
||||
}
|
||||
accepted, err := s.accept(a.epoch, m)
|
||||
if err != nil {
|
||||
diagnostic(a.epoch, "relay-message", err, time.Since(started))
|
||||
failure = err
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package remote
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
sdk "git.stormux.org/storm/cthulhu/sdk/go/tentacle"
|
||||
)
|
||||
|
||||
func diagnosticReason(err error) string {
|
||||
if err == nil {
|
||||
return "none"
|
||||
}
|
||||
for _, item := range []struct {
|
||||
err error
|
||||
name string
|
||||
}{
|
||||
{context.Canceled, "cancelled"}, {context.DeadlineExceeded, "timeout"},
|
||||
{errTimeout, "timeout"}, {errClosed, "closed"}, {errNetwork, "network"},
|
||||
{errTrust, "trust"}, {errProtocol, "protocol"}, {errLimit, "limit"},
|
||||
{errPeer, "peer"}, {errLease, "lease"}, {errKeyboard, "keyboard"},
|
||||
{errKeyboardState, "keyboard"}, {errState, "state"}, {errStale, "stale"},
|
||||
} {
|
||||
if errors.Is(err, item.err) {
|
||||
return item.name
|
||||
}
|
||||
}
|
||||
var rpc *sdk.SessionError
|
||||
if errors.As(err, &rpc) {
|
||||
return "lease"
|
||||
}
|
||||
return "other"
|
||||
}
|
||||
|
||||
// Only constant classifications cross stderr. Never serialize an error, relay
|
||||
// frame, channel key or lease token. Older readers safely discard these records.
|
||||
func writeDiagnostic(w io.Writer, epoch uint64, phase string, err error, elapsed time.Duration, operation ...string) {
|
||||
op := ""
|
||||
if len(operation) != 0 {
|
||||
op = operation[0]
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(struct {
|
||||
Operation string `json:"operation,omitempty"`
|
||||
Version int `json:"cthulhu_diagnostic"`
|
||||
Epoch uint64 `json:"epoch"`
|
||||
Phase string `json:"phase"`
|
||||
Reason string `json:"reason"`
|
||||
ElapsedMS int64 `json:"elapsed_ms"`
|
||||
}{op, 1, epoch, phase, diagnosticReason(err), elapsed.Milliseconds()})
|
||||
}
|
||||
|
||||
// Stay below the reader's lifetime stderr limit. Logging itself must not
|
||||
// terminate an otherwise healthy Tentacle after many connection attempts.
|
||||
type diagnosticSink struct {
|
||||
sync.Mutex
|
||||
writer io.Writer
|
||||
bytes int
|
||||
exhausted bool
|
||||
}
|
||||
|
||||
var diagnosticOutput = diagnosticSink{writer: os.Stderr}
|
||||
|
||||
func (sink *diagnosticSink) write(record []byte) {
|
||||
sink.Lock()
|
||||
defer sink.Unlock()
|
||||
if sink.exhausted {
|
||||
return
|
||||
}
|
||||
if sink.bytes+len(record) > 48*1024 {
|
||||
sink.exhausted = true
|
||||
writeDiagnostic(sink.writer, 0, "diagnostics", errLimit, 0)
|
||||
return
|
||||
}
|
||||
sink.bytes += len(record)
|
||||
_, _ = sink.writer.Write(record)
|
||||
}
|
||||
|
||||
func diagnostic(epoch uint64, phase string, err error, elapsed time.Duration, operation ...string) {
|
||||
var record bytes.Buffer
|
||||
writeDiagnostic(&record, epoch, phase, err, elapsed, operation...)
|
||||
diagnosticOutput.write(record.Bytes())
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDiagnosticFailureClassification(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
err error
|
||||
reason string
|
||||
}{
|
||||
{errTimeout, "timeout"}, {errNetwork, "network"}, {errClosed, "closed"},
|
||||
{errPeer, "peer"}, {errProtocol, "protocol"}, {errLease, "lease"},
|
||||
{context.Canceled, "cancelled"}, {errLimit, "limit"},
|
||||
} {
|
||||
var out bytes.Buffer
|
||||
writeDiagnostic(&out, 7, "relay-read", fmt.Errorf("private detail: %w", test.err), time.Second)
|
||||
if strings.Contains(out.String(), "private") || !strings.Contains(out.String(), `"reason":"`+test.reason+`"`) || !strings.Contains(out.String(), `"epoch":7`) {
|
||||
t.Fatal(out.String())
|
||||
}
|
||||
}
|
||||
if diagnosticReason(errors.New("secret")) != "other" {
|
||||
t.Fatal("unknown error exposed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiagnosticLifetimeBudget(t *testing.T) {
|
||||
var out bytes.Buffer
|
||||
sink := diagnosticSink{writer: &out}
|
||||
for i := 0; i < 1000; i++ {
|
||||
sink.write([]byte(strings.Repeat("x", 1024)))
|
||||
}
|
||||
if !sink.exhausted || out.Len() >= 64*1024 || strings.Count(out.String(), `"phase":"diagnostics"`) != 1 {
|
||||
t.Fatalf("unexpected diagnostic limit: %d bytes", out.Len())
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,7 @@ func (c *coordinator) key_output(a *connectionAttempt, stream *speechStream, con
|
||||
c.mu.Unlock()
|
||||
}
|
||||
stop := func(err error) {
|
||||
diagnostic(a.epoch, "keyboard", err, 0)
|
||||
c.mu.Lock()
|
||||
c.stop_locked(err)
|
||||
c.mu.Unlock()
|
||||
|
||||
Reference in new issue
Block a user