Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## Unreleased

- Interrupt bridge reconnect backoff promptly on SIGINT/SIGTERM. Thanks @SebTardif! (#5)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ printf hey computer turn on the lights

Each line on the FIFO becomes a `voice.transcript`; chat responses from the `main` session are spoken via `espeak-ng`.

SIGINT and SIGTERM interrupt reconnect backoff immediately, including when the bridge is unavailable.

## systemd example

Minimal steps:
Expand Down
14 changes: 12 additions & 2 deletions cmd/clawgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,12 @@ func runNode(cfg NodeConfig) error {
client, err := connectBridge(cfg.BridgeAddr)
if err != nil {
logf("bridge connect failed: %v", err)
time.Sleep(backoff)
if err := sleepContext(ctx, backoff); err != nil {
if mdnsCleanup != nil {
mdnsCleanup()
}
return nil
}
if backoff < 15*time.Second {
backoff *= 2
if backoff > 15*time.Second {
Expand Down Expand Up @@ -482,7 +487,12 @@ func runNode(cfg NodeConfig) error {
}
return nil
}
time.Sleep(backoff)
if err := sleepContext(ctx, backoff); err != nil {
if mdnsCleanup != nil {
mdnsCleanup()
}
return nil
}
if backoff < 15*time.Second {
backoff *= 2
if backoff > 15*time.Second {
Expand Down
17 changes: 17 additions & 0 deletions cmd/clawgo/sleep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"context"
"time"
)

func sleepContext(ctx context.Context, d time.Duration) error {
t := time.NewTimer(d)
defer t.Stop()
select {
case <-ctx.Done():
return ctx.Err()
case <-t.C:
return nil
}
}
44 changes: 44 additions & 0 deletions cmd/clawgo/sleep_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"errors"
"testing"
"time"
)

func TestSleepContextCanceledReturnsCanceled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

err := sleepContext(ctx, 30*time.Second)
if !errors.Is(err, context.Canceled) {
t.Fatalf("sleepContext(canceled, 30s) = %v, want context.Canceled", err)
}
}

func TestSleepContextCancelDuringWait(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() {
done <- sleepContext(ctx, 30*time.Second)
}()
cancel()

var err error
select {
case err = <-done:
case <-time.After(2 * time.Second):
t.Fatal("sleepContext did not return after cancel")
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("sleepContext canceled mid-wait = %v, want context.Canceled", err)
}
}

func TestSleepContextCompletesWhenContextStaysOpen(t *testing.T) {
err := sleepContext(context.Background(), time.Millisecond)
if err != nil {
t.Fatalf("sleepContext(background, 1ms) = %v, want nil", err)
}
}