Skip to content

async_hooks.executionAsyncId is not a function #13

Closed
@jimsimon

Description

I'm getting the following error when trying to use cls-hooked with Express 4 and Sequelize 4 on node 8.4.0:

TypeError: async_hooks.executionAsyncId is not a function
    at AsyncHook.init (/usr/src/app/node_modules/cls-hooked/context.js:291:32)
    at init (async_hooks.js:444:43)
    at emitInitS (async_hooks.js:314:3)
    at setupInit (internal/process/next_tick.js:225:7)
    at process.nextTick (internal/process/next_tick.js:247:5)
    at onwrite (_stream_writable.js:419:15)
    at Socket._writeGeneric (net.js:770:5)
    at Socket._write (net.js:780:8)
    at doWrite (_stream_writable.js:371:12)
    at writeOrBuffer (_stream_writable.js:357:5)

I have an express middleware that looks like this:

const { createNamespace } = require('cls-hooked')
const shardingNamespace = createNamespace('sharding')

// Patch bluebird to ensure cls context isn't lost
const clsBluebird = require('cls-bluebird')

const Promise = require('bluebird')
clsBluebird(shardingNamespace, Promise)

const sequelizePromise = require('sequelize/lib/promise')
clsBluebird(shardingNamespace, sequelizePromise)

module.exports = function (req, res, next) {
  shardingNamespace.bindEmitter(req)
  shardingNamespace.bindEmitter(res)

  shardingNamespace.run(function () {
    shardingNamespace.set('shard', 'default')
    next()
  })
}

One thing to note is that Sequelize creates its own instance of bluebird via const Promise = require('bluebird').getNewLibraryCopy();. I'm wondering if that's causing the issue. Putting a console.log(async_hooks.executionAsyncId) before context.js line 291 shows that most of the time it's a valid function, but occasionally it's undefined.

Any idea what might be causing this?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions