Skip to content
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

RuntimeError: aclose(): asynchronous generator is already running #340

Closed
FF-TeaDoc opened this issue May 6, 2022 · 9 comments
Closed

Comments

@FF-TeaDoc
Copy link

When im using py-cord with aiosonic, and when im using aiosonic request in slash command, im getting this error.

To Reproduce
Create slash_command using module py-cord
add to func with slash command some aiosonic requests.
Profit

Screenshots
image

Additional context
Same requests from last issue.

@sonic182
Copy link
Owner

Hi, please try with new aiosonic release 0.14.1

@FF-TeaDoc
Copy link
Author

Hello! Still same error...
image

@sonic182
Copy link
Owner

Can you put an example of your code?

@FF-TeaDoc
Copy link
Author

    @slash_command(name='test', guild_ids=[779313655740760095])
    async def test(self, ctx):
        req = aiosonic.HTTPClient()
        await ctx.respond(content="wait a sec")
        finded = db.findEm(ctx.author.id)
        data = AuthorizeViaDB(finded)
        device = DeviceAuth(data.actual_account_dict['account_id'], data.actual_account_dict['device_id'], data.actual_account_dict['secret'], req)
        await device.data()
        mcp = MCP(req)
        request = await mcp.QueryPublicProfile(device.account_id, device.access_token)
        await _request(access_token=device.access_token, req=req).DeleteToken()
        embed = Embed(color=0x730d73)
        embed2 = Embed(color=0x730d73)
        embed3 = Embed(color=0x730d73)
        embed4 = Embed(color=0x730d73)
        embeds = []
        count = 0
        for i in request['profileChanges']:
            for templateId in i['profile']['items']:
                item = i['profile']['items'][templateId]
                if item['templateId'].startswith("AccountResource:"):
                    items = Items['Items'][item['templateId']]
                    name = items['name'][data.lang.lower()]
                    emoji = items['smile']
                    type = Items["Item Types"]['accountresource'][data.lang.lower()]
                    quantity = item['quantity']
                    embed.add_field(name=f"{emoji}{name}", value=f"({type}) x{quantity}")
                    count = count + 1
                    if count == 20:
                        embeds.append(embed)
                        embed = embed2
                    if count == 40:
                        embeds.append(embed)
                        embed = embed3
                    if count == 60:
                        embeds.append(embed)
                        embed = embed4

        embeds.append(embed)
        await ctx.edit(content=None, embeds=embeds)

Some shit code with counts, but don't pay attention to it.

@FF-TeaDoc
Copy link
Author

    async def QueryPublicProfile(self, account_id, access_token, profileId='campaign', raw=False):
        response = await self.req.post(self.url.format(account_id,'QueryProfile'),
            headers={
                "Authorization": f"bearer {access_token}",
                "Content-Type": "application/json"
            },
            params={
                "profileId": profileId,
                "rvn": -1,
            },
            data="{}") 
        return await response.json() if not raw else await response.content()
class _request:

    def __init__(
            self,
            access_token=None,
            AccountID=None,
            DeviceID=None,
            Secret=None, req=None
        ):
        self.req: HTTPClient = req
        self.access_token = access_token
        self.AccountID = AccountID
        self.DeviceID = DeviceID
        self.Secret = Secret
        self.json_loader = orjson.loads

    async def DeleteToken(self):
        resp = await self.req.delete(
            f'https://account-public-service-prod.ol.epicgames.com/account/api/oauth/sessions/kill/{self.access_token}',
            headers={
                "Authorization": f"bearer {self.access_token}"},
            params={
                'access_token': self.access_token})
class DeviceAuth:

    def __init__(self, AccountID, DeviceID, Secret, req):
        self.account_id = AccountID
        self.device_id = DeviceID
        self.secret = Secret
        self.req = req
        self.json_loader = orjson.loads
    
    async def data(self):
        request = await _request(AccountID=self.account_id, DeviceID=self.device_id, Secret=self.secret, req=self.req)._DeviceAuth()
        self.access_token = request.get('access_token', None)
        self.account_id = request.get('account_id', None)
        self.displayName = request.get('displayName', None)
        self.expires_in = request.get('expires_in', None)
        self.expires_at = request.get('expires_at', None)
        self.token_type = request.get('token_type', None)
        self.refresh_token = request.get('refresh_token', None)
        self.refresh_expires = request.get('refresh_expires', None)
        self.refresh_expires_at = request.get('refresh_expires_at', None)
        self.client_id = request.get('client_id', None)
        self.internal_client = request.get('internal_client', None)
        self.client_service = request.get('client_service', None)
        self.app = request.get('app', None)
        self.in_app_id = request.get('in_app_id', None)
        self.errorCode = request.get('errorCode', None)
        self.errorMessage = request.get('errorMessage', None)
        self.messageVars = request.get('messageVars', None)
        self.numericErrorCode = request.get('numericErrorCode', None)
        self.originatingService = request.get('originatingService', None)
        self.intent = request.get('intent', None)
        self.error_description = request.get('error_description', None)
        self.error = request.get('error', None)
        self.dict = request

@FF-TeaDoc
Copy link
Author

FF-TeaDoc commented May 15, 2022

Forget to mention:
Error appears, when i spamming with command. Like 2 times in 5 seconds.

@sonic182
Copy link
Owner

Try

await req.close()

At the end, better if inside a try/finally

@sonic182
Copy link
Owner

Other way is to do:

async def myfunc():
    async with aiosonic.HttpClient() as client:
        res = await client.get("https://www.google.com")
        # ... use the client, the context manager will close it at the end  

@FF-TeaDoc
Copy link
Author

FF-TeaDoc commented May 17, 2022

Second variant is works, but it brokes json_serialize / decoder and http2 parameters.
Throws errors:
"ClientSession._request() got an unexpected keyword argument 'json_serializer" (and http2),
ClientResponse.json() got an unexpected keyword argument 'json_decoder'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants