Skip to content

Commit 578b94f

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

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

core/http/endpoints/openai/chat.go

Lines changed: 21 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,26 @@ func ChatEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator
516517

517518
}
518519

520+
// Monitor connection for client disconnection during non-streaming requests
521+
// We access the connection directly via c.Context().Conn() to monitor it
522+
// during ComputeChoices execution, not after the response is sent
523+
// see: https://github.com/mudler/LocalAI/pull/7187#issuecomment-3506720906
524+
var conn net.Conn = c.Context().Conn()
525+
if conn != nil {
526+
go func() {
527+
buf := make([]byte, 1)
528+
for {
529+
_, err := conn.Read(buf)
530+
if err != nil {
531+
// Connection closed - cancel the context to stop gRPC call
532+
log.Debug().Msgf("Client disconnected during non-streaming request, cancelling")
533+
input.Cancel()
534+
return
535+
}
536+
}
537+
}()
538+
}
539+
519540
result, tokenUsage, err := ComputeChoices(
520541
input,
521542
predInput,

0 commit comments

Comments
 (0)