python/cpython · error · RuntimeError

Non-thread-safe operation invoked on an event loop other tha

Error message

Non-thread-safe operation invoked on an event loop other than the current one

What it means

Error "Non-thread-safe operation invoked on an event loop other than the current one" thrown in python/cpython.

Source

Thrown at Lib/asyncio/base_events.py:872

        if handle._source_traceback:
            del handle._source_traceback[-1]
        self._ready.append(handle)
        return handle

    def _check_thread(self):
        """Check that the current thread is the thread running the event loop.

        Non-thread-safe methods of this class make this assumption and will
        likely behave incorrectly when the assumption is violated.

        Should only be called when (self._debug == True).  The caller is
        responsible for checking this condition for performance reasons.
        """
        if self._thread_id is None:
            return
        thread_id = threading.get_ident()
        if thread_id != self._thread_id:
            raise RuntimeError(
                "Non-thread-safe operation invoked on an event loop other "
                "than the current one")

    def call_soon_threadsafe(self, callback, *args, context=None):
        """Like call_soon(), but thread-safe."""
        self._check_closed()
        if self._debug:
            self._check_callback(callback, 'call_soon_threadsafe')
        handle = events._ThreadSafeHandle(callback, args, self, context)
        self._ready.append(handle)
        if handle._source_traceback:
            del handle._source_traceback[-1]
        if handle._source_traceback:
            del handle._source_traceback[-1]
        self._write_to_self()
        return handle

    def run_in_executor(self, executor, func, *args):

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. From another thread, use loop.call_soon_threadsafe() instead of call_soon()/call_later().
  2. Use asyncio.run_coroutine_threadsafe(coro, loop) to submit coroutines from other threads.

When it happens

Trigger: Raised when a non-thread-safe loop operation is invoked from a thread other than the loop's thread.

Common situations: See trigger scenarios.


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/fa70adaac4734126. Report an issue: GitHub.