1

I have node10 express app, something like:

const app = express();
app.use(function(req, res, next) { ..}
app.use(  bodyParser.text({
    limit: '1mb',
    type: '*/*',
  })
); 
more of those..
app.post('myApiName', [(req, res, next) => {..}, (req, res, next) => {..} .. etc many more]);
https.createServer(sslOptions, app) ...

But with many more routes and middlewares. Due to blocked issues I have, in which the cpu is working on something and is "not responding" for seconds, I added https://github.com/naugtur/blocked-at/blob/master/README.md module. But most of the stack trace it provides me is starting with at Server.connectionListener (_http_server.js:319:3),.... and in the readme, it says about this: In some cases your code is not directly called and tracking it down will still be difficult. See how the HTTP test case produces a stack pointing to Server.connectionListener as the slow function, because everything inside of it is synchronously called. You can always wrap your handlers' code in setImmediate if you become desperate. Or use resources.. I understand I need to run my handlers from another "origin" that will start from another task in the event loop (I'm probably not phrasing well), but I don't really understand what exactly I can do using the suggestion of setImmediate. I saw there is some npm library https://www.npmjs.com/package/express-async-handler for error handling, but I'm still not sure exactly how my code should look (especially because I have many handlers and middlewares). I'll be happy to get some suggestions or some code snippets to understand how to get a better stack trace.

FMoran
  • 51
  • 1
  • 4
  • "not responding for seconds" can you track that down to a certain api endpoint? – Jonas Wilms Nov 07 '19 at 22:16
  • @JonasWilms thank you very much for answering. the server is working and something is causing "set timeout now" tasks to happen a second later for example (i think this is what blocked module in npm does). and so many API's happen at the same time and each of them is doing DB calls, aws calls, body calculations and more that I don't know which code exactly takes CPU time – FMoran Nov 07 '19 at 22:30
  • Have you tried connecting the Chrome Devtools? https://nodejs.org/de/docs/guides/debugging-getting-started/ – Jonas Wilms Nov 07 '19 at 22:32
  • thank you @JonasWilms. I tried locally and it didn't really reproduce and I got recommendations for this blocked-at package to run in production, which detects block (but doesn't cause it like v8-profiler I also tried) and prints the stack trace, but some of it is not clear to me, like ```at new TickObject (internal/process/next_tick.js:86:9), at process.nextTick (internal/process/next_tick.js:117:16), at endReadable (_stream_readable.js:1083:13), at IncomingMessage.Readable.read (_stream_readable.js:403:7)...``` or the ```at Server.connectionListener``` – FMoran Nov 07 '19 at 22:46

0 Answers0