-
Notifications
You must be signed in to change notification settings - Fork 206
Allow interactive flow to be aborted by CTRL+C even when running on Windows #404
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
Tested in Azure CLI with the latest MSAL
Weird thing is that if I move
out of the |
Reproduced with an easier sample: import threading
import time
from http.server import HTTPServer, SimpleHTTPRequestHandler
class MyClass:
def __init__(self):
self.server = HTTPServer(('127.0.0.1', 8400), SimpleHTTPRequestHandler)
def _get_auth_response(self, result):
while True:
print(">>> Calling handle_request")
self.server.handle_request()
print("<<< handle_request exits")
def get_auth_response(self):
result = {}
t = threading.Thread(target=self._get_auth_response, args=(result,))
t.daemon = True
t.start()
while True:
time.sleep(1)
if not t.is_alive():
break
return result or None
def close(self):
"""Either call this eventually; or use the entire class as context manager"""
self.server.server_close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
try:
with MyClass() as receiver:
print(receiver.get_auth_response())
except KeyboardInterrupt:
print("cancelled")
# Give the daemon thread some time to exit
time.sleep(1) Output:
Notice Explanation: With the shutdown of the main thread, the first invocation of |
Weirdly enough, if I make odd number of Edit: I think this depends on Chrome holds the socket (#405 (comment)). |
Thank you, @jiasli , for your investigation! Inspired by your experiment, I ended up using a simpler yet more accurate improvement. You can test it again using the latest MSAL dev branch. |
This PR fix #393 which only happens on Windows.