uv: Bytecode compilation hangs non-deterministically hangs in QEMU aarch64 docker

I’m getting “Bytecode timed out (60s)” when running in CI on QEMU emulated arm64. It is expected that this setup is way slower than usual, but I couldn’t find a way to change this timeout, as it seems hard-coded:

https://github.com/astral-sh/uv/blob/7551097a170e02093997b1cdaff1dd86fc30c27a/crates/uv-installer/src/compile.rs#L22

Having this configurable, it could be overridden in environments where byte code compilation might take long (it can be a combination of many modules being installed and a slow system).

I’m currently using uv==0.2.36.

The actual log:

#9 405.5 <jemalloc>: MADV_DONTNEED does not work (memset will be used instead)
#9 405.5 <jemalloc>: (This is the expected behaviour if you are running under QEMU)
#9 406.8 Resolved 2 packages in 442ms
#9 466.2 Prepared 2 packages in 59.41s
#9 466.2 Installed 2 packages in 5ms
#9 526.4 error: Failed to bytecode-compile Python file in: app/venv/lib/python3.12/site-packages
#9 526.4   Caused by: Bytecode timed out (60s)

Full CI log is here: https://github.com/WeblateOrg/docker/actions/runs/10402861578/job/28808187107

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 35 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I think I’ve tracked this down to a fairly straightforward bug in qemu-user and reported it here: https://gitlab.com/qemu-project/qemu/-/issues/2846

Basically there’s an internal structure in qemu-user that tracks open FDs and translates them for the guest process, and they use a lock to protect that structure. But you’re not supposed to mix and match locks and fork, and so if your emulated program has one thread that is in the middle of opening or closing a file descriptor while another thread forks, nothing will ever unlock the lock in the child, and so it will deadlock as soon as it tries to open or close a file descriptor.

I think they just didn’t think about the fact that they run fork (to handle guest fork) when they added the lock. One “solution” would be to revert the patch adding the lock but presumably that will cause a different race condition.

A more practical workaround would be to reduce the concurrency of bytecode compilation with UV_CONCURRENT_INSTALLS=1, new in 0.6.3 (#11615).

For what it’s worth, gdb has tons of problems with getting needlessly confused about Linux namespaces / containers. In this case, since qemu-user-static exists outside of the container, there’s no need for gdb to even bother. You can force it to skip its misguided detection logic by giving it a full path, like gdb -p 12345 /usr/bin/qemu-user-static (from outside the container). No gdbserver is necessary. On Ubuntu, I had to set up ddebs (debuginfod didn’t seem to work for qemu-user-static), apt install qemu-user-static-dbgsym, and also do a sudo apt-get source qemu inside /usr/src and rename the directory appropriately to get source code.

Very nice work @geofft – I also read https://gitlab.com/qemu-project/qemu/-/issues/2846 with pleasure.

I’ve made a minimal reproducer for this: https://github.com/konstin/gh11699

It looks like the specific combination needs is aarch64 through qemu in a docker container.

I tried to debug this with gdb but it couldn’t get it to work. Running gdb in the container errors due to qemu limitations and attaching gdb from the host directly can’t resolve any symbols. Using the recommended gdbserver just hangs for me, using debug.sh from the repo in the container:

$ gdb-multiarch
(gdb) set debug remote 1
(gdb) target remote localhost:7777
Remote debugging using localhost:7777
[remote] start_remote_1: enter
  [remote] Sending packet: $qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;fork-events+;vfork-events+;exec-events+;vContSupported+;QThreadEvents+;QThreadOptions+;no-resumed+;memory-tagging+;xmlRegisters=i386#72
  [remote] Sending packet: $qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;fork-events+;vfork-events+;exec-events+;vContSupported+;QThreadEvents+;QThreadOptions+;no-resumed+;memory-tagging+;xmlRegisters=i386#72
  [remote] Sending packet: $qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;fork-events+;vfork-events+;exec-events+;vContSupported+;QThreadEvents+;QThreadOptions+;no-resumed+;memory-tagging+;xmlRegisters=i386#72
  [remote] Sending packet: $qSupported:multiprocess+;swbreak+;hwbreak+;qRelocInsn+;fork-events+;vfork-events+;exec-events+;vContSupported+;QThreadEvents+;QThreadOptions+;no-resumed+;memory-tagging+;xmlRegisters=i386#72
  [remote] getpkt: Timed out.
  [remote] getpkt: Timed out.
  [remote] getpkt: Timed out.
Ignoring packet error, continuing...
  [remote] packet_ok: Packet qSupported (supported-packets) is supported
warning: unrecognized item "timeout" in "qSupported" response
  [remote] Sending packet: $vCont?#49
  [remote] Sending packet: $vCont?#49
  [remote] Sending packet: $vCont?#49
  [remote] Sending packet: $vCont?#49
  [remote] getpkt: Timed out.
  [remote] getpkt: Timed out.
  [remote] getpkt: Timed out.
Ignoring packet error, continuing...
  [remote] packet_ok: Packet vCont (verbose-resume) is supported
  [remote] Sending packet: $vMustReplyEmpty#3a
  [remote] Sending packet: $vMustReplyEmpty#3a
  [remote] Sending packet: $vMustReplyEmpty#3a
  [remote] Sending packet: $vMustReplyEmpty#3a
  [remote] getpkt: Timed out.
  [remote] getpkt: Timed out.
  [remote] getpkt: Timed out.
Ignoring packet error, continuing...
[remote] start_remote_1: exit
Remote replied unexpectedly to 'vMustReplyEmpty': timeout

I’m only getting this timeout when the gdbserver is running, if it isn’t running it’s a plain could not connect: Connection timed out.. Both container and host are running ubuntu 24.04.

A more practical workaround would be to reduce the concurrency of bytecode compilation with UV_CONCURRENT_INSTALLS=1, new in 0.6.3 (https://github.com/astral-sh/uv/pull/11615).

I can confirm that running my build test case (from #11699) with UV_COMPILE_BYTECODE=1 and UV_CONCURRENT_INSTALLS=1 is a very viable and working workaround. Thanks @geofft !

Temporary solution with timeouts and retries for uv sync commands which helped to fix failing Docker builds:

# syntax=docker/dockerfile:1
FROM ubuntu:noble AS build

# https://docs.docker.com/reference/dockerfile/#automatic-platform-args-in-the-global-scope
ARG TARGETARCH
ARG python_version=3.12

SHELL ["/bin/sh", "-exc"]

...

# https://github.com/astral-sh/uv/pkgs/container/uv
COPY --link --from=ghcr.io/astral-sh/uv:0.6 /uv /usr/local/bin/uv

# https://docs.astral.sh/uv/configuration/environment/
ENV UV_PYTHON="python$python_version" \
  UV_PYTHON_DOWNLOADS=never \
  UV_PROJECT_ENVIRONMENT=/app \
  UV_LINK_MODE=copy \
  PYTHONOPTIMIZE=1

COPY pyproject.toml uv.lock /project/

RUN --mount=type=cache,id=/root/.cache/uv-$TARGETARCH,target=/root/.cache/uv,sharing=locked <<EOF
cd /project
uv sync \
  $([ "$TARGETARCH" = 'arm64' ] && echo '--verbose') \
  --no-dev \
  --no-install-project \
  --locked

timeout 15m sh -ex <<EOT
until timeout 5m uv sync \
  $([ "$TARGETARCH" = 'arm64' ] && echo '--verbose') \
  --no-dev \
  --no-install-project \
  --compile-bytecode
do
  echo "Bytecode compilation timed out"
  echo "Retrying"
done
EOT
EOF

COPY VERSION /project/
COPY src/ /project/src

RUN --mount=type=cache,id=/root/.cache/uv-$TARGETARCH,target=/root/.cache/uv,sharing=locked <<EOF
cd /project
sed -Ei "s/^(version = \")0\.0\.0(\")$/\1$(cat VERSION)\2/" pyproject.toml
uv sync \
  $([ "$TARGETARCH" = 'arm64' ] && echo '--verbose') \
  --no-dev \
  --no-editable

timeout 9m sh -ex <<EOT
until timeout 3m uv sync \
  $([ "$TARGETARCH" = 'arm64' ] && echo '--verbose') \
  --no-dev \
  --no-editable \
  --compile-bytecode
do
  echo "Bytecode compilation timed out"
  echo "Retrying"
done
EOT
EOF

Log with one retry:

#15 0.823 + timeout 15m sh -ex
#15 0.859 + timeout 5m uv sync --verbose --no-dev --no-install-project --compile-bytecode
#15 0.880 <jemalloc>: MADV_DONTNEED does not work (memset will be used instead)
#15 0.880 <jemalloc>: (This is the expected behaviour if you are running under QEMU)
#15 1.115 DEBUG uv 0.6.1
#15 1.120 DEBUG Found project root: `/project`
#15 1.122 DEBUG No workspace root found, using project root
#15 1.125 DEBUG Acquired lock for `/project`
#15 1.127 DEBUG Using Python request `3.12` from explicit request
#15 1.128 DEBUG Checking for Python environment at `/app`
#15 1.136 DEBUG The virtual environment's Python version satisfies `3.12`
#15 1.137 DEBUG Released lock at `/tmp/uv-b95abc02d7f2ad9b.lock`
#15 1.192 DEBUG Using request timeout of 30s
#15 1.210 DEBUG Found static `pyproject.toml` for: my_project @ file:///project
#15 1.211 DEBUG No workspace root found, using project root
#15 1.223 DEBUG Existing `uv.lock` satisfies workspace requirements
#15 1.223 Resolved 69 packages in 38ms
#15 1.228 DEBUG Omitting `my_project` from resolution due to `--no-install-project`
#15 1.239 DEBUG Using request timeout of 30s
#15 1.248 DEBUG Requirement already installed: alembic==1.14.0
...
#15 1.250 DEBUG Requirement already installed: six==1.17.0
#15 1.253 DEBUG Starting 16 bytecode compilation workers
#15 95.27 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.28 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.30 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.32 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.37 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.38 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.46 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.62 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.73 DEBUG Bytecode compilation worker exiting: Ok(())
#15 95.85 DEBUG Bytecode compilation worker exiting: Ok(())
#15 96.17 DEBUG Bytecode compilation worker exiting: Ok(())
#15 96.35 DEBUG Bytecode compilation worker exiting: Ok(())
#15 97.11 DEBUG Bytecode compilation worker exiting: Ok(())
#15 97.44 DEBUG Bytecode compilation worker exiting: Ok(())
#15 98.93 DEBUG Bytecode compilation worker exiting: Ok(())
#15 300.9 + echo Bytecode compilation timed out
#15 300.9 Bytecode compilation timed out
#15 300.9 Retrying
#15 300.9 + echo Retrying
#15 300.9 + timeout 5m uv sync --verbose --no-dev --no-install-project --compile-bytecode
#15 300.9 <jemalloc>: MADV_DONTNEED does not work (memset will be used instead)
#15 300.9 <jemalloc>: (This is the expected behaviour if you are running under QEMU)
#15 301.1 DEBUG uv 0.6.1
#15 301.1 DEBUG Found project root: `/project`
#15 301.1 DEBUG No workspace root found, using project root
#15 301.1 DEBUG Acquired lock for `/project`
#15 301.1 DEBUG Using Python request `3.12` from explicit request
#15 301.1 DEBUG Checking for Python environment at `/app`
#15 301.2 DEBUG The virtual environment's Python version satisfies `3.12`
#15 301.2 DEBUG Released lock at `/tmp/uv-b95abc02d7f2ad9b.lock`
#15 301.2 DEBUG Using request timeout of 30s
#15 301.2 DEBUG Found static `pyproject.toml` for: my_project @ file:///project
#15 301.2 DEBUG No workspace root found, using project root
#15 301.2 DEBUG Existing `uv.lock` satisfies workspace requirements
#15 301.2 Resolved 69 packages in 38ms
#15 301.2 DEBUG Omitting `my_project` from resolution due to `--no-install-project`
#15 301.3 DEBUG Using request timeout of 30s
#15 301.3 DEBUG Requirement already installed: alembic==1.14.0
...
#15 301.3 DEBUG Requirement already installed: six==1.17.0
#15 301.3 DEBUG Starting 16 bytecode compilation workers
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 DEBUG Bytecode compilation worker exiting: Ok(())
#15 304.1 Bytecode compiled 864 files in 2.87s
#15 DONE 304.2s

I will to try to provide more details on local reproduction of the problem later.

I am hitting this now too, when building a nix closure under qemu (aarch64) on an x86_64 machine, that itself is a cloud VM, so there are at least two layers of virtualization in my case.

I can also corroborate that the timeout is not easily reproducible, sometimes “just run it again” seems to work.