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
6 changes: 6 additions & 0 deletions internal/process/process_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ func (p *ProcessCommand) doStart(startCtx context.Context, healthCheckTimeout ti
p.proxyLogger.Debugf("<%s> Executing start command: %s, env: %s", p.id, strings.Join(args, " "), strings.Join(p.config.Env, ", "))

cmdDone := make(chan struct{})
// Reset the process log so a premature exit surfaces only output from
// the current launch, not accumulated history from earlier attempts.
p.processLogger.Clear()
if err := cmd.Start(); err != nil {
cmdCancel()
return startResult{err: fmt.Errorf("failed to start command '%s': %w", strings.Join(args, " "), err)}
Expand Down Expand Up @@ -456,6 +459,9 @@ func (p *ProcessCommand) doStart(startCtx context.Context, healthCheckTimeout ti
}
prematureExit := func() startResult {
cmdCancel()
if output := p.processLogger.GetHistory(); len(output) > 0 {
return startResult{err: fmt.Errorf("upstream command exited prematurely:\n%s", strings.TrimSpace(string(output)))}
}
return startResult{err: fmt.Errorf("upstream command exited prematurely")}
}

Expand Down
28 changes: 28 additions & 0 deletions internal/process/process_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,31 @@ func TestProcessCommand_ConcurrentRunStop(t *testing.T) {
}
}
}

func TestProcessCommand_PrematureExitSurfacesOutput(t *testing.T) {
skipIfNoSimpleResponder(t)

// An undefined flag makes simple-responder (a Go flag program) print an error
// to stderr and exit before it becomes ready, mirroring a real upstream server
// rejecting a bad argument.
cmd, port := simpleResponderCmd(t, "-nonexistent-flag")
p := newProcessCommand(t, config.ModelConfig{
Cmd: cmd,
Proxy: fmt.Sprintf("http://127.0.0.1:%d", port),
CheckEndpoint: "/health",
HealthCheckTimeout: 10,
})
t.Cleanup(func() { p.Stop(testStopTimeout) })

err := p.Run(testStartTimeout)
if err == nil {
t.Fatal("expected Run() to fail for a process that exits prematurely")
}
if !strings.Contains(err.Error(), "exited prematurely") {
t.Errorf("expected a premature-exit error, got: %v", err)
}
// The captured upstream output must be surfaced so the real cause is visible.
if !strings.Contains(err.Error(), "nonexistent-flag") {
t.Errorf("expected error to include upstream output, got: %v", err)
}
}