Auth0 Home Blog Docs

Hook - Code generated an uncaught exception: Error: Callback was already called.

hooks
callback

#1

I’m using a Client Credentials Exchange hook, and while it’s functioning correctly I’m seeing this error in the logs:

Code generated an uncaught exception: Error: Callback was already called.

Here’s the hook, note there’s only one invocation of cb.

module.exports = function(client, scope, audience, context, cb) {
  var access_token = {};
  access_token.scope = scope;

  if (context && context.body && context.body'foo']) {
    access_token'https://prefix.com/foo'] = context.body'foo'];
  }

  cb(null, access_token);
};

Are there circumstances under which the same hook may be called twice with the same callback function, and I need to guard against that? Additionally we are also using rules - is the same callback passed to each?

Full stacktrace:

Code generated an uncaught exception: Error: Callback was already called.
  at /data/sandbox/node_modules/async/dist/async.js:903:32
  at /data/sandbox/node_modules/async/dist/async.js:3858:13
  at /data/sandbox/lib/sandbox.js:800:36
  at ensure_request_consumed (/data/sandbox/lib/sandbox.js:333:36)
  at /data/sandbox/lib/sandbox.js:794:32
  at /data/sandbox/node_modules/auth0-authz-rules-api/index.js:49:9
  at /data/io/7298df97c3a64268970ccbc79deaac44/webtask.js:178:38
  at organizationPicker (/data/io/7298df97c3a64268970ccbc79deaac44/webtask.js:176:3)
  at fn (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/async/lib/async.js:638:34)
  at Immediate._onImmediate (/data/sandbox/node_modules/auth0-authz-rules-api/node_modules/async/lib/async.js:554:34)
  at processImmediate [as _immediateCallback] (timers.js:396:17)

#2

Based on the stack-trace provided the error is coming from one of the rules and not from your client credentials hook (which is correctly invoking the callback only once). You should review your rules in order to ensure none is calling the callback function more than once; the most common issue is with rules that perform asynchronous operations and for which you do not remove the default callback call performed as the last line of the rule template.


#3

Haha, of course - it’s the same log stream. Cheers mate, I’ll look into the rules then. And thanks for the quick response.


#4

You called it. Sure enough one of our rules does exactly as you say. Cheers again.


#5