Skip to content

Instantly share code, notes, and snippets.

@adamantike
Last active November 7, 2021 16:58
Show Gist options
  • Save adamantike/d2af0f0fda5893789d0a1ab71565de48 to your computer and use it in GitHub Desktop.
Save adamantike/d2af0f0fda5893789d0a1ab71565de48 to your computer and use it in GitHub Desktop.
uvicorn/uvicorn#1226

Reproduction steps for uvicorn/uvicorn#1226

Details

This gist contains a Docker Compose configuration, including a simple FastAPI service, and a container with tcping installed, which triggers TCP connections to the service.

To speed up the memory leak reproduction, the tcping container can be scaled up using docker-compose scaling capabilities.

Running

  1. Download the entire content of this gist to a folder.
  2. Run docker-compose up --build --scale tcping=N (e.g. N=10).
  3. On a separate terminal, run docker stats and check memory usage on the API service container.
  4. To stop, just Ctrl+C on the terminal where docker-compose is running.

Testing Uvicorn changes

The Docker Compose configuration allows for Uvicorn changes to be tested locally.

  1. Clone Uvicorn in a subfolder: git clone git@github.com:encode/uvicorn.git
  2. Uncomment the volumes section in the docker-compose.yml file.
  3. Make changes to the cloned Uvicorn repository. For example, apply the changes from encode/uvicorn#1192.
  4. Run the Docker Compose configuration, following the previous steps.
services:
api:
build:
context: .
dockerfile: Dockerfile.api
ports:
- 8080:8080
# volumes:
# - ./uvicorn/uvicorn:/usr/local/lib/python3.9/site-packages/uvicorn/
tcping:
build:
context: .
dockerfile: Dockerfile.tcping
command: ["--port", "8080", "--count", "4294967296", "api"]
depends_on:
- api
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
RUN pip install "uvicorn[standard]"
COPY main.py ./
EXPOSE 8080
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
FROM python:3.9-slim
RUN pip install tcping
ENTRYPOINT ["tcping"]
from fastapi import FastAPI
app = FastAPI()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment