{"record":{"id":"fe1ae14587fa1fa1","repo":"apache/beam","slug":"error-disconnecting-from-mongodb-w","errorCode":null,"errorMessage":"error disconnecting from MongoDB: %w","messagePattern":"error disconnecting from MongoDB: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/common.go","lineNumber":73,"sourceCode":"\nfunc newClient(ctx context.Context, uri string) (*mongo.Client, error) {\n\topts := options.Client().ApplyURI(uri)\n\n\tclient, err := mongo.Connect(ctx, opts)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error connecting to MongoDB: %w\", err)\n\t}\n\n\tif err := client.Ping(ctx, readpref.Primary()); err != nil {\n\t\treturn nil, fmt.Errorf(\"error pinging MongoDB: %w\", err)\n\t}\n\n\treturn client, nil\n}\n\nfunc (fn *mongoDBFn) Teardown(ctx context.Context) error {\n\tif err := fn.client.Disconnect(ctx); err != nil {\n\t\treturn fmt.Errorf(\"error disconnecting from MongoDB: %w\", err)\n\t}\n\n\treturn nil\n}\n\ntype documentID struct {\n\tID any `bson:\"_id\"`\n}\n\nfunc findID(\n\tctx context.Context,\n\tcollection *mongo.Collection,\n\tfilter any,\n\torder int,\n\tskip int64,\n) (any, error) {\n\topts := options.FindOne().\n\t\tSetProjection(bson.M{\"_id\": 1}).","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/common.go#L55-L91","documentation":"This error is returned by mongoDBFn.Teardown when client.Disconnect(ctx) fails while shutting down the MongoDB io Beam source. The library wraps the underlying driver error so a failing disconnect during DoFn teardown is reported to the runner instead of being silently dropped. It signals the MongoDB session could not be cleanly closed (network issue, context deadline, or driver-level failure).","triggerScenarios":"The pipeline element finishes reading and Beam calls Teardown, but fn.client.Disconnect(ctx) returns a non-nil error — e.g. the context passed in has already expired or been canceled, the network to the MongoDB server dropped mid-disconnect, or the client was already closed elsewhere.","commonSituations":"Flaky networks or MongoDB failover during pipeline shutdown; a short Teardown context deadline on slow disconnects; calling beam.ParDo setups where the client was created per-worker and the cluster becomes unreachable before teardown; driver version mismatches after upgrade.","solutions":["Inspect the wrapped %w cause to distinguish network failure from context cancellation","Ensure the Teardown context is not already expired; if the harness uses a short deadline, allow enough time for disconnect","Check network connectivity / MongoDB server availability at pipeline end; add retries only if the error is transient","Verify no other code path calls Disconnect or EndSession on the same client before Teardown","Upgrade the mongo-driver to a version matching your server topology"],"exampleFix":"// before\ntelemetryCtx := ctx\nif err := fn.client.Disconnect(telemetryCtx); err != nil {\n\treturn fmt.Errorf(\"error disconnecting from MongoDB: %w\", err)\n}\n// after\nctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\ndefer cancel()\nif err := fn.client.Disconnect(ctx); err != nil {\n\tlog.Printf(\"non-fatal disconnect error: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"// verify reachability before pipeline\nif err := client.Ping(ctx, readpref.Primary()); err != nil {\n\treturn fmt.Errorf(\"mongo unreachable before pipeline: %w\", err)\n}","typeGuard":"func clientUsable(c *mongo.Client) bool { return c != nil && !c.TimedOut(ctx) }","tryCatchPattern":"if err := fn.client.Disconnect(ctx); err != nil {\n\tlog.Printf(\"disconnect failed (pipeline already done): %v\", err)\n}","preventionTips":["Ping the cluster at Setup time so connection issues surface early","Use generous timeouts on the Teardown context","Avoid sharing/closing the client outside Teardown","Keep driver and server versions compatible"],"tags":["mongodb","go","teardown","connection"],"backgroundTag":"network-request-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}