Skip to content

Commit 38125d9

Browse files
committed
workaround fasthttp
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
1 parent 099cc13 commit 38125d9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

core/http/endpoints/openai/chat.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"encoding/json"
77
"fmt"
8+
"net"
89
"time"
910

1011
"github.com/gofiber/fiber/v2"
@@ -516,6 +517,29 @@ func ChatEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator
516517

517518
}
518519

520+
// NOTE: this is a bad WORKAROUND! We should find a better way to handle this.
521+
// Fasthttp doesn't support context cancellation from the caller
522+
// for non-streaming requests, so we need to monitor the connection directly.
523+
// Monitor connection for client disconnection during non-streaming requests
524+
// We access the connection directly via c.Context().Conn() to monitor it
525+
// during ComputeChoices execution, not after the response is sent
526+
// see: https://github.com/mudler/LocalAI/pull/7187#issuecomment-3506720906
527+
var conn net.Conn = c.Context().Conn()
528+
if conn != nil {
529+
go func() {
530+
buf := make([]byte, 1)
531+
for {
532+
_, err := conn.Read(buf)
533+
if err != nil {
534+
// Connection closed - cancel the context to stop gRPC call
535+
log.Debug().Msgf("Cancelling GRPC call")
536+
input.Cancel()
537+
return
538+
}
539+
}
540+
}()
541+
}
542+
519543
result, tokenUsage, err := ComputeChoices(
520544
input,
521545
predInput,

0 commit comments

Comments
 (0)