# close method

1. @override

Future<void> close()
override

Closes this resource, relinquishing any underlying resources.

This method is invoked automatically when using try-with-resources patterns or should be called explicitly in a finally block.

The close method is idempotent - calling it multiple times should have no additional effect beyond the first call.

## Implementation Note

Implementations should ensure that resources are properly released even if an exception occurs during the close operation. It's recommended to mark the resource as closed before attempting cleanup operations.

## Example

    @override
    Future<void> close() async {
      if (_closed) return; // Idempotent

      try {
        await _performCleanup();
      } finally {
        _closed = true; // Always mark as closed
      }
    }

Throws IOException if an I/O error occurs during closing.

## Implementation

    @override
    Future<void> close() async {
      return synchronized(_lock, () async {
        if (_isClosed) {
          return;
        }

        _isClosed = true;

        try {
          // Stop lifecycle processors
          await _lifecycleProcessor?.onClose();

          // Publish close event
          await publishEvent(ContextClosedEvent.withClock(this, () => DateTime.now()));

          // Destroy singletons
          destroySingletons();

          // Perform actual cleanup
          await doClose();

          _isActive = false;
          _isRunning = false;
        } catch (e) {
          if(logger.getIsErrorEnabled()) {
            logger.error("Unable to close context due to some issues.", error: e);
          }
        }
      });
    }