Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

ggml: Fix race that resulted in "context canceled" when loading

Jesse Grossa6ef73f4f26a
diff --git a/ml/backend/ggml/ggml.go b/ml/backend/ggml/ggml.go
index 177ac6fd0..cd760643c 100644
--- a/ml/backend/ggml/ggml.go
+++ b/ml/backend/ggml/ggml.go
@@ -341,6 +341,11 @@ func New(ctx context.Context, r *os.File, params ml.BackendParams) (ml.Backend,
var s uint64
for s < t.Size() {
+ // Stop if either the parent context has been canceled or if any of the other tensors returned an error
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+
n, err := io.ReadFull(sr, bts[:min(len(bts), int(t.Size()-s))])
if err != nil {
slog.Warn("file read error", "file", r.Name(), "error", err)
@@ -363,14 +368,6 @@ func New(ctx context.Context, r *os.File, params ml.BackendParams) (ml.Backend,
})
}
- // start a goroutine to cancel the errgroup if the parent context is done
- go func() {
- <-ctx.Done()
- g.Go(func() error {
- return ctx.Err()
- })
- }()
-
if err := g.Wait(); err != nil {
return nil, err
}