Skip to content

Commit 76df1d5

Browse files
committed
Using mutex for session cancellation
Signed-off-by: Trung Nguyen <trung.nguyen@docker.com>
1 parent b81dab1 commit 76df1d5

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

pkg/server/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os"
1616
"path/filepath"
1717
"strings"
18+
"sync"
1819
"time"
1920

2021
"dario.cat/mergo"
@@ -41,6 +42,7 @@ type Server struct {
4142
e *echo.Echo
4243
runtimes map[string]runtime.Runtime
4344
runtimeCancels map[string]context.CancelFunc
45+
cancelsMu sync.RWMutex
4446
sessionStore session.Store
4547
agentsDir string
4648
runConfig config.RuntimeConfig
@@ -867,11 +869,13 @@ func (s *Server) deleteSession(c echo.Context) error {
867869
sessionID := c.Param("id")
868870

869871
// Cancel the runtime context if it's still running
872+
s.cancelsMu.Lock()
870873
if cancel, exists := s.runtimeCancels[sessionID]; exists {
871874
slog.Debug("Cancelling runtime for session", "session_id", sessionID)
872875
cancel()
873876
delete(s.runtimeCancels, sessionID)
874877
}
878+
s.cancelsMu.Unlock()
875879

876880
// Clean up the runtime
877881
if _, exists := s.runtimes[sessionID]; exists {
@@ -966,9 +970,13 @@ func (s *Server) runAgent(c echo.Context) error {
966970

967971
// Create a cancellable context for this stream
968972
streamCtx, cancel := context.WithCancel(c.Request().Context())
973+
s.cancelsMu.Lock()
969974
s.runtimeCancels[sess.ID] = cancel
975+
s.cancelsMu.Unlock()
970976
defer func() {
977+
s.cancelsMu.Lock()
971978
delete(s.runtimeCancels, sess.ID)
979+
s.cancelsMu.Unlock()
972980
}()
973981

974982
streamChan := rt.RunStream(streamCtx, sess)

0 commit comments

Comments
 (0)