You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Race condition between worker heartbeat and reconnect #2525
I noticed a race condition between the worker heartbeat and a worker reconnect in case of a severed connection. This race condition leads to an exception raised and logged on the Scheduler. The scheduler and worker are still in a functional state but we receive a lot of misleading error log messages, in particular in combination with issue #2524
From my understanding the following is happening (please correct me if I get something wrong about the asyncio/tornado functionality)
The behavior of the current coroutines:
Worker heartbeat:
The worker heartbeat coroutine has three stages. In between all of these it is possible for another coroutine to gain control of the event loop. The stages are as followed (separated by a yield keyword in the code)
Setup and call to scheduler to signal heartbeat
handle heartbeat response and reconnect if necessary
finalize (nothing interesting)
Handle scheduler / handle stream:
Continuous loop until comm is closed. Every cycle begins with a comm.read() and handles the response by calling a stream handler. After every cycle, control is given back to the event loop and potentially another coroutine is running.
Race condition scenario:
Condition: Connection between scheduler is externally severed.
Scheduler: Connection to worker is broken -> remove worker
Worker handle_scheduler: Catch exception; Add callback to event loop to reconnect
Worker heartbeat: heartbeat stage 1: issue heartbeat to scheduler, response is "missing". Suspend coroutine.
Worker handle_scheduler/reconnect: Reconnect to scheduler, triggered by the caught exception above
Worker heartbeat: handle response of heartbeat with status "missing" since the worker was not registered when issuing the heartbeat -> schedule a reconnect
Worker heartbeat: finalize
Worker heartbeat / reconnect: Worker tries to reconnect but Scheduler raises exception because the worker is already known
Thank you for the detailed writeup @fjetter . This is very useful. Also, my apologies for the long delay in response.
Often in situations like this we set a status that other coroutines can check. For example during this stage:
Worker handle_scheduler: Catch exception; Add callback to event loop to reconnect
We might set something like
self.status = 'connecting'
which this stage might check to determine if it should indeed reconnect
Worker heartbeat: handle response of heartbeat with status "missing" since the worker was not registered when issuing the heartbeat -> schedule a reconnect
In principle I think that something like this would work. We would want to check all of the other uses of status to make sure that they remained accurate, but there are only a few of these.
Is this something that you would be interested in solving? I'm impressed by how you were able to dive into and understand the codebase. I'd love to somehow trick . you into doing more work on the project :) (I'd be more than happy to help review)
Hello, I am interested in fixing this issue. Although I do have some experiences with distributed computing I am a complete Dask noob.
Regarding the solution of the bug:
One way to go would be to synchronize the two threads of execution as suggested via a status variable. Yet I am wondering if it would be also possible to only have either the heartbeat or the handle scheduler loop having the exclusive responsibility to reconnect to the scheduler, therefore eliminating scenarios in which superfluous reconnections appear without synchronization?
Activity
mrocklin commentedon Feb 20, 2019
Thank you for the detailed writeup @fjetter . This is very useful. Also, my apologies for the long delay in response.
Often in situations like this we set a status that other coroutines can check. For example during this stage:
We might set something like
which this stage might check to determine if it should indeed reconnect
In principle I think that something like this would work. We would want to check all of the other uses of
statusto make sure that they remained accurate, but there are only a few of these.Is this something that you would be interested in solving? I'm impressed by how you were able to dive into and understand the codebase. I'd love to somehow trick . you into doing more work on the project :) (I'd be more than happy to help review)
pacman82 commentedon Jul 15, 2019
Hello, I am interested in fixing this issue. Although I do have some experiences with distributed computing I am a complete Dask noob.
Regarding the solution of the bug:
One way to go would be to synchronize the two threads of execution as suggested via a
statusvariable. Yet I am wondering if it would be also possible to only have either the heartbeat or the handle scheduler loop having the exclusive responsibility to reconnect to the scheduler, therefore eliminating scenarios in which superfluous reconnections appear without synchronization?mrocklin commentedon Jul 29, 2019
I wonder if this might be resolved by replacing the reconnect call here with a heartbeat call
distributed/distributed/worker.py
Lines 833 to 835 in cc4fc7d
I mention this because heartbeats already have a mechanism to stop many from overlapping
distributed/distributed/worker.py
Lines 798 to 801 in cc4fc7d
This might resolve the race condition mentioned here.
Call heartbeat rather than reconnect on disconnection
Call heartbeat rather than reconnect on disconnection
mrocklin commentedon Jul 30, 2019
I'm inclined to go with #2906 if that's alright with you all @fjetter @pacman82 @lr4d
Does it resolve your problem?
Call heartbeat rather than reconnect on disconnection (#2906)