Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: can't start new thread #11665

Closed
1 task done
dcraigmv opened this issue Dec 16, 2022 · 15 comments
Closed
1 task done

RuntimeError: can't start new thread #11665

dcraigmv opened this issue Dec 16, 2022 · 15 comments
Labels
resolution: not a bug Determined as not a bug in pip

Comments

@dcraigmv
Copy link

dcraigmv commented Dec 16, 2022

Description

Installing larger packages such as torch cause pip to fail while it renders a progress bar.

Expected behavior

Collecting [package-name]
Downloading [package-name]
|| progress bar ||

Done

pip version

22.0.2

Python version

3.10

OS

Ubuntu Jammy

How to Reproduce

docker run --rm ubuntu:jammy sh -c "apt update && apt install -y python3-pip && pip install torch"

Output

# pip install torch
Collecting torch
  Downloading torch-1.13.1-cp310-cp310-manylinux1_x86_64.whl (887.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/887.5 MB ? eta -:--:--ERROR: Exception:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/base_command.py", line 165, in exc_logging_wrapper
    status = run_func(*args)
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 205, in wrapper
    return func(self, options, args)
  File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py", line 339, in run
    requirement_set = resolver.resolve(
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/resolver.py", line 94, in resolve
    result = self._result = resolver.resolve(
  File "/usr/lib/python3/dist-packages/pip/_vendor/resolvelib/resolvers.py", line 481, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "/usr/lib/python3/dist-packages/pip/_vendor/resolvelib/resolvers.py", line 348, in resolve
    self._add_to_criteria(self.state.criteria, r, parent=None)
  File "/usr/lib/python3/dist-packages/pip/_vendor/resolvelib/resolvers.py", line 172, in _add_to_criteria
    if not criterion.candidates:
  File "/usr/lib/python3/dist-packages/pip/_vendor/resolvelib/structs.py", line 151, in __bool__
    return bool(self._sequence)
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 155, in __bool__
    return any(self)
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 143, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 47, in _iter_built
    candidate = func()
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/factory.py", line 215, in _make_candidate_from_link
    self._link_candidate_cache[link] = LinkCandidate(
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 288, in __init__
    super().__init__(
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 158, in __init__
    self.dist = self._prepare()
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 227, in _prepare
    dist = self._prepare_distribution()
  File "/usr/lib/python3/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 299, in _prepare_distribution
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
  File "/usr/lib/python3/dist-packages/pip/_internal/operations/prepare.py", line 487, in prepare_linked_requirement
    return self._prepare_linked_requirement(req, parallel_builds)
  File "/usr/lib/python3/dist-packages/pip/_internal/operations/prepare.py", line 532, in _prepare_linked_requirement
    local_file = unpack_url(
  File "/usr/lib/python3/dist-packages/pip/_internal/operations/prepare.py", line 214, in unpack_url
    file = get_http_url(
  File "/usr/lib/python3/dist-packages/pip/_internal/operations/prepare.py", line 94, in get_http_url
    from_path, content_type = download(link, temp_dir.path)
  File "/usr/lib/python3/dist-packages/pip/_internal/network/download.py", line 146, in __call__
    for chunk in chunks:
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/progress_bars.py", line 303, in _rich_progress_bar
    with progress:
  File "/usr/lib/python3/dist-packages/pip/_vendor/rich/progress.py", line 652, in __enter__
    self.start()
  File "/usr/lib/python3/dist-packages/pip/_vendor/rich/progress.py", line 643, in start
    self.live.start(refresh=True)
  File "/usr/lib/python3/dist-packages/pip/_vendor/rich/live.py", line 124, in start
    self._refresh_thread.start()
  File "/usr/lib/python3.10/threading.py", line 935, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

Code of Conduct

@dcraigmv dcraigmv added S: needs triage Issues/PRs that need to be triaged type: bug A confirmed bug or unintended behavior labels Dec 16, 2022
@pradyunsg
Copy link
Member

pradyunsg commented Dec 16, 2022

Can you run pip with --debug, post the output in a GitHub Gist and share a link to that here?


/cc @willmcgugan since this is a threading-related exception coming out of rich. I don't know if this has been fixed / wrapped in a nicer exception in a newer version of rich; but it'd be nice for pip if it could catch a specific error raised by rich for this; instead of a generic RuntimeError coming out of threading. Thoughts?

@dcraigmv
Copy link
Author

@pradyunsg
Copy link
Member

That's useful but not what I was looking for. I guess I should have been explicit. Can you run pip install --debug torch and post the output of that in a GitHub Gist?

@dcraigmv
Copy link
Author

I'm sorry, i already had a debug command in my history.

https://gist.github.com/dcraigmv/0ffe10635f154c92094a8b15d79ee6cd

@willmcgugan
Copy link

Can you run pip with --debug, post the output in a GitHub Gist and share a link to that here?

/cc @willmcgugan since this is a threading-related exception coming out of rich. I don't know if this has been fixed / wrapped in a nicer exception in a newer version of rich; but it'd be nice for pip if it could catch a specific error raised by rich for this; instead of a generic RuntimeError coming out of threading. Thoughts?

That's a new one on me. Google tells me it is due to having too many threads open in a process.

It probably would be possible to catch that and disable auto refresh, but it does suggest that there are a lot of threads running in the process. Could there be some kind of thread leak in pip? Or maybe threads not being joined early enough?

@pradyunsg
Copy link
Member

pradyunsg commented Dec 16, 2022

This is the first instance of pip trying to start a thread and failing, IIUC, so it's unlikely to be that. I don't think anything other that rich uses threads within pip, but that might not be 100% accurate.

It's more likely to be that the system is hitting the OS thread limit already.

@dcraigmv
Copy link
Author

Injecting print(threading.active_count()) into rich/live.py#L124 prints out 1

@willmcgugan
Copy link

Ah, so its a system limit.

@dcraigmv You said that this effects larger packages only? Can you see from htop if something is using a lot of threads prior to running pip?

@dcraigmv
Copy link
Author

dcraigmv commented Dec 16, 2022

Only in that i first saw this when installing from a requirements.txt, it installed a couple of packages and then failed at torch.

I know torch is big and 2+2 lead me to think rich has enough time/payload size to auto refresh and start a thread.. then fail.

</speculation>

I will get back onto the server in a few hours to run it again and check process list.

@pradyunsg
Copy link
Member

IIRC, pip only shows the progress bar above a certain size. I'm AFK at the moment, but that'd explain the "only big packages" issue here.

@pradyunsg pradyunsg added project: vendored dependency Related to a vendored dependency state: needs discussion This needs some more discussion and removed S: needs triage Issues/PRs that need to be triaged labels Dec 16, 2022
@pradyunsg
Copy link
Member

https://gist.github.com/dcraigmv/0ffe10635f154c92094a8b15d79ee6cd

Huh, that doesn't look like the debug flag was passed.

@jdecorte-spraoi
Copy link

Any updates on this? I have a similar issue (I think) when trying to use pip to install some packages during a docker build within CircleCI. CircleCI uses cimg/python:3.10 image to create the remote docker environment and then begins the docker build from the base image jupyter/tensorflow-notebook:hub-3.1.0. The next step in the docker file is to install blosc and poetry using pip. It downloads the blosc wheel and then immediately fails:

Step 2/10 : RUN pip install blosc dask-labextension poetry
 ---> Running in b5ad756c00c7
Collecting blosc
  Downloading blosc-1.11.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.5 MB)
ERROR: Exception:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/cli/base_command.py", line 160, in exc_logging_wrapper
    status = run_func(*args)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/cli/req_command.py", line 247, in wrapper
    return func(self, options, args)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/commands/install.py", line 400, in run
    requirement_set = resolver.resolve(
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 92, in resolve
    result = self._result = resolver.resolve(
  File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers.py", line 481, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers.py", line 348, in resolve
    self._add_to_criteria(self.state.criteria, r, parent=None)
  File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers.py", line 172, in _add_to_criteria
    if not criterion.candidates:
  File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/resolvelib/structs.py", line 151, in __bool__
    return bool(self._sequence)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 155, in __bool__
    return any(self)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 143, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 47, in _iter_built
    candidate = func()
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 206, in _make_candidate_from_link
    self._link_candidate_cache[link] = LinkCandidate(
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 297, in __init__
    super().__init__(
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 162, in __init__
    self.dist = self._prepare()
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 231, in _prepare
    dist = self._prepare_distribution()
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 308, in _prepare_distribution
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/operations/prepare.py", line 491, in prepare_linked_requirement
    return self._prepare_linked_requirement(req, parallel_builds)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/operations/prepare.py", line 536, in _prepare_linked_requirement
    local_file = unpack_url(
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/operations/prepare.py", line 166, in unpack_url
    file = get_http_url(
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/operations/prepare.py", line 107, in get_http_url
    from_path, content_type = download(link, temp_dir.path)
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/network/download.py", line 147, in __call__
    for chunk in chunks:
  File "/opt/conda/lib/python3.10/site-packages/pip/_internal/cli/progress_bars.py", line 52, in _rich_progress_bar
    with progress:
  File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/rich/progress.py", line 1165, in __enter__
    self.start()
  File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/rich/progress.py", line 1156, in start
    self.live.start(refresh=True)
  File "/opt/conda/lib/python3.10/site-packages/pip/_vendor/rich/live.py", line 132, in start
    self._refresh_thread.start()
  File "/opt/conda/lib/python3.10/threading.py", line 935, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

@benoitheintz
Copy link

In case this can help: I stumbled upon this issue as I had the same issue with the same stack. And I solved it by upgrading my docker version. I had this version when I had the issue.

~# docker --version
Docker version 20.10.7, build f0df350

and I have now:

~# docker --version
Docker version 23.0.1, build a5ee5b1

@pradyunsg
Copy link
Member

I'm gonna close this out now -- there's nothing actionable for pip here and it's a system-limitation of whatever system you're running on.

Consider looking into why a new thread can't be created on your system, by Python itself.

@pradyunsg pradyunsg added resolution: not a bug Determined as not a bug in pip and removed type: bug A confirmed bug or unintended behavior project: vendored dependency Related to a vendored dependency state: needs discussion This needs some more discussion labels Mar 27, 2023
@namphv

This comment was marked as resolved.

@pypa pypa locked as resolved and limited conversation to collaborators Apr 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolution: not a bug Determined as not a bug in pip
Projects
None yet
Development

No branches or pull requests

6 participants