Skip to content

Make MSAL AuthenticationService.init safe to call concurrently - #69735

Closed
droyad wants to merge 1 commit into
dotnet:mainfrom
droyad:fix/msal-concurrent-init
Closed

droyad wants to merge 1 commit into
dotnet:mainfrom
droyad:fix/msal-concurrent-init

Conversation

@droyad

@droyad droyad commented Oct 9, 2026 •

Copy link
Copy Markdown

Make MSAL AuthenticationService.init safe to call concurrently

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Cache the MSAL interop's init promise so concurrent calls don't race

Description

#66055 (backported to 10.0 in #66094) added await AuthenticationService.instance.initialize() to the MSAL AuthenticationService.init, but _initialized is still only set after that await. Before that change the method body ran synchronously, so it couldn't be re-entered. Now, any init call that arrives while msal.js is initialising passes the check and creates a second MsalAuthorizeService.

On a login callback, both instances run handleRedirectPromise() and handleResult():

  • The first one consumes the saved state from sessionStorage, including the return URL.
  • The second becomes AuthenticationService.instance and returns success with no state.
  • RemoteAuthenticatorViewCore then falls back to Navigation.BaseUri, so the user lands on the app root instead of the page they asked for.

On the .NET side, RemoteAuthenticationService.EnsureAuthService() doesn't serialise its calls into init, so two concurrent startup callers are enough to trigger this. A common pair is the authentication state lookup and an access token request from BaseAddressAuthorizationMessageHandler in a layout component.

This change caches the initialisation promise, matching what the OIDC AuthenticationService in WebAssembly.Authentication already does ("Multiple initializations can start concurrently and we want to avoid that"). Concurrent callers now await the same initialisation.

A companion PR, #69736, makes EnsureAuthService() share a single init call on the .NET side as well. Either change fixes the bug; together, neither layer depends on the other to dedupe.

Testing

There's no JS test project for this interop, so I checked it with a throwaway harness:

  • Bundled AuthenticationService.ts with esbuild against a stub @azure/msal-browser whose initialize() resolves after 20ms.
  • Called init twice concurrently, then once more.
MSAL instances handleRedirectPromise calls
main 2 2
this PR 1 1

The change type-checks under the repo's Shared.JS tsconfig (strict) with @azure/msal-browser@4.30.0. I also reproduced the original bug end to end in a real app on 10.0.12 by instrumenting sessionStorage across the redirect; the trace is in this comment on #68136.

Fixes #68136

🤖 Generated with Claude Code

init awaits msal.js initialize() before setting _initialized, so a second call
arriving during that await creates another MsalAuthorizeService. Both instances
process the redirect response: the first consumes the saved state (including
the return URL), and the second, which becomes AuthenticationService.instance,
completes sign-in without state, so the user is sent to the base URI.

Cache the initialization promise instead, as the OIDC AuthenticationService
already does, so concurrent callers await the same initialization.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 03:26
@droyad
droyad requested a review from a team as a code owner October 9, 2026 03:26
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Oct 9, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 9, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @droyad. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The browser navigation regression lacks the required permanent Selenium coverage.

1 open finding
What changed in this PR

Caches MSAL initialization so concurrent callers share one promise and avoid duplicate redirect handling.

Changes:

  • Replaces the initialization flag with a cached promise.
  • Extracts initialization into initializeCore.
File Description
AuthenticationService.ts Deduplicates concurrent MSAL initialization.

🧠 Review effort: Balanced

await AuthenticationService.instance.initialize();
AuthenticationService.instance.initializeMsalHandler();
AuthenticationService._initialized = true;
AuthenticationService._initialized = AuthenticationService.initializeCore(settings, jsLoggingOptions);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no existing MSAL testing

@Youssef1313

Copy link
Copy Markdown
Member

Thanks for the contribution @droyad.

We already have a PR to fix that in #69726 which also adds tests. Hence, I'm going to close this one. Feel free to review the other PR and add your input there.

Thanks again for the contribution!

@Youssef1313 Youssef1313 closed this Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RemoteAuthenticatorView ignores ReturnUrl after successful login in Microsoft.Authentication.WebAssembly.Msal 10.0.10

3 participants