Skip to content

Commit

Permalink
fix: fix stop signal calling stack
Browse files Browse the repository at this point in the history
fix: fix stop signal calling stack
Co-Authored-By: Minghan Zhang <[email protected]>
  • Loading branch information
Sh1n3zZ and zmh-program committed Jun 28, 2024
1 parent b0a684c commit 3dffa7f
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions manager/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"chat/globals"
"chat/manager/conversation"
"chat/utils"
"time"

"database/sql"
"errors"
Expand Down Expand Up @@ -51,17 +52,24 @@ type partialChunk struct {
func createStopSignal(conn *Connection) chan bool {
stopSignal := make(chan bool, 1)
go func(conn *Connection, stopSignal chan bool) {
ticker := time.NewTicker(100 * time.Millisecond)
defer func() {
ticker.Stop()
if r := recover(); r != nil && !strings.Contains(fmt.Sprintf("%s", r), "closed channel") {
stack := debug.Stack()
globals.Warn(fmt.Sprintf("caught panic from stop signal: %s\n%s", r, stack))
}
}()

for {
if conn.PeekStop() != nil {
stopSignal <- true
break
select {
case <-ticker.C:
state := conn.PeekStop() != nil // check the stop state
stopSignal <- state

if state {
break
}
}
}
}(conn, stopSignal)
Expand Down Expand Up @@ -169,16 +177,19 @@ func createChatTask(

return hit, sendPackError
}
case <-stopSignal:
globals.Info(fmt.Sprintf("client stopped the chat request (model: %s, client: %s)", model, conn.GetCtx().ClientIP()))
_ = conn.SendClient(globals.ChatSegmentResponse{
Quota: buffer.GetQuota(),
End: true,
Plan: plan,
})
interruptSignal <- errors.New("signal")
case signal := <-stopSignal:
// if stop signal is received
if signal {
globals.Info(fmt.Sprintf("client stopped the chat request (model: %s, client: %s)", model, conn.GetCtx().ClientIP()))
_ = conn.SendClient(globals.ChatSegmentResponse{
Quota: buffer.GetQuota(),
End: true,
Plan: plan,
})
interruptSignal <- errors.New("signal")

return
return
}
}
}
}
Expand Down

0 comments on commit 3dffa7f

Please sign in to comment.