{"record":{"id":"6e597bc456d959b3","repo":"cayleygraph/cayley","slug":"error-fetching-size-context-is-nil-graph-not-cor","errorCode":null,"errorMessage":"error fetching size, context is nil, graph not correctly initialised","messagePattern":"error fetching size, context is nil, graph not correctly initialised","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/gaedatastore/quadstore.go","lineNumber":514,"sourceCode":"\t\tif _, ok := err.(*datastore.ErrFieldMismatch); !ok {\n\t\t\treturn quad.Quad{}, err\n\t\t}\n\t}\n\tvar label interface{}\n\tif q.Label != \"\" {\n\t\tlabel = q.Label\n\t}\n\treturn quad.Make(\n\t\tq.Subject,\n\t\tq.Predicate,\n\t\tq.Object,\n\t\tlabel,\n\t), nil\n}\n\nfunc (qs *QuadStore) Stats(ctx context.Context, exact bool) (graph.Stats, error) {\n\tif qs.context == nil {\n\t\treturn graph.Stats{}, errors.New(\"error fetching size, context is nil, graph not correctly initialised\")\n\t}\n\tkey := qs.createKeyForMetadata()\n\tm := new(MetadataEntry)\n\terr := datastore.Get(qs.context, key, m)\n\tif err != nil {\n\t\treturn graph.Stats{}, err\n\t}\n\treturn graph.Stats{\n\t\tNodes: refs.Size{\n\t\t\tValue: m.NodeCount,\n\t\t\tExact: true,\n\t\t},\n\t\tQuads: refs.Size{\n\t\t\tValue: m.QuadCount,\n\t\t\tExact: true,\n\t\t},\n\t}, nil\n}","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/gaedatastore/quadstore.go#L496-L532","documentation":"Stats reads the MetadataEntry from the datastore to report quad counts; the datastore Get needs the App Engine request context. A nil context means the store was not initialized, so sizes cannot be fetched.","triggerScenarios":"Calling Stats (directly or via getSize, QuadIteratorSize, estimateSize, sizeForIterator) on a QuadStore with nil context — store created without the HTTPRequest option or init failed silently.","commonSituations":"Collecting size metrics in cron/background jobs without a request context; dashboard code opening the store at process start; ignoring the creation error from graph.NewQuadStore.","solutions":["Initialize the store per-request with opts[\"HTTPRequest\"] = r before calling Stats.","Check store-creation errors and skip stats collection when initialization failed.","Cache size stats and compute them within a properly contextualized request.","Move stats to a backend that supports background contexts if you need off-request reporting."],"exampleFix":"// before\nst, err := qs.Stats(ctx, false)\n// after\nif qs.context == nil { return graph.Stats{}, errors.New(\"gae store not initialized\") }\nst, err = qs.Stats(ctx, false)","handlingStrategy":"validation","validationCode":"if qs.context == nil {\n    return graph.Stats{}, errors.New(\"gaedatastore not initialized; Stats unavailable\")\n}","typeGuard":"func statsAvailable(qs *QuadStore) bool { return qs.context != nil }","tryCatchPattern":"st, err := qs.Stats(ctx, false)\nif err != nil {\n    if strings.Contains(err.Error(), \"not correctly initialised\") {\n        // skip stats or re-init store with request context\n    }\n    return err\n}","preventionTips":["Collect stats only from request-scoped, initialized stores.","Handle store creation errors before metrics code runs.","Cache stats rather than querying in background jobs without context."],"tags":["appengine","stats","initialization","state"],"backgroundTag":"invalid-state-transition","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}