diff --git a/lib/middleware.js b/lib/middleware.js index 7e1d37e3..dda1826a 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -112,6 +112,14 @@ module.exports = function(srf, logger) { req.locals.callingNumber = sipURIs[1]; } } + + // Feature server INVITE request pipelines taking time to finish, + // while connecting and fetch application from db and invoking webhook. + // call can be canceled without any handling, so we add a listener here + req.once('cancel', (sipMsg) => { + logger.info(`${callId} got CANCEL request`); + req.locals.canceled = true; + }); next(); } diff --git a/lib/session/inbound-call-session.js b/lib/session/inbound-call-session.js index 222a9f40..ee3fd3cd 100644 --- a/lib/session/inbound-call-session.js +++ b/lib/session/inbound-call-session.js @@ -22,6 +22,12 @@ class InboundCallSession extends CallSession { this.req = req; this.res = res; + // if the call was canceled before we got here, handle it + if (this.req.locals.canceled) { + req.locals.logger.info('InboundCallSession: constructor - call was already canceled'); + this._onCancel(); + } + req.once('cancel', this._onCancel.bind(this)); this.on('callStatusChange', this._notifyCallStatusChange.bind(this));