auth0 / auth0-python

Auth0 SDK for Python
https://auth0-python.readthedocs.io
MIT License
506 stars 164 forks source link

Getting an error coroutine 'AsyncRestClient._request_with_session' was never awaited #555

Closed chipxsd closed 9 months ago

chipxsd commented 9 months ago

Checklist

Description

I've been noticing some errors in our logs ever since we bumped the Auth0-python lib version to 4.6.0:

2023-11-21 23:42:35   File "/osiris/pkgs/osiris_api_service/providers/auth0/identity_provider_client.py", line 82, in _get_auth0_access_token
2023-11-21 23:42:35     return response["access_token"]
2023-11-21 23:42:35            ~~~~~~~~^^^^^^^^^^^^^^^^
2023-11-21 23:42:35 INFO:     169.254.1.1:28012 - "GET /organizations/5e220319-013b-****-****-2dfeb8ad7cf0/invitations HTTP/1.1" 200 OK
2023-11-21 23:42:36 TypeError: 'coroutine' object is not subscriptable
2023-11-21 23:44:42 sys:1: RuntimeWarning: coroutine 'AsyncRestClient._request_with_session' was never awaited
2023-11-21 23:44:42 RuntimeWarning: Enable tracemalloc to get the object allocation traceback

I don't have it captured in a test to reproduce it, but I'll share our authentication code.

Reproduction

AsyncGetToken = asyncify(authentication.GetToken)

...

    @property
    async def client(self) -> AsyncAuth0:
        """
        This property method returns an instance of AsyncAuth0. If the instance
        does not exist, it creates a new one using the domain and access
        token obtained from the _get_auth0_access_token method.
        """
        if self._client is None:
            auth0_token = await self._get_auth0_access_token()
            self._client = AsyncAuth0(self.domain, auth0_token)

        return self._client

    async def _get_auth0_access_token(self) -> str:
        """
        Performs the Auth0 authentication and returns the access token.
        """
        get_token = AsyncGetToken(self.domain, self.client_id, self.client_secret)
        response = await get_token.client_credentials_async(f"https://{self.domain}/api/v2/")

        return response["access_token"] # <--- fails here with TypeError: 'coroutine' object is not subscriptable

Additional context

No response

auth0-python version

4.6.0

Python version

3.11.4

chipxsd commented 9 months ago

Not sure, but I think the error is referring to this: https://github.com/auth0/auth0-python/blob/21add3471ccadc1ab1b8def3ea049a8820e644a1/auth0/rest_async.py#L87-L93

adamjmcgrath commented 9 months ago

Thanks for raising this @chipxsd

Have identified the issue and a fix is in flight https://github.com/auth0/auth0-python/pull/556

Please continue to use 4.5.0 until this is resolved

WarpedPixel commented 9 months ago

This also seems to break the _async methods which suddenly return a coroutine instead of the object. For example

response = await self.auth0.users.create_async({'given_name': given_name,
  [...]
})
return response['user_id']  # indexing breaks here in 4.6 because response is a coroutine

returns a coroutine even after the await. This code works unchanged with 4.4 (and returns a dict) but breaks with 4.6 (and returns a coroutine).