Skip to content

bug: handleWaitForSync in MCP server can block forever despite deadline #29

Description

@gitcoder89431

Problem

handleWaitForSync in internal/mcpserver/server.go has an outer time.Now().Before(deadline) loop, but each iteration calls WatchEvents(since) which does a 60-second long poll. If Syncthing is slow to respond, each poll iteration can block for up to 60 seconds, meaning the actual wait time can overshoot timeout_seconds by up to 60s:

for time.Now().Before(deadline) {
    st, err := s.client.GetFolderStatus()
    ...
    events, nextID, err := s.client.WatchEvents(since) // blocks up to 60s
    ...
}

With the default 30s timeout, it's possible the loop checks deadline at t=0, starts a 60s long-poll, and doesn't return until t=60.

Fix

Once WatchEvents accepts a context.Context (see related issue), pass a context with the deadline:

ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
events, nextID, err := s.client.WatchEvents(ctx, since)

This ensures the long-poll respects the timeout exactly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions