How to Fix MongoDB Error 46 (LockBusy)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 46 when an operation cannot get a required database lock right away. This official LockBusy failure happens during heavy write traffic, and you fix it by spacing out conflicting requests. Database engines use locks to keep data safe when many users run tasks at the same time. When one task holds a lock on a resource, other tasks must wait or stop. If a task asks for a lock without waiting and finds it busy, the server returns code 46. What causes mongodb error 46 in prod

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 46 when an operation cannot get a required database lock right away. This official LockBusy failure happens during heavy write traffic, and you fix it by spacing out conflicting requests.

Database engines use locks to keep data safe when many users run tasks at the same time. When one task holds a lock on a resource, other tasks must wait or stop. If a task asks for a lock without waiting and finds it busy, the server returns code 46.

What causes mongodb error 46 in production

The official MongoDB error list names code 46 as LockBusy. It occurs when two or more tasks try to change the same database or collection at once. One task grabs the lock first, and the second task fails because the lock is busy.

Admin tasks often trigger this lock clash during busy hours. Actions like creating an index, dropping an index, or dropping a collection need strong locks. When an app runs normal inserts or updates at the same time, lock contention spikes.

Long transactions also hold locks open for too long. If an app starts a change and waits for user input, other requests pile up behind it. Background jobs that update thousands of rows at once cause the same problem.

In a sharded cluster, chunk moves and balance tasks also need locks on collections. If your team runs schema changes while the balancer moves data, one task will hit LockBusy.

How to diagnose and fix LockBusy safely

Start by checking what operations are running right now on the cluster. Look for tasks that have run for several seconds and hold exclusive locks on a collection. Check your server logs to see which command triggered error 46 and which collection it targeted.

To check live tasks safely, give your engineers the lowest role needed for the job. MongoDB grants access through roles that apply to a database down to the collection level. The built-in read role grants actions like find, listCollections, listIndexes, dbStats, and collStats on non-system collections.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Do not give full write roles to engineers who only need to inspect collection stats. The readWrite role includes actions like insert, update, remove, createIndex, dropIndex, and dropCollection. Limiting who can run dropIndex or dropCollection reduces surprise lock clashes during peak traffic.

Once you find the conflicting task, add retry logic in your app code. Wait a short time before sending the failed request again. Break huge batch updates into smaller chunks so no single write holds a lock for long.

How to prevent future lock contention

Schedule heavy schema changes and collection drops for low-traffic windows. When fewer app requests hit the server, admin commands can grab and release locks fast. Make sure automated scripts do not run two index changes on the same collection at once.

Review your database roles across both self-hosted clusters and MongoDB Atlas. Note that Atlas database users have different built-in roles than self-hosted users, though both use the same privilege actions. Separate read users from users who can modify collections or indexes.

Keep transactions short and avoid network calls inside an open transaction block. When every write task finishes in milliseconds, locks stay free and LockBusy errors go away.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.

Frequently asked questions

What does MongoDB error code 46 mean?

Error code 46 is the official LockBusy error in MongoDB. It means a command tried to grab a lock that another active operation already held.

Is it safe to retry an operation after LockBusy?

Yes, LockBusy is a temporary state caused by active traffic on the same resource. Adding a short wait and retry in your app usually fixes the failed request.

Which MongoDB role actions can cause heavy lock contention?

Actions in the readWrite role like createIndex, dropIndex, createCollection, and dropCollection need strong locks. Restricting these actions to planned maintenance runs helps stop sudden lock conflicts.

The query runs. The emails stay hidden.