How to Fix MongoDB Error 312 (OperationIsKilledAndDelisted)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 312 when the database kills a running task and removes it. You fix it by retrying the read or write and stopping long tasks early. The official name for this error is OperationIsKilledAndDelisted. It belongs to the CancellationError and InternalOnly categories in the database engine. Most apps do not see it unless an internal thread drops a slow request. Knowing why tasks get killed helps your team keep the cluster stable. Root causes of mongodb error 312 in the database

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 see mongodb error 312 when the database kills a running task and removes it. You fix it by retrying the read or write and stopping long tasks early.

The official name for this error is OperationIsKilledAndDelisted. It belongs to the CancellationError and InternalOnly categories in the database engine. Most apps do not see it unless an internal thread drops a slow request. Knowing why tasks get killed helps your team keep the cluster stable.

Root causes of mongodb error 312 in the database engine

Database nodes track active work in an internal list. When a query runs too long, the engine can stop it to save memory. Once the engine kills the operation, it removes the entry from the active list. If another internal thread checks that task later, the engine throws mongodb error 312.

This failure sits in the CancellationError and InternalOnly categories. That means the server uses this code to manage its own worker threads. A replica set stepdown can trigger this state during a primary node change. High memory use or a manual kill command from an admin can also cause it.

Network drops between shards can also leave worker threads in a dead state. The shard router cancels the sub-task and clears it from the work queue. When the local node tries to report back, the task record is already gone. The node logs the code and drops the work item.

How to diagnose and fix cancelled operations safely

Start your check by reading the server logs around the time of the error. Look for slow query warnings, replica set elections, or admin kill actions. You want to find out what killed the operation before it was delisted. Do not give engineers full admin access just to inspect live server tasks.

MongoDB grants access through roles that allow specific actions on defined resources. A role applies to a database and works down to the collection level. Give your team the built-in read role when they inspect data during an incident. This role allows 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.

It does not allow insert, update, remove, or dropCollection actions. Avoid handing out the readWrite role unless an engineer must change data or indexes. Keeping privileges small prevents someone from killing the wrong task during a live outage.

Once you find the slow query, add an index or set a query timeout. Tell the client app to retry safe read tasks when a cancellation happens. If a stepdown caused the error, check why the primary node lost contact. Fixing the underlying slow query or network drop stops the error for good.

Preventing task cancellations in shared production clusters

Many cancelled operations start when an engineer runs a heavy ad-hoc query in production. A full collection scan can lock up memory and force an admin kill. You can prevent this by reviewing queries before they run against live collections. Setting time limits on ad-hoc sessions also keeps the work queue clean.

When engineers inspect production collections, protect sensitive user fields in the query output. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs. This lets your team check real query patterns while keeping private values safe.

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 OperationIsKilledAndDelisted mean in MongoDB?

It means the server killed an active task and removed it from the tracking list. It falls under the CancellationError and InternalOnly categories in the database engine.

Should client apps retry after mongodb error 312?

Yes, client apps can retry read queries after this cancellation error. For writes, check if the change finished before you send the request again.

How can teams debug error 312 with safe database roles?

Assign the built-in read role so engineers can run dbStats, collStats, and listIndexes safely. This role blocks data changes like insert, update, remove, and dropCollection during checks.

The query runs. The emails stay hidden.