Files
Willie 6a9e9efbe2 fix(server): release the config lock before slow plugin, Pi and MCP reads (#747)
Every /api/ request reloads config under the write lock of s.mu. The
Plugins page's inventory (Agent CLIs) and update check (network) held
the read lock for their whole run, and Go's RWMutex makes later readers
queue behind a waiting writer. Switching pages or reloading while a
check ran froze the whole dashboard until the check finished.

Build each service under the read lock and release it before the slow
work: plugin list, discover and preview; Pi extensions view and
preview, which run the Pi CLI; and MCP check, which looks up hosts and
probes servers live. Writes still take the write lock, and apply paths
still compare revisions, so a read that races a write is refused at
apply time rather than applied.
2026-11-02 15:13:01 +08:00

65 lines
2.0 KiB
Go

package server
import (
"net/http"
"skillshare/internal/mcp"
"skillshare/internal/version"
)
// handleMCPCheck answers `skillshare mcp check --json` for the dashboard. It writes no
// config and no operation log entry. Findings that are errors still return 200; only a
// check that cannot run fails the request. ?dns=0 skips host lookups. ?live=1 also probes
// the servers, as `mcp check --live` does, and each server's live result lists the tool
// names it reported; it also pins and compares tool definitions as the CLI does, which
// writes mcp/tool-pins.json, but never accepts a change. Repeated ?name= limits the check
// to those servers.
func (s *Server) handleMCPCheck(w http.ResponseWriter, r *http.Request) {
// Release s.mu before the lookups and probes: every other API request waits on it.
s.mu.RLock()
service := s.mcpService()
s.mu.RUnlock()
query := r.URL.Query()
report, err := service.Check(mcp.CheckOptions{
SkipDNS: query.Get("dns") == "0",
Live: query.Get("live") == "1",
Names: query["name"],
ClientVersion: version.Version,
})
if err != nil {
writeError(w, 400, err.Error())
return
}
writeJSON(w, report)
}
// handleMCPProbe probes the server in the request, as the dashboard's form describes it
// before it is saved, the way ?live=1 probes a saved one. It saves and syncs nothing.
func (s *Server) handleMCPProbe(w http.ResponseWriter, r *http.Request) {
var body mcpRequest
if !decodeMCPRequest(w, r, &body) {
return
}
if body.Mutation.Server == nil {
writeError(w, 400, "server is required")
return
}
s.mu.RLock()
service, ok := s.mcpProjectService(w, body.Mutation.Project)
s.mu.RUnlock()
if !ok {
return
}
// A project server starts in its root, as a live check of a saved one does.
root := ""
if body.Mutation.Project != "" {
root = service.ProjectRoot
}
result, err := mcp.ProbeDraft(*body.Mutation.Server, root, mcp.CheckOptions{ClientVersion: version.Version})
if err != nil {
writeError(w, 400, err.Error())
return
}
writeJSON(w, result)
}