Skip to content
Snippets Groups Projects
Commit d5825d08 authored by Jan David Mol's avatar Jan David Mol
Browse files

Merge branch 'L2SS-560-avoid-asyncio-race-condition' into 'master'

L2SS-560: Avoid race condition in asyncio.all_tasks

Closes L2SS-560

See merge request !212
parents 89bc8d43 35047af2
No related branches found
No related tags found
1 merge request!212L2SS-560: Avoid race condition in asyncio.all_tasks
...@@ -4,6 +4,7 @@ from threading import Semaphore ...@@ -4,6 +4,7 @@ from threading import Semaphore
from threading import Thread from threading import Thread
import asyncio import asyncio
import logging import logging
import sys
from tangostationcontrol.clients.statistics_client_thread import StatisticsClientThread from tangostationcontrol.clients.statistics_client_thread import StatisticsClientThread
...@@ -351,4 +352,25 @@ class TCPReplicator(Thread, StatisticsClientThread): ...@@ -351,4 +352,25 @@ class TCPReplicator(Thread, StatisticsClientThread):
def nof_tasks_pending(self): def nof_tasks_pending(self):
""" Return the number of pending tasks in our event loop. """ """ Return the number of pending tasks in our event loop. """
# asyncio.all_tasks is not thread safe, and can fail on a race condition if another
# thread adds a task to a different loop while we're in all_tasks().
# We thus occasionally need to retry. We try a limited number of times to avoid
# infinite loops.
#
# See https://bugs.python.org/issue36607 and https://support.astron.nl/jira/browse/L2SS-560
#
# This is fixed (in a similar manner as here) in python 3.7.4+, see
# https://github.com/python/cpython/blob/v3.7.3/Lib/asyncio/tasks.py#L34
# versus
# https://github.com/python/cpython/blob/v3.7.4/Lib/asyncio/tasks.py#L34
if sys.version_info >= (3,7,4):
return asyncio.all_tasks(self._loop)
else:
for i in range(100,0,-1):
try:
return len(asyncio.all_tasks(self._loop)) return len(asyncio.all_tasks(self._loop))
except RuntimeError as e:
if i == 1:
# ran out of tries, and we want to expose the original exception
raise
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment