tj / node-blocked Goto Github PK
View Code? Open in Web Editor NEWCheck if the event loop is blocked
Check if the event loop is blocked
Node8 introduces Async Hooks, which allow for implementing 'blocked' in a way that makes it aware of current execution context.
Suggested mention:
If You're running Node.js 8+ you can get a stacktrace pointing to the blocking function using very similar [blocked-at](https://github.com/naugtur/blocked-at) package.
I would suggest merging it into blocked, but it's way too early and I believe both packages have their usecases, so instead - let's cross-link them.
I'm making a pull request with a suggestion, but I don't have a strong opinion about where to put the link.
add probes so that you can correlate blocking with a given task
Want to start off by saying thank you for providing this library... while troubleshooting a main thread blocking issue in a Koa app and we systematically removed each element of the app from our server.js file only to find that the main thread will report blocked with the simplest of Node apps.
With either of these code samples in node version 9.8.0:
const Koa = require('koa');
const app = new Koa();
const blocked = require('blocked');
blocked(function(ms) {
console.log(`!!! >>> MAIN THREAD Blocked for ${ms}ms.`);
}, {threshold:10});
app.use(ctx => {
ctx.body = '{"debug":"return from debug"}';
});
app.listen(4000);
or
const http = require('http')
const port = 4000
const blocked = require('blocked');
blocked(function(ms) {
console.log(`!!! >>> MAIN THREAD Blocked for ${ms}ms.`);
}, {threshold:10});
const requestHandler = (request, response) => {
response.end('{"debug":"return from debug"}')
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('error:', err)
}
console.log(`http server listening on port: ${port}`)
})
With output:
http server listening on port: 4000
!!! >>> MAIN THREAD Blocked for 35ms.
!!! >>> MAIN THREAD Blocked for 30ms.
!!! >>> MAIN THREAD Blocked for 45ms.
!!! >>> MAIN THREAD Blocked for 18ms.
!!! >>> MAIN THREAD Blocked for 11ms.
!!! >>> MAIN THREAD Blocked for 11ms.
!!! >>> MAIN THREAD Blocked for 36ms.
...
With either of the code sample above, the main thread will report blocked when the application is idle with no requests inbound to the web server and this points to a node.js internal blocking or node-blocked is misreporting blocking in certain instances. I am new to this level of internals and apologize if this is a known item.
Are there false positives with node-blocked or are we potentially seeing issues within the node core internals?
when event loop is blocked i would like to see which functions were called during that interval and how many times. especially sync operations. so that i can chase after them ...
probably out of scope or too much for such a small package. but this would help a lot!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.