{"record":{"id":"702858c1a258ce62","repo":"python/cpython","slug":"self-class-name-object-is-already-initia","errorCode":null,"errorMessage":"{self.__class__.__name__} object is already initialized","messagePattern":"(.+?) object is already initialized","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/futures.py","lineNumber":83,"sourceCode":"    #   the difference between\n    #   `await Future()` or `yield from Future()` (correct) vs.\n    #   `yield Future()` (incorrect).\n    _asyncio_future_blocking = False\n\n    # Used by the capture_call_stack() API.\n    __asyncio_awaited_by = None\n\n    __log_traceback = False\n\n    def __init__(self, *, loop=None):\n        \"\"\"Initialize the future.\n\n        The optional event_loop argument allows explicitly setting the event\n        loop object used by the future. If it's not provided, the future uses\n        the default event loop.\n        \"\"\"\n        if self._loop is not None:\n            raise RuntimeError(f\"{self.__class__.__name__} object is already \"\n                                \"initialized\")\n\n        if loop is None:\n            self._loop = events.get_event_loop()\n        else:\n            self._loop = loop\n        self._callbacks = []\n        if self._loop.get_debug():\n            self._source_traceback = format_helpers.extract_stack(\n                sys._getframe(1))\n\n    def __repr__(self):\n        return base_futures._future_repr(self)\n\n    def __del__(self):\n        if not self.__log_traceback:\n            # set_exception() was not called, or result() or exception()\n            # has consumed the exception","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/futures.py#L65-L101","documentation":"asyncio.Future.__init__ raises RuntimeError if the future already has a bound loop, i.e. __init__ is being run twice on the same instance. This happens when a subclass forgets to call super().__init__() correctly or when __init__ is re-invoked on a reused/pooled object.","triggerScenarios":"A Future subclass whose __init__ calls super().__init__() twice; calling Future.__init__(fut) manually on an existing instance; subclass __new__ returning a cached object that was already initialized.","commonSituations":"Custom Future subclasses (e.g. for tracing or instrumentation); code that pools/recycles future objects; copy/deepcopy of a Future partially re-running init; refactors that moved super().__init__() into a conditional branch executed twice.","solutions":["Ensure super().__init__() is called exactly once in the subclass constructor","Do not reuse or manually re-initialize Future instances; create a new one","Avoid deepcopy of futures; recreate them instead"],"exampleFix":"# before\nclass MyFuture(asyncio.Future):\n    def __init__(self, *, loop=None):\n        super().__init__(loop=loop)\n        if loop is not None:\n            super().__init__(loop=loop)  # second call -> RuntimeError\n\n# after\nclass MyFuture(asyncio.Future):\n    def __init__(self, *, loop=None):\n        super().__init__(loop=loop)  # exactly once","handlingStrategy":"validation","validationCode":"class MyFuture(asyncio.Future):\n    def __init__(self, **kw):\n        assert getattr(self, '_loop', None) is None  # not yet initialized\n        super().__init__(**kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call super().__init__() exactly once, unconditionally, first","Never reuse or re-init Future instances","Never deepcopy futures; recreate them"],"tags":["asyncio","future","subclassing"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}