Node.js agent stuck in createBackgroundTransaction

I would like you guys to help me figure out what is happening in my background worker monitoring. My application consists in an express WebApp plus the following background worker initialized in the app.js file:

let newrelic = require('newrelic');
// load worker
pollingWorker.run();

Then the pollingWorker is like this:

    let newrelic = require('newrelic');
  
    async function crawl() {
      console.log('Transaction started');
      let transaction = newrelic.getTransaction();
      try {
        const rule = await allocNext();
        transaction.endTransaction();
        setTimeout( () => {
          pollData();
        }, <int> );
      }
      catch(e) {
        logger.error( e );
        transaction.endTransaction();
        pollData(); // in case of failure, restart
      }
    }

    function pollData() {
      console.log('Prepare background transaction');
      newrelic.createBackgroundTransaction('crawler_x', 'crawl', crawl);
    }

    function run() {
      killSignal = false;
      pollData();
    }

The only log that I see is the ‘Prepare background transaction’ one, the rest just don’t show, so it means it probably get stuck in the call.

Also, I get this error message in newrelic_agent.log:

{
  "v": 0,
  "level": 40,
  "name": "newrelic",
  "hostname": "5ed3d5e470cc",
  "pid": 11824,
  "time": "2017-08-18T21:46:06.449Z",
  "msg": "Failed to instrument module inspector using /app/node_modules/newrelic/lib/instrumentation/core/inspector.js",
  "component": "shimmer",
  "message": "Cannot read property 'Session' of undefined",
  "stack": "TypeError: Cannot read property 'Session' of undefined
      at initialize (/app/node_modules/newrelic/lib/instrumentation/core/inspector.js:8:31)
          at instrument (/app/node_modules/newrelic/lib/shimmer.js:52:22)
              at cb_forEach (/app/node_modules/newrelic/lib/shimmer.js:288:7)
                  at Array.forEach (native)
                      at Object.bootstrapInstrumentation (/app/node_modules/newrelic/lib/shimmer.js:273:39)
                          at initialize (/app/node_modules/newrelic/index.js:85:15)
                              at Object.<anonymous> (/app/node_modules/newrelic/index.js:22:3)
                                  at Module._compile (module.js:571:32)
                                      at Object.Module._extensions..js (module.js:580:10)
                                          at Module.load (module.js:488:32)"
}

Which might be the cause of the error.

@felipeclopes Thanks for posting to the Community and also for the support ticket you opened. In the support ticket, we mentioned the Node agent does not currently support async/await.

I also noticed from your code snippet that you’re using createBackgroundTransaction(). This API call does not start the background transaction. You should use startBackgroundTransaction instead. The transaction handle you’ve returned from getTransaction() has endTransaction instead of end (more info here).

We put together an example code snippet that uses promises (rather that using async/await) with the corrected API calls:

const newrelic = require('newrelic');

pollData()

function crawl() {
let transaction = newrelic.getTransaction();

allocNext()
.then(rule => {
transaction.end()

  setTimeout( () => {
    pollData();
  }, 1000 );
})
.catch(e => {
  pollData()
})
}

function pollData() {
console.log('Prepare background transaction');
newrelic.startBackgroundTransaction('crawler_x', 'crawl', crawl);
}

Regarding the TypeError you see in your log snippet, can you please let us know the exact version of Node you’re using? Also, please let us know the output of running the following from the command line:

$ node
> require('inspector').Session