Skip to content

Race condition between worker heartbeat and reconnect #2525

Closed

Description

@fjetter

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)

  1. Setup and call to scheduler to signal heartbeat
  2. handle heartbeat response and reconnect if necessary
  3. 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

Any suggestions on how to avoid this?

Activity

  1. mrocklin commented on Feb 20, 2019

    @mrocklin
    Member

    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)

  2. pacman82 commented on Jul 15, 2019

    @pacman82

    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?

  3. mrocklin commented on Jul 29, 2019

    @mrocklin
    Member

    I wonder if this might be resolved by replacing the reconnect call here with a heartbeat call

    if self.reconnect:
    logger.info("Connection to scheduler broken. Reconnecting...")
    self.loop.add_callback(self._register_with_scheduler)

    I mention this because heartbeats already have a mechanism to stop many from overlapping

    async def heartbeat(self):
    if not self.heartbeat_active:
    self.heartbeat_active = True
    logger.debug("Heartbeat: %s" % self.address)

    This might resolve the race condition mentioned here.

  4. mrocklin commented on Jul 30, 2019

    @mrocklin
    Member

    I'm inclined to go with #2906 if that's alright with you all @fjetter @pacman82 @lr4d

    Does it resolve your problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions