python / python/cpython

`webbrowser.open` hangs indefinitely when exiting browser defined in `BROWSER` environment variable

Open
#125,231 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

To reproduce

On my system, I have set the BROWSER environment variable to suggest a preferred browser for console applications to use.

# my .zshrc
# Use Firefox Dev Edition as my preferred browser if it's on the path
if command -pv firefox-aurora &> /dev/null; then
    export BROWSER='firefox-aurora'
elif command -pv firefox &> /dev/null; then
    export BROWSER='firefox'
fi

As a consequence, calls to webbrowser.open launch Firefox Deverloper Edition (firefox-aurora), which is correct. However, if an instance of firefox-aurora isn't already running, my script will freeze until firefox-aurora exits entirely.

import webbrowser

webbrowser.open('https://example.com')
print('Done')

Additionally, interrupting my script using Ctrl+C causes the browser to crash, likely due to a SIGHUP.

This causes significant issues for my script, as I wish to open multiple sites at once.

Suggested fix

The browser defined by the BROWSER variable should be disowned by the webbrowser , perhaps by making it a UnixBrowser rather than a GenericBrowser. This does risk breaking terminal-based browsers, so I wonder if there's a way to determine if the browser is terminal-based so that we can tell whether to p.wait() or not.

Otherwise, do you believe this may be an issue with Firefox instead, in that they should launch and detach a new process, allowing the process launched through Python to exit immediately?

One more possibility is that the BROWSER variable is intended for terminal-based browsers, similarly to the EDITOR variable for terminal editors and the VISUAL variable for graphical editors.

CPython versions tested on:

3.12

Operating systems tested on:

Linux (specifically, Fedora 40)

Linked PRs
  • gh-149255

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing linked PR gh-149255, then inspect the webbrowser.open entry point and its handling of the BROWSER environment variable. Reproduce the provided script on Linux with Firefox configured, and consider the issue complete when launching the configured browser returns promptly without making Ctrl+C crash it, with appropriate coverage for terminal browsers.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
desktop-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.