Skip to content
Open
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
22 changes: 20 additions & 2 deletions cmd/root/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
return cmd
}

func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error {
func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) (retErr error) {
telemetry.TrackCommand("serve", append([]string{"api"}, args...))

ctx := cmd.Context()
Expand All @@ -65,6 +65,10 @@
defer func() {
if err := cleanup(); err != nil {
slog.Error("Failed to cleanup fake proxy", "error", err)
// Only set return error if no other error occurred
if retErr == nil {
retErr = fmt.Errorf("failed to cleanup fake proxy: %w", err)
}
}
}()

Expand All @@ -76,6 +80,10 @@
defer func() {
if err := recordCleanup(); err != nil {
slog.Error("Failed to cleanup recording proxy", "error", err)
// Only set return error if no other error occurred
if retErr == nil {
retErr = fmt.Errorf("failed to cleanup recording proxy: %w", err)
}
}
}()

Expand All @@ -87,7 +95,14 @@
if err != nil {
return err
}
defer lnCleanup()
defer func() {
if err := lnCleanup(); err != nil {

Check failure on line 99 in cmd/root/api.go

View workflow job for this annotation

GitHub Actions / lint

lnCleanup() (no value) used as value) (typecheck)

Check failure on line 99 in cmd/root/api.go

View workflow job for this annotation

GitHub Actions / lint

lnCleanup() (no value) used as value (typecheck)

Check failure on line 99 in cmd/root/api.go

View workflow job for this annotation

GitHub Actions / build-and-test

lnCleanup() (no value) used as value
slog.Error("Failed to cleanup listener", "error", err)
if retErr == nil {
retErr = fmt.Errorf("failed to cleanup listener: %w", err)
}
}
}()

out.Println("Listening on", ln.Addr().String())

Expand All @@ -106,6 +121,9 @@
defer func() {
if err := sessionStore.Close(); err != nil {
slog.Error("Failed to close session store", "error", err)
if retErr == nil {
retErr = fmt.Errorf("failed to close session store: %w", err)
}
}
}()

Expand Down
6 changes: 2 additions & 4 deletions cmd/root/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ func parseModelShorthand(s string) *latest.ModelConfig {
}

// newListener creates a TCP listener and returns a cleanup function that
// must be deferred by the caller. The cleanup function closes the listener.
// The listener is also closed if the context is cancelled, which unblocks
// any in-progress Serve call.
// must be deferred by the caller. The cleanup function triggers the
// context.AfterFunc to close the listener.
func newListener(ctx context.Context, addr string) (net.Listener, func(), error) {
ln, err := server.Listen(ctx, addr)
if err != nil {
Expand All @@ -146,7 +145,6 @@ func newListener(ctx context.Context, addr string) (net.Listener, func(), error)
})
cleanup := func() {
stop()
_ = ln.Close()
}
return ln, cleanup, nil
}
11 changes: 10 additions & 1 deletion cmd/root/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (f *runExecFlags) runRunCommand(cmd *cobra.Command, args []string) error {
return f.runOrExec(ctx, out, args, useTUI)
}

func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []string, useTUI bool) error {
func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []string, useTUI bool) (retErr error) {
slog.Debug("Starting agent", "agent", f.agentName)

// Start CPU profiling if requested
Expand Down Expand Up @@ -216,6 +216,9 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s
defer func() {
if err := fakeCleanup(); err != nil {
slog.Error("Failed to cleanup fake proxy", "error", err)
if retErr == nil {
retErr = fmt.Errorf("failed to cleanup fake proxy: %w", err)
}
}
}()

Expand All @@ -228,6 +231,9 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s
defer func() {
if err := recordCleanup(); err != nil {
slog.Error("Failed to cleanup recording proxy", "error", err)
if retErr == nil {
retErr = fmt.Errorf("failed to cleanup recording proxy: %w", err)
}
}
}()
out.Println("Recording mode enabled, cassette: " + cassettePath)
Expand Down Expand Up @@ -260,6 +266,9 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s
defer func() {
if err := rt.Close(); err != nil {
slog.Error("Failed to close runtime", "error", err)
if retErr == nil {
retErr = fmt.Errorf("failed to close runtime: %w", err)
}
}
}()
var initialTeamCleanupOnce sync.Once
Expand Down
Loading