Skip to content

Python 3.13: KeyError: 'name' on request_meta in ResponseContextManager.__exit__ after GC #3050

@ktosiek

Description

@ktosiek

Prerequisites

Description

After upgrade to Python 3.13 I've started seeing errors when running locust (I've added the traceback at the end). It seems if a GC happens within with self.client.get(..., catch_response=True) then somehow ResponseContextManager.request_meta gets cleared.

Changing ResponseContextManager.__init__ to copy the response.__dict__, instead of just grabbing it, seems to help. I'm not sure how that change affects the rest of the project.

The Traceback on failure:

[2025-02-19 07:59:11,514] SDUPC07/ERROR/locust.user.task: 'name'
Traceback (most recent call last):
  File "/home/wsl/.cache/uv/archive-v0/s81GZnyMdf_txEZnLJlMJ/lib/python3.13/site-packages/locust/user/task.py", line 340, in run
    self.execute_next_task()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/wsl/.cache/uv/archive-v0/s81GZnyMdf_txEZnLJlMJ/lib/python3.13/site-packages/locust/user/task.py", line 373, in execute_next_task
    self.execute_task(self._task_queue.popleft())
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wsl/.cache/uv/archive-v0/s81GZnyMdf_txEZnLJlMJ/lib/python3.13/site-packages/locust/user/task.py", line 490, in execute_task
    task(self.user)
    ~~~~^^^^^^^^^^^
  File "/home/wsl/locust-gc-bug-demo/locustfile.py", line 10, in get_json
    with self.client.get("/json", catch_response=True) as get_json:
         ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wsl/.cache/uv/archive-v0/s81GZnyMdf_txEZnLJlMJ/lib/python3.13/site-packages/locust/clients.py", line 368, in __exit__
    self.url = self.request_meta["name"]
               ~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'name'

Command line

uv run --with=locust==2.32.10 -p 3.13 locust --headless -u 1 -t 1s

Locustfile contents

from locust import run_single_user, HttpUser, task
import gc


class Tests(HttpUser):
    host = "https://httpbin.org"

    @task
    def get_json(self):
        with self.client.get("/json", catch_response=True) as get_json:
            gc.collect()


if __name__ == "__main__":
    run_single_user(Tests)

Python version

3.13.2

Locust version

2.32.10

Operating system

Ubuntu 20.04, but also happens on Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstaleIssue had no activity. Might still be worth fixing, but dont expect someone else to fix it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions