locustio/locust · error · LocustError
In order to use a with-block for requests, you must also pas
Error message
In order to use a with-block for requests, you must also pass catch_response=True
What it means
ResponseContextManager.__enter__ raises LocustError if the request was not made with catch_response=True, because only then is the response wrapped in a context manager that allows manual success/failure marking. Using a with-block without that flag would silently do nothing, so the library fails fast.
Source
Thrown at locust/contrib/fasthttp.py:721
_manual_result = None
_entered = False
def __init__(self, response, request_event, request_meta, catch_response: bool):
# copy data from response to this object (use update to avoid sharing
# the dict reference, which creates a GC-reference cycle that Python 3.13+
# can collect mid-request — see #3388)
self.__dict__.update(response.__dict__)
try:
self._cached_content = response._cached_content
except AttributeError:
pass
self._request_event = request_event
self.request_meta = request_meta
self._catch_response = catch_response
def __enter__(self):
if not self._catch_response:
raise LocustError("In order to use a with-block for requests, you must also pass catch_response=True")
self._entered = True
return self
def __exit__(self, exc, value, traceback):
# if the user has already manually marked this response as failure or success
# we ignore the default behaviour of letting the response code determine the outcome
if self._manual_result is not None:
if self._manual_result is True:
self.request_meta["exception"] = None
self._report_request()
elif isinstance(self._manual_result, Exception):
self.request_meta["exception"] = self._manual_result
self._report_request()
return exc is None
if exc:
if isinstance(value, ResponseError):View on GitHub (pinned to f391a716e1)
Solutions
- Add catch_response=True to the request call used in the with-block
- Drop the with-block and use the plain response object if manual result marking is not needed
Example fix
// before
with self.client.get("/api/item") as response:
if response.status_code != 200:
response.failure("bad status")
// after
with self.client.get("/api/item", catch_response=True) as response:
if response.status_code != 200:
response.failure("bad status") Defensive patterns
Strategy: validation
Validate before calling
def with_response(session, method, *args, **kwargs):
kwargs.setdefault("catch_response", True)
return getattr(session, method)(*args, **kwargs) Prevention
- Always pass catch_response=True when using a with-block for requests
- Create a small helper/wrapper that injects catch_response=True automatically
- Review locustfiles for 'with self.client.' lines missing the flag
When it happens
Trigger: Writing 'with self.client.get("/url") as response:' in a FastHttpUser task without catch_response=True in the request kwargs.
Common situations: Copy-pasting with-block response validation examples from Locust docs while omitting the catch_response=True argument, often after refactoring an existing plain request call.
Related errors
- If you want to change the state of the request, you must pas
- Tried to set status on a request that has not yet been made.
- In order to use a with-block for requests, you must also pas
- Tried to set status on a request that has not yet been made.
- If you want to change the state of the request using .succes
AI-assisted analysis of locustio/locust@f391a716e1 (2026-08-29).
Data as JSON: /api/errors/483788f463b90e5e.
Report an issue: GitHub.