googleapis / google-auth-library-python-oauthlib

Apache License 2.0
186 stars 82 forks source link

run_local_server does not seem to release its port if an Exception is raised #338

Closed IsaacTay closed 6 months ago

IsaacTay commented 7 months ago

Environment details

Steps to reproduce

  1. Run the code example in an environment without browser access (eg. docker container)

Code example

flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
try:
  creds = flow.run_local_server(port=8080)
except:
  ## Adding sleep here doesn't seem help with release the port either
  creds = flow.run_local_server(port=8080, open_browser=False)

Stack trace

Traceback (most recent call last):
  File "example.py", line 4, in test
    creds = flow.run_local_server(port=8080)
  File "/usr/local/lib/python3.10/site-packages/google_auth_oauthlib/flow.py", line 444, in run_local_server
    webbrowser.get(browser).open(auth_url, new=1, autoraise=True)
  File "/usr/local/lib/python3.10/webbrowser.py", line 65, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "example.py", line 7, in test
    creds = flow.run_local_server(port=8080, open_browser=False)
  File "/usr/local/lib/python3.10/site-packages/google_auth_oauthlib/flow.py", line 432, in run_local_server
    local_server = wsgiref.simple_server.make_server(
  File "/usr/local/lib/python3.10/wsgiref/simple_server.py", line 154, in make_server
    server = server_class((host, port), handler_class)
  File "/usr/local/lib/python3.10/socketserver.py", line 452, in __init__
    self.server_bind()
  File "/usr/local/lib/python3.10/wsgiref/simple_server.py", line 50, in server_bind
    HTTPServer.server_bind(self)
  File "/usr/local/lib/python3.10/http/server.py", line 137, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/local/lib/python3.10/socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

clundin25 commented 7 months ago

Thank you for bringing this to our attention @IsaacTay !

clundin25 commented 7 months ago

@IsaacTay Can you confirm that https://github.com/googleapis/google-auth-library-python-oauthlib/pull/339 fixes your issue?

clundin25 commented 6 months ago

Merged #339