{"record":{"id":"60f75fae3f812331","repo":"locustio/locust","slug":"tried-to-stop-user-in-an-unexpected-state-self","errorCode":null,"errorMessage":"Tried to stop User in an unexpected state: {self._state}. This should never happen.","messagePattern":"Tried to stop User in an unexpected state: (.+?)\\. This should never happen\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"locust/user/users.py","lineNumber":221,"sourceCode":"        return self._greenlet\n\n    def stop(self, force: bool = False):\n        \"\"\"\n        Stop the user greenlet.\n\n        :param force: If False (the default) the stopping is done gracefully by setting the state to LOCUST_STATE_STOPPING\n                      which will make the User instance stop once any currently running task is complete and on_stop\n                      methods are called. If force is True the greenlet will be killed immediately.\n        :returns: True if the greenlet was killed immediately, otherwise False\n        \"\"\"\n        if force or self._state == LOCUST_STATE_WAITING:\n            self._group.killone(self._greenlet)\n            return True\n        elif self._state == LOCUST_STATE_RUNNING:\n            self._state = LOCUST_STATE_STOPPING\n            return False\n        else:\n            raise Exception(f\"Tried to stop User in an unexpected state: {self._state}. This should never happen.\")\n\n    @property\n    def group(self):\n        return self._group\n\n    @property\n    def greenlet(self):\n        return self._greenlet\n\n    def context(self) -> dict:\n        \"\"\"\n        Adds the returned value (a dict) to the context for :ref:`request event <request_context>`.\n        Override this in your User class to customize the context.\n        \"\"\"\n        return {}\n\n    @classmethod\n    def json(cls):","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/user/users.py#L203-L239","documentation":"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.","triggerScenarios":"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.","commonSituations":"Programmatically stopping users outside the normal runner lifecycle; custom scripts calling stop() at odd times; bugs in state transitions.","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"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if user._state in (LOCUST_STATE_WAITING, LOCUST_STATE_RUNNING): user.stop()","typeGuard":null,"tryCatchPattern":"try:\n    user.stop()\nexcept Exception as e:\n    logging.warning(\"User in unexpected state: %s\", e)","preventionTips":["Let the runner/environment manage user lifecycle","Avoid manual _state manipulation"],"tags":["python","locust","state-machine","internal"],"backgroundTag":"invalid-user-state","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}