{"record":{"id":"bf02479a8a4a0152","repo":"googleapis/mcp-toolbox","slug":"failed-to-retrieve-updated-document-w","errorCode":null,"errorMessage":"failed to retrieve updated document: %w","messagePattern":"failed to retrieve updated document: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/firestore/firestore.go","lineNumber":334,"sourceCode":"\t// Get the collection reference\n\tcollection := s.FirestoreClient().Collection(collectionPath)\n\n\t// Add the document to the collection\n\tdocRef, writeResult, err := collection.Add(ctx, documentData)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to add document: %w\", err)\n\t}\n\t// Build the response\n\tresponse := map[string]any{\n\t\t\"documentPath\": docRef.Path,\n\t\t\"createTime\":   writeResult.UpdateTime.Format(\"2006-01-02T15:04:05.999999999Z\"),\n\t}\n\t// Add document data if requested\n\tif returnData {\n\t\t// Fetch the updated document to return the current state\n\t\tsnapshot, err := docRef.Get(ctx)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to retrieve updated document: %w\", err)\n\t\t}\n\t\t// Convert the document data back to simple JSON format\n\t\tsimplifiedData := FirestoreValueToJSON(snapshot.Data())\n\t\tresponse[\"documentData\"] = simplifiedData\n\t}\n\treturn response, nil\n}\n\nfunc (s *Source) UpdateDocument(ctx context.Context, documentPath string, updates []firestore.Update, documentData any, returnData bool) (map[string]any, error) {\n\t// Get the document reference\n\tdocRef := s.FirestoreClient().Doc(documentPath)\n\n\t// Prepare update data\n\tvar writeResult *firestore.WriteResult\n\tvar writeErr error\n\n\tif len(updates) > 0 {\n\t\twriteResult, writeErr = docRef.Update(ctx, updates)","sourceCodeStart":316,"sourceCodeEnd":352,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/firestore/firestore.go#L316-L352","documentation":"Thrown by Source.AddDocuments in the returnData branch: after successfully adding the document, a follow-up docRef.Get(ctx) to read back the freshly written document failed. This is a read failure on an already-created document, distinct from the creation failure in error 792 — the document usually exists but could not be read back.","triggerScenarios":"Calling AddDocuments with returnData=true when the caller's IAM grants create but not read, the document was deleted between Add and Get (TTL policy or concurrent delete), or a transient gRPC error during the read-back.","commonSituations":"Fine-grained IAM where the service account only has write scopes, Firestore TTL policies deleting documents shortly after creation, replication delays in multi-region setups, transient DEADLINE_EXCEEDED on the read-back Get.","solutions":["Verify the service account can also read the collection (roles/datastore.user; check for restrictive IAM conditions).","Check for TTL policies or concurrent writers deleting the collection's documents right after creation.","Retry the operation — the document was created, so a re-read may succeed.","Set a longer context deadline to avoid DEADLINE_EXCEEDED on the read-back.","If 'not found' persists, investigate concurrent deletes on that collection."],"exampleFix":"// before\nsnapshot, err := docRef.Get(ctx)\nif err != nil {\n    return nil, fmt.Errorf(\"failed to retrieve updated document: %w\", err)\n}\n// after (caller-side)\nresponse, err := source.AddDocuments(ctx, \"docs\", data)\nif err != nil {\n    log.Printf(\"document may still exist; add succeeded: %v\", err)\n    return fallbackWithKnownPath(response, err)\n}","handlingStrategy":"retry","validationCode":"// Go\n// Pre-check read access and set an adequate deadline before the read-back\nctx, cancel := context.WithTimeout(ctx, 10*time.Second)\ndefer cancel()\nif _, err := source.GetDocuments(ctx, []string{knownDocPath}); err != nil {\n    return fmt.Errorf(\"no read permission on collection; returnData will fail: %w\", err)\n}","typeGuard":"func isNotFoundErr(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"not found\")\n}","tryCatchPattern":"resp, err := source.AddDocuments(ctx, coll, data)\nif err != nil {\n    // The document was likely created; degrade gracefully instead of failing hard\n    if strings.Contains(err.Error(), \"failed to retrieve updated document\") {\n        log.Printf(\"document created but read-back failed: %v\", err)\n        return nil\n    }\n    return err\n}","preventionTips":["Set returnData=false when read access isn't guaranteed.","Retry transient read-back failures; the write is already durable.","Check for TTL policies that delete documents soon after creation.","Use generous context deadlines for the add-then-get sequence."],"tags":["firestore","read-back","grpc","gcp"],"backgroundTag":"firestore-read-after-write-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}