Skip to content

Bug: generated token cache .json file is empty #1

@mlsalloum

Description

@mlsalloum

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions