Skip to content

Commit 5b5138a

Browse files
committed
fix blocked shutdown due to ui
1 parent 4b97168 commit 5b5138a

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

proxy/proxymanager.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package proxy
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"io"
78
"mime/multipart"
@@ -33,6 +34,10 @@ type ProxyManager struct {
3334
muxLogger *LogMonitor
3435

3536
processGroups map[string]*ProcessGroup
37+
38+
// shutdown signaling
39+
shutdownCtx context.Context
40+
shutdownCancel context.CancelFunc
3641
}
3742

3843
func New(config Config) *ProxyManager {
@@ -63,6 +68,8 @@ func New(config Config) *ProxyManager {
6368
upstreamLogger.SetLogLevel(LevelInfo)
6469
}
6570

71+
shutdownCtx, shutdownCancel := context.WithCancel(context.Background())
72+
6673
pm := &ProxyManager{
6774
config: config,
6875
ginEngine: gin.New(),
@@ -72,6 +79,9 @@ func New(config Config) *ProxyManager {
7279
upstreamLogger: upstreamLogger,
7380

7481
processGroups: make(map[string]*ProcessGroup),
82+
83+
shutdownCtx: shutdownCtx,
84+
shutdownCancel: shutdownCancel,
7585
}
7686

7787
// create the process groups
@@ -261,6 +271,7 @@ func (pm *ProxyManager) Shutdown() {
261271
}(processGroup)
262272
}
263273
wg.Wait()
274+
pm.shutdownCancel()
264275
}
265276

266277
func (pm *ProxyManager) swapProcessGroup(requestedModel string) (*ProcessGroup, string, error) {

proxy/proxymanager_api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,9 @@ func (pm *ProxyManager) apiListModelsSSE(c *gin.Context) {
9696
}
9797
})()
9898

99-
<-c.Request.Context().Done()
99+
select {
100+
case <-c.Request.Context().Done():
101+
case <-pm.shutdownCtx.Done():
102+
}
103+
100104
}

proxy/proxymanager_loghandlers.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ func (pm *ProxyManager) streamLogsHandler(c *gin.Context) {
5252
}
5353
}
5454

55-
cancelFn := logger.OnLogData(func(data []byte) {
55+
defer logger.OnLogData(func(data []byte) {
5656
if c != nil && c.Writer != nil {
5757
c.Writer.Write(data)
5858
flusher.Flush()
5959
}
60-
})
61-
defer cancelFn()
62-
<-c.Request.Context().Done()
60+
})()
61+
62+
select {
63+
case <-c.Request.Context().Done():
64+
case <-pm.shutdownCtx.Done():
65+
}
66+
6367
}
6468

6569
func (pm *ProxyManager) streamLogsHandlerSSE(c *gin.Context) {
@@ -85,14 +89,17 @@ func (pm *ProxyManager) streamLogsHandlerSSE(c *gin.Context) {
8589
}
8690
}
8791

88-
cancelFn := logger.OnLogData(func(data []byte) {
92+
defer logger.OnLogData(func(data []byte) {
8993
if c != nil && c.Writer != nil {
9094
c.SSEvent("message", string(data))
9195
c.Writer.Flush()
9296
}
93-
})
94-
defer cancelFn()
95-
<-c.Request.Context().Done()
97+
})()
98+
99+
select {
100+
case <-c.Request.Context().Done():
101+
case <-pm.shutdownCtx.Done():
102+
}
96103
}
97104

98105
// getLogger searches for the appropriate logger based on the logMonitorId

0 commit comments

Comments
 (0)