Closed jiasli closed 2 years ago
Azure CLI doesn't have this issue because Azure CLI doesn't close the server upon receiving Ctrl+C at all!
My initial implementation has a flaw: if some noises reach first, self.server.auth_response
will be set, and self._server.handle_request()
will be called again, triggering the failure. This can be reproed with curl
:
python -m azure.cli login --debug
curl localhost:xxxxx
However, when I visit localhost:xxxxx
in Chrome, after pressing Ctrl+C, self._server.handle_request()
never exits and the failure is not triggered. Meanwhile, localhost:xxxxx
doesn't respond to curl
anymore. Guess Chrome is holding the socket for some reason, preventing self._server.handle_request()
from exiting. Once Chrome is closed, everything resumes to normal.
Closing this, since we currently choose a boolean flag approach.
Refine https://github.com/AzureAD/microsoft-authentication-library-for-python/pull/404
Symptom
_AuthCodeReceiver
is used as a context manager:https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/f97cb7c69d1f5c2eed6c85e28dd828b2434263b4/msal/oauth2cli/oauth2.py#L636
When Ctrl+C is pressed, Python calls
https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/f97cb7c69d1f5c2eed6c85e28dd828b2434263b4/msal/oauth2cli/authcode.py#L279-L280
then
https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/f97cb7c69d1f5c2eed6c85e28dd828b2434263b4/msal/oauth2cli/authcode.py#L272-L274
This shuts down the server and causes
https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/f97cb7c69d1f5c2eed6c85e28dd828b2434263b4/msal/oauth2cli/authcode.py#L264
to return.
Because of the outer loop
https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/f97cb7c69d1f5c2eed6c85e28dd828b2434263b4/msal/oauth2cli/authcode.py#L261 ,
https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/f97cb7c69d1f5c2eed6c85e28dd828b2434263b4/msal/oauth2cli/authcode.py#L264
is called again. However, as the server has been shut down, it fails with
Change
If a request is handled by
msal.oauth2cli.authcode._AuthCodeHandler.do_GET
,self.server.auth_response
will always be set. Later on, ifself._server.handle_request()
returns leaving an emptyself.server.auth_response
, that means the server is shutdown before any request is handled, so we break the loop.