{"record":{"id":"01ceeae71edd51d6","repo":"petkaantonov/bluebird","slug":"cannot-enable-cancellation-after-promises-are-in-u","errorCode":null,"errorMessage":"cannot enable cancellation after promises are in use","messagePattern":"cannot enable cancellation after promises are in use","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/debuggability.js","lineNumber":347,"sourceCode":"            Promise.longStackTraces();\n        } else if (!opts.longStackTraces && Promise.hasLongStackTraces()) {\n            disableLongStackTraces();\n        }\n    }\n    if (\"warnings\" in opts) {\n        var warningsOption = opts.warnings;\n        config.warnings = !!warningsOption;\n        wForgottenReturn = config.warnings;\n\n        if (util.isObject(warningsOption)) {\n            if (\"wForgottenReturn\" in warningsOption) {\n                wForgottenReturn = !!warningsOption.wForgottenReturn;\n            }\n        }\n    }\n    if (\"cancellation\" in opts && opts.cancellation && !config.cancellation) {\n        if (async.haveItemsQueued()) {\n            throw new Error(\n                \"cannot enable cancellation after promises are in use\");\n        }\n        Promise.prototype._clearCancellationData =\n            cancellationClearCancellationData;\n        Promise.prototype._propagateFrom = cancellationPropagateFrom;\n        Promise.prototype._onCancel = cancellationOnCancel;\n        Promise.prototype._setOnCancel = cancellationSetOnCancel;\n        Promise.prototype._attachCancellationCallback =\n            cancellationAttachCancellationCallback;\n        Promise.prototype._execute = cancellationExecute;\n        propagateFromFunction = cancellationPropagateFrom;\n        config.cancellation = true;\n    }\n    if (\"monitoring\" in opts) {\n        if (opts.monitoring && !config.monitoring) {\n            config.monitoring = true;\n            Promise.prototype._fireEvent = activeFireEvent;\n        } else if (!opts.monitoring && config.monitoring) {","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/petkaantonov/bluebird/blob/c220cfe480868e2f5c5de4fe8bb3b04b97588d7f/src/debuggability.js#L329-L365","documentation":"Promise.config({cancellation: true}) must be enabled before any promise is created, because enabling cancellation swaps in new prototype methods (_onCancel, _propagateFrom, etc.). If async.haveItemsQueued() shows promises already in use, Bluebird throws this Error rather than retroactively changing prototype behavior on live promises.","triggerScenarios":"Calling Promise.config({cancellation: true}) after at least one Bluebird promise has been instantiated — e.g. enabling it inside a request handler, a lazily-loaded module, or after another module already created promises at import time.","commonSituations":"Enabling cancellation mid-request to abort work; assuming config can be changed anytime; a dependency importing bluebird and creating promises before your config runs; copying example code into a controller instead of app bootstrap.","solutions":["Enable cancellation at process startup, immediately after requiring bluebird and before any promise creation","Reject the request cleanly instead of enabling cancellation dynamically; pre-configure at boot","Set NODE_ENV=development or BLUEBIRD_DEBUG where cancellation can be enabled by default early","Audit import order so config runs before modules that create promises","Use AbortController/native facilities for late cancellation needs instead of toggling bluebird config"],"exampleFix":"// before\napp.post('/job', (req, res) => {\n  Promise.config({cancellation: true}); // too late\n  ...\n});\n// after\n// bootstrap.js (first file)\nconst Promise = require('bluebird');\nPromise.config({cancellation: true});\n// then routes can use promise.cancel()","handlingStrategy":"validation","validationCode":"const Promise = require('bluebird');\nPromise.config({ cancellation: true }); // first lines of bootstrap, before any promise creation","typeGuard":"function cancellationEnabledSafely(Promise) {\n  try { Promise.config({ cancellation: true }); return true; }\n  catch (e) { return e.message.indexOf('cancellation') === -1 ? (e => { throw e; })(e) : false; }\n}","tryCatchPattern":"try {\n  Promise.config({ cancellation: true });\n} catch (e) {\n  if (e.message.includes('cancellation after promises are in use')) {\n    console.error('Enable cancellation at startup, before creating promises');\n  } else throw e;\n}","preventionTips":["Enable cancellation in the app entrypoint before any imports that create promises","Do not attempt dynamic cancellation config per request","Use one bootstrap module for all Promise.config calls","Set NODE_ENV=development early so debug defaults apply at load"],"tags":["cancellation","configuration","promise"],"backgroundTag":"cancellation-enabled-too-late","analyzedSha":"c220cfe480868e2f5c5de4fe8bb3b04b97588d7f","analyzedAt":"2026-09-02T02:30:20.863Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}