diff --git a/internal/library/market.go b/internal/library/market.go index dbc46e7e4..7e966e6cd 100644 --- a/internal/library/market.go +++ b/internal/library/market.go @@ -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() diff --git a/internal/library/market_registry_timeout_test.go b/internal/library/market_registry_timeout_test.go new file mode 100644 index 000000000..54999daef --- /dev/null +++ b/internal/library/market_registry_timeout_test.go @@ -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) + } +}