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. 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;
  final List<T> toDispose = List<T>.of(_idle);
  _idle.clear();
  // 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();
  for (final T resource in toDispose) {
    await _dispose(resource);
  }
}