locustio/locust · error · Exception
Tried to stop User in an unexpected state: {self._state}. Th
Error message
Tried to stop User in an unexpected state: {self._state}. This should never happen. What it means
User.stop() only knows how to stop users in WAITING or RUNNING states. If the user's internal _state is anything else (e.g. stopped/starting), the else-branch raises, since this state should be unreachable.
Source
Thrown at locust/user/users.py:221
return self._greenlet
def stop(self, force: bool = False):
"""
Stop the user greenlet.
:param force: If False (the default) the stopping is done gracefully by setting the state to LOCUST_STATE_STOPPING
which will make the User instance stop once any currently running task is complete and on_stop
methods are called. If force is True the greenlet will be killed immediately.
:returns: True if the greenlet was killed immediately, otherwise False
"""
if force or self._state == LOCUST_STATE_WAITING:
self._group.killone(self._greenlet)
return True
elif self._state == LOCUST_STATE_RUNNING:
self._state = LOCUST_STATE_STOPPING
return False
else:
raise Exception(f"Tried to stop User in an unexpected state: {self._state}. This should never happen.")
@property
def group(self):
return self._group
@property
def greenlet(self):
return self._greenlet
def context(self) -> dict:
"""
Adds the returned value (a dict) to the context for :ref:`request event <request_context>`.
Override this in your User class to customize the context.
"""
return {}
@classmethod
def json(cls):View on GitHub (pinned to f391a716e1)
Solutions
- Only call stop() on running/waiting users managed by the runner
- Let the environment/runner stop users instead of calling stop() manually
- Report to Locust if reproducible — message says this should never happen
Defensive patterns
Strategy: try-catch
Validate before calling
if user._state in (LOCUST_STATE_WAITING, LOCUST_STATE_RUNNING): user.stop()
Try / catch
try:
user.stop()
except Exception as e:
logging.warning("User in unexpected state: %s", e) Prevention
- Let the runner/environment manage user lifecycle
- Avoid manual _state manipulation
When it happens
Trigger: Calling user.stop() while the user greenlet is in an unexpected _state (neither LOCUST_STATE_WAITING nor LOCUST_STATE_RUNNING), typically a race or manual state manipulation.
Common situations: Programmatically stopping users outside the normal runner lifecycle; custom scripts calling stop() at odd times; bugs in state transitions.
Related errors
- 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.
- User.run() is a method used internally by Locust, and you mu
- No tag name was supplied
- No tasks defined on {self.__class__.__name__}{extra_message}
AI-assisted analysis of locustio/locust@f391a716e1 (2026-08-29).
Data as JSON: /api/errors/60f75fae3f812331.
Report an issue: GitHub.