apache/beam · error
error disconnecting from MongoDB
Error message
error disconnecting from MongoDB: %w
What it means
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).
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
Example fix
// before
telemetryCtx := ctx
if err := fn.client.Disconnect(telemetryCtx); err != nil {
return fmt.Errorf("error disconnecting from MongoDB: %w", err)
}
// after
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := fn.client.Disconnect(ctx); err != nil {
log.Printf("non-fatal disconnect error: %v", err)
} Defensive patterns
Strategy: try-catch
Validate before calling
// verify reachability before pipeline
if err := client.Ping(ctx, readpref.Primary()); err != nil {
return fmt.Errorf("mongo unreachable before pipeline: %w", err)
} Type guard
func clientUsable(c *mongo.Client) bool { return c != nil && !c.TimedOut(ctx) } Try / catch
if err := fn.client.Disconnect(ctx); err != nil {
log.Printf("disconnect failed (pipeline already done): %v", err)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- batch size must be greater than 0
- bundle size must be greater than 0
- couldn't connect to docker
- err
- error connecting to MongoDB
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/fe1ae14587fa1fa1.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/go/pkg/beam/io/mongodbio/common.go:73
func newClient(ctx context.Context, uri string) (*mongo.Client, error) {
opts := options.Client().ApplyURI(uri)
client, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, fmt.Errorf("error connecting to MongoDB: %w", err)
}
if err := client.Ping(ctx, readpref.Primary()); err != nil {
return nil, fmt.Errorf("error pinging MongoDB: %w", err)
}
return client, nil
}
func (fn *mongoDBFn) Teardown(ctx context.Context) error {
if err := fn.client.Disconnect(ctx); err != nil {
return fmt.Errorf("error disconnecting from MongoDB: %w", err)
}
return nil
}
type documentID struct {
ID any `bson:"_id"`
}
func findID(
ctx context.Context,
collection *mongo.Collection,
filter any,
order int,
skip int64,
) (any, error) {
opts := options.FindOne().
SetProjection(bson.M{"_id": 1}).View on GitHub (pinned to 12126d8942)