blocking_ioBlocking operations detected on event loop
Synchronous I/O operations are blocking the Node.js event loop, degrading overall application responsiveness. While blocked, no other requests can be processed.
Common Causes
- —Synchronous file operations (fs.readFileSync, fs.writeFileSync)
- —CPU-intensive operations on the main thread
- —Synchronous JSON.parse on large payloads
- —Missing async/await on I/O operations
How to Fix
- 1.Use async/await for all I/O operations
- 2.Use fs.promises instead of synchronous fs methods
- 3.Use worker threads for CPU-intensive tasks
- 4.Profile event loop lag with clinic.js
Blocks the entire process
Node.js runs on a single thread. Any synchronous CPU or I/O work blocks every concurrent request for its entire duration.
CPU-intensive work
typescript
import { Worker } from 'worker_threads';
// BAD — blocks event loop for every request
app.get('/report', (req, res) => {
const result = heavyCsvProcessing(data); // synchronous, blocks all requests
res.json(result);
});
// GOOD — offload to worker thread
app.get('/report', (req, res) => {
const worker = new Worker('./workers/csv-processor.js', { workerData: data });
worker.once('message', (result) => res.json(result));
});