diff --git a/sdk/identity/identity/CHANGELOG.md b/sdk/identity/identity/CHANGELOG.md index f5f202a539d9..d2aa4d192b83 100644 --- a/sdk/identity/identity/CHANGELOG.md +++ b/sdk/identity/identity/CHANGELOG.md @@ -2,6 +2,9 @@ ## 2.0.0-beta.4 (Unreleased) +## Bug fixes + +- Fixed an issue in which `InteractiveBrowserCredential` on Node would sometimes cause the process to hang if there was no browser available. ### Breaking changes - Removed the protected method `getAzureCliAccessToken` from the public API of the `AzureCliCredential`. While it will continue to be available as part of v1, we won't be supporting this method as part of v2's public API. diff --git a/sdk/identity/identity/src/msal/nodeFlows/msalOpenBrowser.ts b/sdk/identity/identity/src/msal/nodeFlows/msalOpenBrowser.ts index 2b2ab23dbde8..674bcbec57d9 100644 --- a/sdk/identity/identity/src/msal/nodeFlows/msalOpenBrowser.ts +++ b/sdk/identity/identity/src/msal/nodeFlows/msalOpenBrowser.ts @@ -138,18 +138,13 @@ export class MsalOpenBrowser extends MsalNode { cleanup(); }); }; + const app = http.createServer(requestListener); + const server = stoppable(app); const listen = app.listen(this.port, this.hostname, () => this.logger.info(`InteractiveBrowserCredential listening on port ${this.port}!`) ); - app.on("connection", (socket) => socketToDestroy.push(socket)); - const server = stoppable(app); - - this.openAuthCodeUrl(scopes).catch((e) => { - cleanup(); - reject(e); - }); function cleanup(): void { if (listen) { @@ -166,13 +161,24 @@ export class MsalOpenBrowser extends MsalNode { } } - const abortSignal = options?.abortSignal; - if (abortSignal) { - abortSignal.addEventListener("abort", () => { + app.on("connection", (socket) => socketToDestroy.push(socket)); + + app.on("listening", () => { + const openPromise = this.openAuthCodeUrl(scopes); + + const abortSignal = options?.abortSignal; + if (abortSignal) { + abortSignal.addEventListener("abort", () => { + cleanup(); + reject(new Error("Aborted")); + }); + } + + openPromise.then().catch((e) => { cleanup(); - reject(new Error("Aborted")); + reject(e); }); - } + }); }); }