Skip to content

Commit

Permalink
Merge pull request #2861 from redpanda-data/shutdown
Browse files Browse the repository at this point in the history
ollama: fix subprocess shutdown
  • Loading branch information
rockwotj authored Sep 10, 2024
2 parents 5d8b9a0 + 6e6e4c1 commit f8f5e06
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions internal/impl/ollama/subprocess_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/cenkalti/backoff/v4"
"golang.org/x/sync/errgroup"

"github.com/redpanda-data/connect/v4/internal/singleton"
)
Expand Down Expand Up @@ -154,8 +155,26 @@ var ollamaProcess = singleton.New(singleton.Config[*exec.Cmd]{
if cmd.Process == nil {
return nil
}
if err := cmd.Process.Kill(); err != nil {
if err := cmd.Process.Signal(os.Interrupt); err != nil {
return err
}
return cmd.Wait()
// Wait for 3 seconds for the process to exit gracefully then kill it by force.
stop := make(chan any, 1)
wg := errgroup.Group{}
wg.Go(func() error {
err := cmd.Wait()
stop <- struct{}{}
return err
})
wg.Go(func() error {
select {
case <-stop:
case <-time.After(3 * time.Second):
// Ignore errors if there is a race
// and the process has already closed.
_ = cmd.Process.Kill()
}
return nil
})
return wg.Wait()
}})

0 comments on commit f8f5e06

Please sign in to comment.