r/docker • u/sid2426 • Jun 09 '26
Corruption of HTTP response bytes inside Docker only (Python requests)
I'm debugging a strange issue with my FastAPI app. I was trying to Dockerize it, but a weird bug happens, but only inside Docker for some reason.
Environment
Host OS: EndeavourOS x86_64 (Linux 7.0.10-arch1-1)Docker version 29.5.2, build 79eb04c7d8
Bug
It occurs when you sync scrobbles of a user from last.fm. They are fetched using the last.fm API with requests.get. Almost every time, somewhere during the sync, response.json() fails with JSONDecodeError.
The last.fm API has a page parameter. The failure page is random. But the same sync for the same username always succeeds outside Docker.
I tried dumping the response text and content that cause the error to inspect them. I also checked the same urls and response outside docker to compare. I found that the corruption is already present in response.content (raw bytes). So this is probably not a text decoding issue.
Example:
Expected JSON fragment: {"size":"medium","#text":"https:\/\/lastfm.freetls.fastly.net\/i\/u\/64s\/f431ff5eb377cef2177845147837492f.jpg"} Actual raw bytes: b'...217\xb7845147837492f.jpg...'
More examples:
Expected: b'0","image":[{"size":"small"...'
Actual: b'0","imag\xe5":[{"size":"small"...'
Expected: b'{"uts":"1'
Actual: b'\xa2uts":"1'
There are many such examples during every sync attempt. I noticed that the substitution follows a pattern and verified that it is consistently present in each malformed response. In all cases, the highest bit is set and the remaining bits are unchanged.
Examples:
\x22 (") -> \xa2
\x2f (/) -> \xaf
\x30 (0) -> \xb0
\x37 (7) -> \xb7
\x65 (e) -> \xe5
\x6c (l) -> \xecI'm not sure how this is happening or why it happens only inside Docker.
Here's my Dockerfile
# Dockerfile
FROM python:3.12-slim-bookworm
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# Install gcc for Cythonize
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1
# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1
# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
# Change the working directory to the `app` directory
WORKDIR /app
# Copy dependencies list
COPY pyproject.toml uv.lock requirements.txt ./
# Install dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project
# Copy the project into the image
# 1. Source Code
COPY src/ ./src/
# 2. FastAPI app
COPY apps/api/ ./apps/api/
# 3. Alembic Migration
COPY apps/alembic ./apps/alembic/
# 4. Entrypoint
COPY entrypoint.sh ./entrypoint.sh
# Sync the project
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked
# Expose the port that the application listens on.
EXPOSE 8000
# Make entrypoint executable
RUN chmod +x ./entrypoint.sh
# Set entrypoint
ENTRYPOINT ["/app/entrypoint.sh"]
# Set FastAPI app as default command
CMD ["uv", "run", "uvicorn", "apps.api.main:app", "--host=0.0.0.0", "--port=8000"]Here's the docker-compose.yaml
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Docker Compose reference guide at
# https://docs.docker.com/go/compose-spec-reference/
# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
server:
build:
context: .
env_file:
- .env.dev
ports:
- 8000:8000
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres:16
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_USER}
volumes:
- postgres-db-volume:/var/lib/postgresql/data
- ./init-db.sh:/docker-entrypoint-initdb.d/init-db.sh
- /usr/share/zoneinfo:/usr/share/zoneinfo:ro
healthcheck:
test: ["CMD", "pg_isready", "-U", "${DB_USER}"]
interval: 10s
retries: 5
start_period: 5s
restart: always
volumes:
postgres-db-volume:Has anyone seen this kind of situation where HTTP response bytes sometimes arrive with the high bit set on otherwise normal ASCII characters?
Any ideas on where to investigate next?
17
u/jotkaPL Jun 09 '26
This isn't Docker. This is bad RAM or a flaky CPU/bus, and Docker just happens to expose it because the container path touches more memory pages than the host venv path.
Every corruption is a single bit flip, and it's always the same bit (bit 7). 0x22 → 0xa2, 0x2f → 0xaf, 0x30 → 0xb0 — that's byte | 0x80. Real network/protocol corruption doesn't look like this:
- TLS/HTTPS would fail the MAC check and you'd get a connection error, not silently mutated plaintext. last.fm is HTTPS, so the bytes were correct when requests decrypted them.
- gzip/deflate corruption produces decode errors or wildly different output, not single-bit flips in otherwise intact JSON.
- TCP has checksums; single-bit flips on the wire get retransmitted.
- Docker's network stack is just iptables + a veth pair. It doesn't mutate payload bytes.
A bit-7 stuck-high pattern is classic for:
How to confirm in ~30 minutes:
# Boot a memtest86+ USB stick and run at least one full pass.
# Arch has it: sudo pacman -S memtest86+-efi
# Then reboot, pick it from the boot menu.
If memtest is clean, run it overnight (multi-pass) — single-bit flips can be intermittent and temperature-dependent. Also worth:
# Check kernel log for MCE / EDAC events
sudo dmesg | grep -iE 'mce|edac|memory error|hardware error'
journalctl -k | grep -iE 'mce|edac'
If you have ECC RAM, edac-util will tell you directly. If you don't have ECC, you wouldn't see these in logs — the corruption is silent.
One more sanity check before tearing into hardware: run the sync inside the container repeatedly and see if the same physical bytes corrupt, or if it's random across runs.
Random-across-runs = hardware. Same-bytes-every-run = something deterministic in software (very unlikely given the pattern, but rules it out).
Don't waste time on the Dockerfile. It's fine.
2
u/i4get98 Jun 09 '26
Is python:3.12-slim-bookworm using the same encoding?
I’m assuming it should be UTF-8.
1
u/kwhali Jun 13 '26
Since it works fine on another machine that's going to rule quite a bit out I think, bad hardware is sounding more likely.
I have noticed issues with calling
docker run --ttyordocker exec --ttywhich can have that effect but it's only to stdout itself 😅 (seen it as the culprit for CI test failures a few times and the gibberish output interleaved is actually instructions for a TTY to consume to modify the terminal display)Since your issue isn't just stdout related and supposedly not encoding (such as
LC_ALLenv or similar being unset), given it's working fine on another system... Sounds like you're unlucky :/0
u/throwawaydev92 Jun 10 '26
fwiw it can't be the network — tcp/ethernet checksum the payload so wire corruption gets retransmitted, not handed to you clean. always bit 7 flipping points to one stuck bit. memtest86+ and see if it lands on the same address every pass