title: Why is __aexit__ not fully executed when it has await inside?
body: <p>This is the simplified version of my code:</p>
  <p><code>main</code> is a coroutine which stops after the second iteration.<br />
  <code>get_numbers</code> is an async generator which yields numbers but within an async context manager.</p>
  <pre class="lang-py prettyprint-override"><code>import asyncio
  
  
  class MyContextManager:
      async def __aenter__(self):
          print(&quot;Enter to the Context Manager...&quot;)
          return self
  
      async def __aexit__(self, exc_type, exc_value, exc_tb):
          print(exc_type)
          print(&quot;Exit from the Context Manager...&quot;)
          await asyncio.sleep(1)
          print(&quot;This line is not executed&quot;)  # &lt;-------------------
          await asyncio.sleep(1)
  
  
  async def get_numbers():
      async with MyContextManager():
          for i in range(30):
              yield i
  
  
  async def main():
      async for i in get_numbers():
          print(i)
          if i == 1:
              break
  
  
  asyncio.run(main())
  </code></pre>
  <p>And the output is:</p>
  <pre class="lang-none prettyprint-override"><code>Enter to the Context Manager...
  0
  1
  &lt;class 'asyncio.exceptions.CancelledError'&gt;
  Exit from the Context Manager...
  </code></pre>
  <p>I have two questions actually:</p>
  <ol>
  <li>From my understanding, AsyncIO schedules a Task to be <em>called soon</em> in the next cycle of the event loop and gives <code>__aexit__</code> a chance to execute. But the line <code>print(&quot;This line is not executed&quot;)</code> is not executed. Why is that? Is it correct to assume that if we have an <code>await</code> statement inside the <code>__aexit__</code>, the code after that line is not going to execute at all and we shouldn't rely on that for cleaning?</li>
  </ol>
  <br>
  <ol start="2">
  <li>Output of the <code>help()</code> on async generators shows that:</li>
  </ol>
  <pre class="lang-none prettyprint-override"><code> |  aclose(...)
   |      aclose() -&gt; raise GeneratorExit inside generator.
  </code></pre>
  <p>so why I get <code>&lt;class 'asyncio.exceptions.CancelledError'&gt;</code> exception inside the <code>__aexit__</code> ?</p>
  <p><sub>* I'm using Python 3.10.4</sub></p>