-
Notifications
You must be signed in to change notification settings - Fork 0
Description
I've run into an issue whereby, when providing a token_storage_file along with a username and password to cache the token, the storage file is created if it doesn't already exist, but it isn't written to so remains empty. The following runtime warning is also produced:
RuntimeWarning: coroutine 'AsyncFile.write' was never awaited
I believe the issue is arising from the following snippet from CloudTransport._cache_tokens() in transports/cloudtransport.py:
async with await anyio.open_file(
self._token_storage_file, "w", encoding="utf-8"
) as f:
json.dump(self._token, f, ensure_ascii=False, indent=4)
await f.aclose()
I presume what's happening here is that json.dump() is trying to call the .write() method of f, but since it is an async file object the method should be awaited which isn't happening, hence the failed write.
Making the very small change to the following resolved this for me and meant that the token was successfully cached and able to be reused in a subsequent auth attempt:
f = await anyio.open_file(self._token_storage_file, "w", encoding="utf-8")
await f.write(json.dumps(self._token, ensure_ascii=False, indent=4))
await f.aclose()
I'm not sure if this behaviour has already been reproduced/reported, but in case it's helpful I'm using v2025.9.1 of pykasacloud and running on Python 3.12.2.