Skip to content

Commit d32050f

Browse files
author
CHENBING1
committed
Fix the issue where the 'Shutdown' method fails to properly exit.
1 parent 5daa583 commit d32050f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

server/sse.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ func NewTestServer(server *MCPServer, opts ...SSEOption) *httptest.Server {
260260
// It sets up HTTP handlers for SSE and message endpoints.
261261
func (s *SSEServer) Start(addr string) error {
262262
s.mu.Lock()
263-
defer s.mu.Unlock()
264-
265263
if s.srv == nil {
266264
s.srv = &http.Server{
267265
Addr: addr,
@@ -274,22 +272,29 @@ func (s *SSEServer) Start(addr string) error {
274272
return fmt.Errorf("conflicting listen address: WithHTTPServer(%q) vs Start(%q)", s.srv.Addr, addr)
275273
}
276274
}
275+
srv := s.srv
276+
s.mu.Unlock()
277277

278-
return s.srv.ListenAndServe()
278+
return srv.ListenAndServe()
279279
}
280280

281281
// Shutdown gracefully stops the SSE server, closing all active sessions
282282
// and shutting down the HTTP server.
283283
func (s *SSEServer) Shutdown(ctx context.Context) error {
284-
if s.srv != nil {
284+
s.mu.RLock()
285+
srv := s.srv
286+
s.mu.RUnlock()
287+
288+
if srv != nil {
285289
s.sessions.Range(func(key, value interface{}) bool {
286290
if session, ok := value.(*sseSession); ok {
287291
close(session.done)
288292
}
289293
s.sessions.Delete(key)
290294
return true
291295
})
292-
return s.srv.Shutdown(ctx)
296+
297+
return srv.Shutdown(ctx)
293298
}
294299
return nil
295300
}

0 commit comments

Comments
 (0)