Skip to content

Commit

Permalink
stream: fix goroutine leak
Browse files Browse the repository at this point in the history
  • Loading branch information
criyle committed Feb 6, 2024
1 parent aa41950 commit 581b925
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions cmd/go-judge/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ func Start(baseCtx context.Context, s Stream, w worker.Worker, srcPrefix []strin
if err != nil {
return fmt.Errorf("convert exec request: %w", err)
}
logger.Sugar().Debugf("request: %+v", rq)
defer func() {
closeFunc := func() {
for _, f := range streamIn {
f.Close()
}
streamIn = nil
for _, f := range streamOut {
f.Close()
}
}()
streamOut = nil
}
defer closeFunc()

logger.Sugar().Debugf("request: %+v", rq)

var wg errgroup.Group
execCtx, execCancel := context.WithCancel(baseCtx)
Expand Down Expand Up @@ -120,6 +124,8 @@ func Start(baseCtx context.Context, s Stream, w worker.Worker, srcPrefix []strin
err = sendLoop(ctx, s, outCh, rtCh, logger)

cancel()
closeFunc()
streamOut = nil
wg.Wait()
return err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/go-judge/ws_executor/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ func (h *wsHandle) handleStream(c *gin.Context) {

w := &streamWrapper{ctx: ctx, conn: conn, sendCh: make(chan stream.Response)}
go w.sendLoop()
stream.Start(ctx, w, h.worker, h.srcPrefix, h.logger)
if err := stream.Start(ctx, w, h.worker, h.srcPrefix, h.logger); err != nil {
h.logger.Sugar().Debugln("stream start: ", err)
c.Error(err)
}
}

type contextMap struct {
Expand Down

0 comments on commit 581b925

Please sign in to comment.