close method

Future<void> close()

Closes the server and cancels the timeout.

Idempotent and chain-safe: a call landing while a previous close is still in flight awaits that release, so when the returned future completes the socket is provably closed and the port is free to rebind.

Implementation

Future<void> close() async {
  _timeoutTimer?.cancel();
  _timeoutTimer = null;
  final server = _server;
  _server = null;
  if (server != null) {
    _closing = server.close();
  }
  final closing = _closing;
  if (closing != null) {
    // finally, not after-await: an errored close must not stay pinned in
    // _closing and rethrow out of every later close()/start().
    try {
      await closing;
    } finally {
      _closing = null;
    }
  }
}