pallets / pallets/click

xdg-open zombie process remains by default in WSL2 after click.launch()

Open
#2,154 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
17.7k
Forks
2.3k
Avg merge
1d 30m
Merged PRs (30d)
18

Description

Description

When calling click.launch(<url>) from within WSL2 (specifically Ubuntu-20.04 in my case), a zombie process is left behind for xdg-open. Setting wait=True avoids this, meaning that the docs for launch:

wait (bool) – Wait for the program to exit before returning. This only works if the launched program blocks. In particular, xdg-open on Linux does not block.

are perhaps not fully accurate when it comes to WSL2.

As an aside, I believe this is an issue that has only arisen recently, as I have not noticed issues before, but it is difficult to know if it has arrived with any specific package upgrade or change to the WSL implementation.

To replicate:

From within a python shell in WSL2:

import click
click.launch("https://www.google.com")

The site will open, in another window, run ps aux | grep [x]dg-open and you will see a zombie process. This will disappear of course when the parent process (the python shell) is closed, but remains until then.

Expected behaviour

There should be no zombie processes, regardless of whether the wait flag is passed or not.

Suggested solution

Add a check for WSL in src/click/_compat.py:

WSL = "microsoft-standard" in package.uname().release

Then use this in src/click/_termui_impl.py to implicitly call Popen with wait() if running from WSL (without assigning it to a var, which I think may also keep the process open!

        if WSL:
            subprocess.Popen(["xdg-open", url]).wait()
        else:
            c = subprocess.Popen(["xdg-open", url])
            if wait:
                return c.wait()

Failing that, if this is undesirable for any reason, then I think at least an amendment to the docs to indicate that wait=True should always be set when running within WSL in order to avoid zombie processes.

Environment:
  • Python version: 3.9.9
  • Click version: Tested on both 7.1.2 (where it was originally discovered), and on 8.0.3. Same on both.
  • Current version of xdg-open : snapd-xdg-open/focal-updates 2.51.1+20.04ubuntu2 amd64

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 reproducing click.launch("https://www.google.com") in WSL2 and inspect the launch handling in src/click/_compat.py and src/click/_termui_impl.py. Verify the behavior with wait omitted and with wait=True, and consider the documented WSL2 behavior; done means launching the URL leaves no xdg-open zombie process.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.