library: the MCP Registry is asked with a budget of its own (#3043, shupivot)

The market's one client gave every request 20 seconds, and the Registry's
search answers in 13-37 s often enough (9 of the reporter's 33 tries past
13 s, 6 past 20 s, the slowest 37.3 s) that a search came back empty with
"context deadline exceeded" - twice in ten tries, nothing listed, and
nothing to do but type the same box again. The registry is asked with a
60 s client now, past its measured tail; skills.sh and the icons keep
the short one. The error that remains says what it is (SearchError, the
source named, #1589), the client's own seq guard keeps a slow answer
from overwriting a newer keyword's results, and what the market has of
its own still lists beside the failure.

Red before, on the baseline code with the shared client's timeout stood
in at a scale a test can wait for (the probe: list=0, "context deadline
exceeded (Client.Timeout exceeded while awaiting headers)" - the
reporter's error verbatim); after, the same slow registry answers and
the search lists freecad-mcp. A registry past even its own budget still
fails as a named SearchError (control).

Ported from skygunner's branch fix/mcp-registry-timeout (11d176bd7),
author kept; reported by shupivot.

Run at this head (a7072926d plus this commit): go test ./internal/library
-count=1 ok (181 s), gofmt -l clean, GOOS=linux and GOOS=windows go vet
-tags nogui ./internal/library ok. Broken by hand: fetchRegistry put back
on the shared client, both TestRegistry tests red ("context deadline
exceeded (Client.Timeout exceeded while awaiting headers)"), green again
with the fix. Real registry: three searches for freecad answered 200 in
0.8-1.3 s today, so the reporter's 13-37 s tail wasn't reproduced live;
the fake registry's delay stands in for it. Not tried: the GUI against a
slow registry (client.js's api() sets no timeout of its own).
This commit is contained in:
vincent
2026-10-26 16:32:23 +08:00
committed by yetone
parent a7072926d6
commit 27ca0bef53
2 changed files with 99 additions and 3 deletions
+28 -3
View File
@@ -367,7 +367,7 @@ func fetchRegistry(q string) ([]listed, error) {
} `json:"_meta"`
} `json:"servers"`
}
if err := getJSON(u, &body); err != nil {
if err := getJSONBy(registryClient, u, &body); err != nil {
return nil, &SearchError{Source: "the MCP Registry", Err: err}
}
var list []listed
@@ -1139,6 +1139,17 @@ func fetchImage(u string) ([]byte, string, error) {
var client = &http.Client{Timeout: 20 * time.Second}
// registryTimeout is the MCP Registry's own budget: its search answers in
// 13-37 s often enough (9 of 33 tries past 13 s, 6 past 20 s, the slowest
// 37.3 s - measured in #3043) that the market's 20 s client emptied the
// search with "context deadline exceeded". Its asks go with a longer one;
// skills.sh and the icons keep the short one.
var registryTimeout = 60 * time.Second
// registryClient is the client the registry is asked with, a variable for
// tests.
var registryClient = &http.Client{Timeout: registryTimeout}
// SearchError is a source the market searches that didn't answer; what
// the market has of its own is still listed beside it (#1589).
type SearchError struct {
@@ -1165,6 +1176,11 @@ func metaDescription(page []byte) string {
}
func get(u, accept string, limit int64) ([]byte, error) {
return getBy(client, u, accept, limit)
}
// getBy is get with the client: the registry's own budget (#3043).
func getBy(c *http.Client, u, accept string, limit int64) ([]byte, error) {
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, err
@@ -1172,7 +1188,7 @@ func get(u, accept string, limit int64) ([]byte, error) {
// skills.sh answers a client it doesn't know with a page of its own
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 magpie")
req.Header.Set("Accept", accept)
resp, err := client.Do(req)
resp, err := c.Do(req)
if err != nil {
return nil, err
}
@@ -1181,7 +1197,7 @@ func get(u, accept string, limit int64) ([]byte, error) {
if resp.StatusCode == http.StatusServiceUnavailable {
resp.Body.Close()
time.Sleep(retryPause)
if resp, err = client.Do(req); err != nil {
if resp, err = c.Do(req); err != nil {
return nil, err
}
}
@@ -1207,6 +1223,15 @@ func getJSON(u string, v any) error {
return json.Unmarshal(b, v)
}
// getJSONBy is getJSON with the client (the registry's own budget, #3043).
func getJSONBy(c *http.Client, u string, v any) error {
b, err := getBy(c, u, "application/json", 8<<20)
if err != nil {
return err
}
return json.Unmarshal(b, v)
}
// loadLocked reads the library, waiting for a change being made.
func loadLocked() (*Library, error) {
mu.Lock()
@@ -0,0 +1,71 @@
package library
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// The MCP Registry's search answers in 13–37 s often enough (9 of 33 tries
// over 13 s, 6 over 20 s, the slowest 37.3 s — measured in #3043) that the
// market's 20 s client timed out and the search came back empty, "context
// deadline exceeded", with nothing to retry but the same box. The registry
// is asked with a budget of its own, long enough for its slow tail; the
// other market requests (skills.sh, icons) keep the short one.
// slowRegistry is the registry with a controlled answer delay.
func slowRegistry(t *testing.T, delay time.Duration) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(delay)
w.Write([]byte(`{"servers": [
{"server": {"name": "io.github.blwfish/freecad-mcp", "title": "FreeCAD MCP", "remotes": [{"type": "streamable-http", "url": "https://freecad.example/mcp"}]}}
]}`))
}))
t.Cleanup(srv.Close)
was := registryURL
registryURL = srv.URL
t.Cleanup(func() { registryURL = was })
return srv
}
// TestRegistrySlowTailStillAnswers: a registry that answers past the market
// client's 20 s is searched all the same (#3043). The shared client's
// timeout stands in for the real 20 s here, at a scale a test can wait for.
func TestRegistrySlowTailStillAnswers(t *testing.T) {
sandbox(t)
slowRegistry(t, 120*time.Millisecond)
was := client
client = &http.Client{Timeout: 50 * time.Millisecond} // the 20 s of the report, shrunk
t.Cleanup(func() { client = was })
list, err := MarketServers("freecad")
if err != nil {
t.Fatalf("the registry's slow tail emptied the search: %v", err)
}
if len(list) == 0 || list[0].ID != "io.github.blwfish/freecad-mcp" {
t.Fatalf("found: %+v", list)
}
}
// TestRegistryTimeoutStillSaysSo: past even the registry's own budget the
// search fails with what it is, and what the market has of its own is
// still listed beside it (#1589) — not an empty page.
func TestRegistryTimeoutStillSaysSo(t *testing.T) {
sandbox(t)
slowRegistry(t, 200*time.Millisecond)
was := registryClient
registryClient = &http.Client{Timeout: 30 * time.Millisecond}
t.Cleanup(func() { registryClient = was })
list, err := MarketServers("freecad")
if err == nil {
t.Fatalf("a registry past its own budget answered: %+v", list)
}
var se *SearchError
if !(errors.As(err, &se) && se.Source == "the MCP Registry") {
t.Fatalf("error %v, want a search error naming the registry", err)
}
}