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

  1. Inspect the wrapped %w cause to distinguish network failure from context cancellation
  2. Ensure the Teardown context is not already expired; if the harness uses a short deadline, allow enough time for disconnect
  3. Check network connectivity / MongoDB server availability at pipeline end; add retries only if the error is transient
  4. Verify no other code path calls Disconnect or EndSession on the same client before Teardown
  5. 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

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


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)