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

"ResourceWarning: Unclosed socket" while stopping generator iteration #2

Closed
isaacclinton opened this issue Sep 3, 2021 · 2 comments

Comments

@isaacclinton
Copy link

I would like to exit the get channel generator after I have reached the video I want. However, it seems there is no safe exit, a socket is left open. Below is the code:

import scrapetube

channel_videos_generator = scrapetube.get_channel(channel_id=channel_id, sort_by="newest")

for video in channel_videos_generator:
    if video["videoId"] != since_video_id:
        yield video["videoId"]
    else:
        channel_videos_generator.close()

When I run the above code, I get the warning below:

ResourceWarning: unclosed <ssl.SSLSocket fd=756, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.0.20', 58222), raddr=('216.58.223.78', 443)>
  channel_videos_generator.close()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
@dermasmid
Copy link
Owner

thanks for reporting!
i had to add this to reproduce, i guess you were running it with some sort of testing

import warnings

warnings.filterwarnings(action="always", message="unclosed", category=ResourceWarning)

@isaacclinton
Copy link
Author

It's true, I have incorporated testing in my codebase. I filtered the warning. I went through scrapetube's source code and it seems a session in a while loop might not be closed after exiting abruptly; or maybe the warning is only raised during testing. Thanks though, in case the problem is inherent, and you consider fixing it, I will appreciate.

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