question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

import trio
import httpx


async def worker(client, nurse):
    r = await client.head('https://google.com')
    nurse.cancel_scope.cancel()


async def main():
    async with httpx.AsyncClient(timeout=None) as client, trio.open_nursery() as nurse:
        for _ in range(3):
            nurse.start_soon(worker, client, nurse)


if __name__ == "__main__":
    trio.run(main)
Traceback (most recent call last):
  File "c:\Users\AmericaN\Desktop\Lab\test.py", line 17, in <module>
    trio.run(main)
  File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\trio\_core\_run.py", line 1946, in 
run
    raise runner.main_task_outcome.error
  File "c:\Users\AmericaN\Desktop\Lab\test.py", line 11, in main
    async with httpx.AsyncClient(timeout=None) as client, trio.open_nursery() as nurse:
  File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_client.py", line 1997, in __aexit__
    await self._transport.__aexit__(exc_type, exc_value, traceback)
  File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpx\_transports\default.py", line 332, in __aexit__
    await self._pool.__aexit__(exc_type, exc_value, traceback)
  File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpcore\_async\connection_pool.py", line 326, in __aexit__
    await self.aclose()
  File "C:\Users\AmericaN\Desktop\Lab\MyEnv\lib\site-packages\httpcore\_async\connection_pool.py", line 312, in aclose
    raise RuntimeError(
RuntimeError: The connection pool was closed while 2 HTTP requests/responses were still in-flight.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tomchristiecommented, Nov 30, 2022

Okay, so my assessment of this has changed…

Actually the simplest way to fix this error is simply to not raise a RuntimeError in this case. The pool becomes closed, the connections become closed, and the example given in the opening post works absolutely fine. Everything is okay.

We only get an error if we later attempt to read from a connection which has been closed.

0reactions
Zac-HDcommented, Nov 25, 2022

Looking into this, the problem is that except BaseException: means that you might be cancelled, so you can’t checkpoint (await or async for or async with) unless in a shielded cancel-scope. (see: anyio docs)

https://github.com/encode/httpcore/blob/d3ca620e8ffd1e18fe2a1aa5369f2df8b4de1662/httpcore/_async/connection_pool.py#L228-L234

Since we presumably don’t want to hang forever if we can’t get the lock (?), I’d wrap this in with anyio.fail_after(delay, shield=True):, with delay set to e.g. 65, or None if hanging is better than raising an error.


This is tricky enough to get right consistently at scale that flake8-trio warns about it (error 102), and if you’d use it I’m open to adding anyio support https://github.com/Zac-HD/flake8-trio/issues/61 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug Definition & Meaning - Merriam-Webster
noun (1) · 1. a · 2. : an unexpected defect, fault, flaw, or imperfection · 3. a · 4. : a concealed...
Read more >
Bug (2006) - IMDb
An unhinged war veteran holes up with a lonely woman in a spooky Oklahoma motel room. The line between reality and delusion is...
Read more >
bug - Wiktionary
(entomology) An insect of the order Hemiptera (the “true bugs”). Any of various species of marine or freshwater crustaceans; e.g. a Moreton Bay...
Read more >
Bug - Wikipedia
A terrestrial arthropod animal (with at least six legs). Insect, a six-legged arthropod · Covert listening device, used in surveillance, espionage and policing ......
Read more >
BUG | definition in the Cambridge English Dictionary
bug noun (INSECT) ... an insect: Some tiny white bugs had eaten the leaves of my house plants. ... A bug is also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found