How to Fix MongoDB Error 143 (CursorInUse)
You get mongodb error 143 when two requests try to use the same cursor at once. This code maps to CursorInUse, and you fix it by giving each thread its own cursor. A cursor points to a specific batch of query results on the database server. When your app shares one cursor across threads, requests collide. The server rejects the second request to keep your read state safe. What causes mongodb error 143 in your app The database engine tracks every open cursor by its unique ID. Only one operati
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 143 when two requests try to use the same cursor at once. This code maps to CursorInUse, and you fix it by giving each thread its own cursor.
A cursor points to a specific batch of query results on the database server. When your app shares one cursor across threads, requests collide. The server rejects the second request to keep your read state safe.
What causes mongodb error 143 in your app
The database engine tracks every open cursor by its unique ID. Only one operation can advance or close a specific cursor at any given moment. If a second request arrives for that same cursor ID before the first finishes, the engine stops it. It returns code 143 with the CursorInUse error name.
This problem often starts in your app code. Developers sometimes store a single cursor in a global variable or a shared cache. When two web requests hit that shared object at the same time, both send fetch calls for one cursor. Worker pools can also trigger this bug if multiple jobs pull from the same result stream.
Network timeouts can also cause this error. Suppose a client sends a request to read the next batch of results. The network drops the response, or the client times out too early. The client then retries the read right away while the server is still running the first batch request. The server sees two active requests for one cursor and throws the CursorInUse error.
How to diagnose and fix CursorInUse safely
Start by checking your app logs to find which query failed. Look for places where your code passes a cursor between async tasks or threads. Each thread or task should run its own find query and manage its own cursor. If many workers need the same data, read the batch in one thread and pass plain data objects to a queue.
Next, check how your client driver handles timeouts and retries. Set client read timeouts high enough so the app does not retry while the server is still working. If a timeout does happen, let the old cursor expire or close it before starting a fresh query.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
When engineers log in to debug live queries, keep your security rules tight. Do not grant full admin rights just to inspect query behavior. MongoDB grants access through roles that define allowed actions on specific resources. A role applies to the database where you define it and can grant access down to the collection level.
Give your debugging team the built-in read role on the target database. The read role grants read access to all non-system collections through actions like find, listCollections, listIndexes, dbStats, and collStats. It leaves out data modification actions like insert, update, remove, and dropCollection. This lets engineers test queries and check stats without risking production data.
Preventing cursor collisions in production
Good coding habits stop cursor errors before they reach production. Always scope cursors to a single function call or request lifecycle. Close cursors as soon as your loop finishes reading the needed documents. Avoid keeping cursors open across user clicks or long pauses.
If your app exports huge collections, use pagination based on indexed fields instead of one shared cursor. Each worker can query a distinct range of document IDs with its own cursor. This design removes the risk of two threads hitting the same cursor ID at once.
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 CursorInUse mean in MongoDB?
CursorInUse is the official name for error code 143. It means the server received a request for a cursor that is already running another operation.
Can a network retry trigger mongodb error 143?
Yes, a fast retry after a client timeout can cause this error. The second request reaches the server while the first request is still reading from the same cursor.
Which role should engineers use to debug read queries?
Assign the built-in read role on the specific database. It allows find, listIndexes, and collStats actions without permitting insert, update, or remove actions.
Related guides
In this post