fix(server): shut down through fx so buffered reports flush (closes #22)
check / check (push) Failing after 0s
check / check (push) Failing after 0s
The server ran os.Exit at the end of its own goroutine, which raced fx's teardown and could kill the process before reportbuf's OnStop flushed the buffer — losing up to a full flush window of telemetry on every restart, silently and with exit 0. The server now requests shutdown through fx.Shutdowner, so fx runs every OnStop in dependency order. The http.Server is built synchronously in OnStart before the serving goroutine starts, so shutdown can no longer race or nil-deref it; the field is never written and read from two goroutines without a happens-before edge. A listen failure now exits non-zero via fx.ExitCode(1). reportbuf's OnStop is guarded by sync.Once. writeTimeout now exceeds the chi per-request budget, with a comment, so that budget is reachable. Dead startupTime, exitCode, and cancelFunc fields are gone. A new test buffers a report and asserts it reaches disk after the fx lifecycle stops. Model: opus-4-8
This commit is contained in:
@@ -40,11 +40,12 @@ type Params struct {
|
||||
// Buffer accumulates JSON lines in memory and flushes them
|
||||
// to zstd-compressed files on disk.
|
||||
type Buffer struct {
|
||||
buf bytes.Buffer
|
||||
dataDir string
|
||||
done chan struct{}
|
||||
log *slog.Logger
|
||||
mu sync.Mutex
|
||||
buf bytes.Buffer
|
||||
dataDir string
|
||||
done chan struct{}
|
||||
log *slog.Logger
|
||||
mu sync.Mutex
|
||||
stopOnce sync.Once
|
||||
}
|
||||
|
||||
// New creates a Buffer and registers lifecycle hooks to
|
||||
@@ -76,8 +77,13 @@ func New(
|
||||
return nil
|
||||
},
|
||||
OnStop: func(_ context.Context) error {
|
||||
close(b.done)
|
||||
b.flushLocked()
|
||||
// stopOnce makes OnStop idempotent: a second
|
||||
// invocation must not close an already-closed channel
|
||||
// (which would panic) or flush again.
|
||||
b.stopOnce.Do(func() {
|
||||
close(b.done)
|
||||
b.flushLocked()
|
||||
})
|
||||
|
||||
return nil
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user