-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process hangs until SQLiteBackend instance is closed #173
Comments
This looks like an issue stemming from |
Could you give the changes in |
For those interested, the fix was introduced in a33331f. |
Fixed in |
See NOAA-OWP#103 for background as to why aiohttp was previously pinned. In short, the issues that led to pinning aiohttp have be resolved upstream. aiohttp_client_cache >= 0.9.0 needed b.c. requests-cache/aiohttp-client-cache#173
Related to #147. |
I am reopening this because it seems that the switch to import asyncio
from aiohttp_client_cache import SQLiteBackend, CachedSession
async def main():
cache = SQLiteBackend()
client = CachedSession(cache=cache)
req = await client.get("http://httpbin.org/get")
resp = await req.text()
req.close()
print(resp)
# does not hang if properly close (identical behavior to async context manager)
# await client.close()
if __name__ == "__main__":
asyncio.run(main()) TLDR; the above code hangs because It is required that The unfortunate side-affect of this behavior is that all code that uses a import asyncio
from aiohttp_client_cache import SQLiteBackend, CachedSession
class Client:
def __init__(self):
self._session = CachedSession(cache=SQLiteBackend())
async def get(self) -> str:
async with self._session.get("http://httpbin.org/delay/1") as req:
return await req.text()
async def close(self) -> None:
# failing to call close will result in the program hanging
await self._session.close()
async def __aenter__(self) -> "Client":
return self
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
await self.close()
async def main():
client = Client()
res = await client.get()
print(res)
# this hangs because client.close() is never called
if __name__ == "__main__":
asyncio.run(main()) |
|
If the CachedSession is not properly closed this can happen, however, close is not called magically without using a contextmanager. You have to do:
after the with block finished, the session and the associated cache will be properly closed and no dangling sqlite connection will be running. |
Okay, I understand now. Yeah, getting aiosqlite to close properly on its own without using a contextmanager is a bit tricky. Looks like there are a couple relevant open issues on the aiosqlite repo, but there are some (less than ideal) ways I could hack around this in the mean time. I'll make a separate issue for this (#187). |
@JWCook thanks for continuing to look into this and try and find a viable way to move forward. I am more than happy to collaborate on this issue, meaning submitting a PR, working through proposed solutions, or just testing it. I've been using your package for a long time and would like to continue to do so. You've always been quick to respond and great to communicate with! |
Thanks, I'm glad you've found it useful! I'll try to get at least a short-term fix for this released within the next couple weeks. One thing that would help would be testing out the changes in #189, and let me know if you have any thoughts about that approach. |
@JWCook for sure! I'll take a look at that over the next few days. |
The problem
Process will hang until instance of
SQLiteBackend
is closed. More specifically, the process will hang until theSQLiteBackend
'sresponses: SQLitePickleCache
andredirects: SQLiteCache
instance variables are closed. Note, Ive not tested this on other backends.Expected behavior
Process will exit without explicit close of
SQLiteBackend
.Steps to reproduce the behavior
Workarounds
The only workaround ive found thus far is to explicitly close the
SQLiteBackend
cache instance (e.g.await cache.close()
).Environment
0.8.2
]3.9
]The text was updated successfully, but these errors were encountered: