Stop tears the supervisor down. Idempotent. Blocks until the underlying session is closed.
(ctx context.Context)
| 400 | // Stop tears the supervisor down. Idempotent. Blocks until the underlying |
| 401 | // session is closed. |
| 402 | func (s *Supervisor) Stop(ctx context.Context) error { |
| 403 | s.mu.Lock() |
| 404 | if s.stopping { |
| 405 | watchDone := s.watchDone |
| 406 | s.mu.Unlock() |
| 407 | return waitForWatcher(ctx, watchDone) |
| 408 | } |
| 409 | s.stopping = true |
| 410 | sess := s.session |
| 411 | s.session = nil |
| 412 | watchDone := s.watchDone |
| 413 | pending := s.inflightConnect |
| 414 | s.inflightConnect = nil |
| 415 | s.mu.Unlock() |
| 416 | |
| 417 | s.tracker.Set(StateStopped) |
| 418 | s.signalDone() |
| 419 | |
| 420 | // Reap a Connect left in flight by a startup timeout so its eventual |
| 421 | // session is closed rather than leaked. Runs in the background: a wedged |
| 422 | // handshake must not block Stop. |
| 423 | if pending != nil { |
| 424 | go s.reapPendingConnect(context.WithoutCancel(ctx), pending) |
| 425 | } |
| 426 | |
| 427 | var closeErr error |
| 428 | if sess != nil { |
| 429 | closeErr = sess.Close(context.WithoutCancel(ctx)) |
| 430 | } |
| 431 | waitErr := waitForWatcher(ctx, watchDone) |
| 432 | if closeErr != nil && ctx.Err() == nil { |
| 433 | return closeErr |
| 434 | } |
| 435 | return waitErr |
| 436 | } |
| 437 | |
| 438 | func waitForWatcher(ctx context.Context, done <-chan struct{}) error { |
| 439 | if done == nil { |
nothing calls this directly
no test coverage detected