How to Fix MongoDB Error 342 (ReshardingCriticalSectionTimeout)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 342 when a resharding critical section exceeds its time limit. To fix it, reduce write traffic during resharding and retry during low load. In the database engine, this failure carries the official name ReshardingCriticalSectionTimeout. When you change a shard key, the cluster moves data in the background. Near the end of this job, the cluster enters a brief critical section. During this phase, the database pauses writes to the target collection. This pause lets all shards

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get mongodb error 342 when a resharding critical section exceeds its time limit. To fix it, reduce write traffic during resharding and retry during low load.

In the database engine, this failure carries the official name ReshardingCriticalSectionTimeout. When you change a shard key, the cluster moves data in the background. Near the end of this job, the cluster enters a brief critical section. During this phase, the database pauses writes to the target collection. This pause lets all shards catch up and swap the new data cleanly. If the catch-up phase takes too long, the engine aborts the job. It does this to protect your apps from a long write outage.

What causes mongodb error 342 in your cluster

High write traffic is the most common cause of mongodb error 342. While the cluster copies data, new writes still arrive at the old shards. The target shards must apply all these recent changes during the final pause. If apps send heavy writes right before the swap, a backlog builds up. The target shards cannot finish reading the change log before the timer expires.

Slow disk speed or high network delay between shards also triggers this timeout. When one shard lags behind, the coordinator waits for it to finish. Once the timer runs out, the coordinator cancels the resharding job right away. It then lifts the write block and returns the collection to its old state. Your existing data stays safe, and client writes can run again.

How to diagnose and fix the timeout safely

Start by checking your cluster metrics from the time of the failure. Look at disk usage, network traffic, and replication lag across all shards. Find out which apps were sending heavy writes to the target collection. You can inspect collection stats and running jobs using safe, read-only permissions. MongoDB grants access through roles that apply down to the collection level. The built-in read role lets an engineer check collection stats and database stats. It does not allow actions that insert, update, or remove data.

Do not hand out broad admin roles to every engineer during an incident. Keep inspection access separate from the roles that change cluster state. Once you find the source of the write spike, plan a retry window. Schedule the next resharding run when customer traffic is at its lowest point. Pause background batch jobs and bulk imports that write to that collection. Make sure all replica set members are healthy and caught up before starting.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

How to prevent future resharding timeouts

Good capacity planning stops resharding jobs from hitting the critical section limit. Check that your storage drives have enough free input and output capacity. Resharding reads and writes large amounts of data while serving normal app traffic. If your disks are already busy, the final catch-up phase will stall.

You should also review how your apps handle brief write pauses. Good retry logic keeps app traffic smooth when the cluster swaps collections. Do not let apps flood the database with rapid retries during the pause. A sudden wave of retries adds load right when the shards need quiet. Control your app write rates, and the critical section will finish on time.

Inspect MongoDB wire traffic in flight

Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.

First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.

Frequently asked questions

What does ReshardingCriticalSectionTimeout mean in MongoDB?

It means the final write-blocking phase of a resharding job took too long. The database stopped the job so your apps would not stay blocked.

Does mongodb error 342 cause data loss?

No, this error does not corrupt or delete your existing data. The cluster rolls back the unfinished resharding job and keeps your original collection.

Can I check collection stats without full write access?

Yes, the built-in read role allows collection and database stats checks. It blocks users from running insert, update, remove, or drop actions.

The query runs. The emails stay hidden.