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

AsyncStreamingClient 429 error on reconnect from uncaught exceptions #1904

Closed
acarasimon96 opened this issue May 24, 2022 · 5 comments
Closed
Labels
Bug This is regarding a bug with the library

Comments

@acarasimon96
Copy link

First, thank you so much for (finally) adding an asynchronous Twitter API v2 streaming client in Tweepy v4.10. It works great and I like the fact that it can disconnect immediately rather than waiting for the next response from Twitter. However, I ran into a strange bug with this new implementation.

I subclassed AsyncStreamingClient to make it run forever until the user pressed Ctrl+C instead of exiting on its own when an uncaught exception occurred. Every time the client reconnects after the unhandled exception, I would get a 429 error for about 30 seconds before it would connect to the stream endpoint just fine. But the weird part is when I manually shut down my program and start it back up within those 30 seconds, the client connects successfully on the first try.

Here's the code I used to reproduce this scenario:

import asyncio
import logging
import sys
from tweepy.asynchronous.streaming import AsyncStreamingClient

BEARER_TOKEN = "yourtokenhere"


class RunForeverClient(AsyncStreamingClient):
    async def on_keep_alive(self):
        await super().on_keep_alive()
        # This will be triggered on the first keepalive signal once connected
        raise Exception("Uncaught exception")

    def run_forever(self) -> asyncio.Task:
        async def task():
            while True:
                await self.filter()  # or self.sample()
                if sys.exc_info()[0] == KeyboardInterrupt:
                    break
        return asyncio.create_task(task())


async def main():
    client = RunForeverClient(BEARER_TOKEN)
    await client.run_forever()


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass
@SirBernardPhilip
Copy link

I have the same issue, I would like to be able to keep the stream running when any exception occurs and I'm struggling with it as well

@Arctek
Copy link

Arctek commented Jun 28, 2022

I believe this is caused by aiohttp not closing the underlying connection - as the 429 error occurs when you try and connect concurrently to the streaming endpoint.

Though I'm not familiar enough with aiohttp to dig any deeper as to what exactly is causing it.
FYI I'm on windows at the moment, so not sure if it has something to do with the underlying transport layer or just a general issue.

@nono-london

This comment was marked as off-topic.

@Harmon758

This comment was marked as resolved.

@Harmon758 Harmon758 added the Bug This is regarding a bug with the library label Oct 29, 2022
@Harmon758
Copy link
Member

Thanks for the bug report and the reproducer.
This took me a bit to wrangle, but it seems like this was due to lingering open sockets from closed SSL transports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This is regarding a bug with the library
Projects
None yet
Development

No branches or pull requests

5 participants