httpx_auth icon indicating copy to clipboard operation
httpx_auth copied to clipboard

Document workaround to run a text-mode browser in a subprocess

Open blag opened this issue 1 year ago • 2 comments

I figured this out to support an OAuth flow within a Dockerized environment, but it might also be helpful for other authentication backends.

According to the webbrowser module documentation:

For non-Unix platforms, or when a remote browser is available on Unix, the controlling process will not wait for the user to finish with the browser, but allow the remote browser to maintain its own windows on the display. If remote browsers are not available on Unix, the controlling process will launch a new browser and wait.

When you are running a project in a containerized environment, you probably don't have access to a graphical interface. In that case, webbrowser.open() blocks and waits for the text-mode browser to close. However, the opened browser attempts to load the content provided by httpx-auth's authentication response server. This becomes a deadlock - the browser is loading content from the auth response server, but the auth response server cannot send the content because it is blocking on the webbrowser.

The webbrowser module already has support for opening a text-mode browser in a non-blocking fashion:

        if '%s' in browser:
            # User gave us a command line, split it into name and args
            browser = shlex.split(browser)
            if browser[-1] == '&':
                return BackgroundBrowser(browser[:-1])
            else:
                return GenericBrowser(browser)

This PR simply documents how to correctly trigger the preexisting functionality to open a text-mode browser in a separate process.

blag avatar Sep 03 '24 03:09 blag

Since this is just a documentation update, I suspect that the test failures are unrelated, or need to be rerun.

blag avatar Sep 03 '24 03:09 blag

Tests now pass. @Colin-b Can I get a review and merge? TIA! 😄

blag avatar Nov 25 '24 22:11 blag

thx a lot !

Colin-b avatar Nov 26 '24 18:11 Colin-b