Closed samsan-nasa closed 11 months ago
Hello @samsan-nasa : I believe this part of the polling/waiting logic occurs in the getAuthorizationResultFromHttpListener method of 'AcquireTokenByInteractiveFlowSupplier'.
There, it waits until there's a result, the future is canceled, or it reaches an expiration time based on the httpPollingTimeoutInSeconds
value for InteractiveRequestParameters
, which has a default timeout of 120 seconds: https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/d4b5096f9e7926fb491978b45599dc96b46f8f83/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/InteractiveRequestParameters.java#L100
It does seems like there's bad behavior here: that thread does end when you 'cancel' the future, but doesn't seem to when the future completes 'exceptionally' because of a timeout.
However, setting that value to your desired timeout should cause the thread to close correctly for now, and we'll investigate a good way way to improve this behavior or make it more clear.
Thank you. Yes, I copied the wrong code where I saw the 120 value hardcoded. Perhaps you can expose this value as a settable builder value or change the httpPollingTimeoutInSeconds value to match the value specified by the parameter passed to the orTimeout() function for the PublicClientApplication class.
As of the recent 1.14.1 release, the threads should now shut down when the futures time out. The httpPollingTimeoutInSeconds
value will be used to end the loop if you don't set a timeout in the future itself, but if you set a timeout in the future then that will be followed instead.
Library version used
1.14.0
Java version
Java 11
Scenario
PublicClient (AcquireTokenInteractive, AcquireTokenByUsernamePassword)
Is this a new or an existing app?
This is a new app or experiment
Issue description and reproduction steps
This example is using OAuth2 authentication. I am using the Interactive method using the default system browser to acquire a token. When I use a timeout value with the call to acquire the token with the .orTImeout() or .completeOnTimeout() methods, the HttpListener thread stays alive after the client timeout. If you attempt a retry you will get an exception regarding the localhost port being in use because the HttpListener thread stays alive on that port.
Relevant code snippets
Expected behavior
When the client timeout is smaller than 120 seconds (the length of the HttpListener timout) the HttpListener stays alive after the acquireToken() call has completed.
If you add retry logic to the code example, you will get an exception that the localhost port is aleady in use.
Id you add a breakpoint in the catch clause you will see the HttpListener thread (HTTP-Dispatcher thread) stay alive until the 120 second timeout.
Identity provider
Other
Regression
No response
Solution and workarounds
Do not retry authentication for 120 seconds to allow HttpListener to die.