Skip to content

Callback invoked twice on unhandled exception #527

Description

@adamjmcgrath

The callback handler is invoked twice when there is an unhandled exception in the callback, eg:

const { SignedXml } = require("xml-crypto");
const crypto = require("crypto");

const xml = `<x xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' Id='_1'></x>`;
const sig = new SignedXml();
sig.privateKey = crypto.generateKeyPairSync("rsa", {
  modulusLength: 2048,
  privateKeyEncoding: {
    type: "pkcs1",
    format: "pem"
  }
}).privateKey;
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml, (err, result) => {
  console.log("Callback invoked", !!err, !!result);
  throw new Error('Error Thrown');
});

// Results in:
// Callback invoked false true
// Callback invoked true false

// Error: Error Thrown

The callback should only be invoked a single time, eg:

require('fs').readdir(".", (err, files) => {
  console.log("Callback invoked", !!err, !!files);
  throw new Error('Error Thrown');
});

// Results in:
// Callback invoked false true

// Error: Error Thrown

I believe the problem is the synchronous execution of the callback in https://github.com/node-saml/xml-crypto/blob/master/src/types.ts#L259

Activity

  1. srd90 commented on Jan 15, 2026

    @srd90

    I believe the problem is the synchronous execution of the callback in https://github.com/node-saml/xml-crypto/blob/master/src/types.ts#L259

    That link becomes obselete once someone modifies types.ts at master branch so that there shall be new lines of code on top of line 259

    Here is link to that line so that link is pinned to same version of types.ts that was most recent version of master branch when this issue was created:

    possibleCallback(null, result);

  2. added a commit that references this issue on Sep 8, 2026
    b97e5da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions