This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author asvetlov
Recipients aeros, asvetlov, yselivanov
Date 2020-11-19.06:58:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Perhaps Kyle is right, I had a misunderstanding with `get_running_loop()` vs `_get_running_loop()`.

The last version seems good except for the rare chance of race condition.

The safe code can look like:

global_lock = threading.Lock()  like GIL

       def _get_loop(self):
           loop = asyncio.get_running_loop()
           if self._loop is None:
                # the lock is required because
                # the thread switch can happen
                # between `self._loop is None` check
                # and `self._loop = loop` assignment
                with global_lock:
                    if self._loop is not None:
                        self._loop = loop
           if loop is not self._loop: raise

The alternative is using the fast C atomic `compare_and_swap` function
which is executed under the hold GIL.
We need the pure-Python fallback anyway.

Multithreading is hard...
Date User Action Args
2020-11-19 06:58:16asvetlovsetrecipients: + asvetlov, yselivanov, aeros
2020-11-19 06:58:16asvetlovsetmessageid: <>
2020-11-19 06:58:16asvetlovlinkissue42392 messages
2020-11-19 06:58:16asvetlovcreate