close method

Future<void> close()

Closes the pool: disposes every idle resource, fails every waiting borrower with StateError, and blocks new acquisitions. Awaits all idle disposals. If a disposer throws, the remaining idle resources are still disposed and the FIRST disposer error is then rethrown from close. Resources still checked out are NOT disposed by the pool — return them before closing (or dispose them yourself); a late release just drops the reference from the count. Audited: 2026-06-12 11:26 EDT

Implementation

Future<void> close() async {
  _isClosed = true;
  // Fail anyone parked on a slot that will now never free.
  for (final Completer<T> waiter in _waiters) {
    waiter.completeError(StateError('ResourcePool is closed'), StackTrace.current);
  }
  _waiters.clear();
  // Snapshot then empty the idle list next to the disposal loop. Both stay
  // ahead of the first await, and failing the waiters above runs no listener
  // synchronously, so nothing can touch the idle list before this copy.
  final List<T> toDispose = List<T>.of(_idle);
  _idle.clear();
  await _disposeAll(toDispose);
}