code: func (a *Agent) Leave(ctx context.Context) error {
  	select {
  	case <-a.started:
  	default:
  		return errAgentNotStarted
  	}
  
  	a.leaveOnce.Do(func() {
  		close(a.leaving)
  	})
  
  	// Do not call Wait until we have confirmed that the agent is no longer
  	// accepting assignments. Starting a worker might race with Wait.
  	select {
  	case <-a.left:
  	case <-a.closed:
  		return ErrClosed
  	case <-ctx.Done():
  		return ctx.Err()
  	}
  
  	// agent could be closed while Leave is in progress
  	var err error
  	ch := make(chan struct{})
  	go func() {
  		err = a.worker.Wait(ctx)
  		close(ch)
  	}()
  
  	select {
  	case <-ch:
  		return err
  	case <-a.closed:
  		return ErrClosed
  	}
  }
docs: // Leave instructs the agent to leave the cluster. This method will shutdown
  // assignment processing and remove all assignments from the node.
  // Leave blocks until worker has finished closing all task managers or agent
  // is closed.