feat(chat): Delay MCP servers reload to avoid websocket initialization race condition

* Introduced a delay before reloading MCP servers to ensure the websocket server is ready.
* This change prevents potential race conditions during initialization.
This commit is contained in:
qianlifeng 2025-06-09 09:58:24 +08:00
parent c1c6ced55d
commit 1657a44f19
No known key found for this signature in database
2 changed files with 17 additions and 1 deletions

View File

@ -278,7 +278,11 @@ func (r *AIChatPlugin) Init(ctx context.Context, initParams plugin.InitParams) {
}
})
r.reloadMCPServers(ctx)
// Delay MCP servers reload to avoid websocket server initialization race condition
util.Go(ctx, "reload MCP servers", func() {
time.Sleep(time.Millisecond * 1000) // Wait for websocket server to be ready
r.reloadMCPServers(util.NewTraceContext())
})
}
func (r *AIChatPlugin) IsAutoFocusToChatInputWhenOpenWithQueryHotkey(ctx context.Context) bool {

View File

@ -127,6 +127,12 @@ func serveAndWait(ctx context.Context, port int) {
}
func requestUI(ctx context.Context, request WebsocketMsg) error {
// Check if melody websocket server is initialized
if m == nil {
logger.Warn(ctx, fmt.Sprintf("websocket server not ready, skipping UI request: %s", request.Method))
return fmt.Errorf("websocket server not initialized")
}
request.Type = WebsocketMsgTypeRequest
request.Success = true
marshalData, marshalErr := json.Marshal(request)
@ -141,6 +147,12 @@ func requestUI(ctx context.Context, request WebsocketMsg) error {
}
func responseUI(ctx context.Context, response WebsocketMsg) {
// Check if melody websocket server is initialized
if m == nil {
logger.Warn(ctx, fmt.Sprintf("websocket server not ready, skipping UI response: %s", response.Method))
return
}
response.Type = WebsocketMsgTypeResponse
marshalData, marshalErr := json.Marshal(response)
if marshalErr != nil {