The async generator handling code in metric_scope has the following lines
except Exception as ex:
await logger.flush()
if not isinstance(ex, StopIteration):
raise
But async generator never throws StopIteration. It throws AsyncStopIteration. Therefore, the exception is always raised when generator completes.
Furthermore, instead of using while True and relying on Exception handling for every iteration, we can just use async for ...: yield result syntax. Same goes for regular generator.