CastagnaIT / plugin.video.netflix

InputStream based Netflix plugin for Kodi
MIT License
1.89k stars 258 forks source link

NFAuthenticationKey.py: polling loop without wait burns 100% CPU and makes system unresponsive #1673

Closed rptaylor closed 2 months ago

rptaylor commented 5 months ago

Netflix add-on version

N/A

Operative systems used

Linux (Ubuntu / Mint / ...)

Kodi version used

Other (specify in description)

Description of the bug

There is a polling loop with no delay. It executes as fast as possible, burning up 100% CPU and heating up your room.

    def wait_user_logged(self):
        start_time = time.time()
        while time.time() - start_time < 300:  # 5 min
            history_data = self.ws_request('Page.getNavigationHistory')
            history_index = history_data['currentIndex'] 
            # If the current page url is like "https://www.n*****x.com/browse" means that the user should have logged in successfully
            if '/browse' in history_data['entries'][history_index]['url']:
                return True
        return False

On some systems (I tested on a VM) it can make the system extremely sluggish and unresponsive, potentially requiring the system to be hard powered off (this might be related to IOPS, not CPU use). You can easily see the problem by inserting a line in the while loop like print('this loop has no chill') and see thousands of lines per second being printed.

Steps to reproduce the behavior

Debug log - mandatory

not applicable

Possible fix

Easy, just wait 1s on each loop iteration:

    def wait_user_logged(self):
        start_time = time.time()
        while time.time() - start_time < 300:  # 5 min
            time.sleep(1)
            history_data = self.ws_request('Page.getNavigationHistory') 
            history_index = history_data['currentIndex']
            # If the current page url is like "https://www.n*****x.com/browse" means that the user should have logged in successfully
            if '/browse' in history_data['entries'][history_index]['url']:
                return True
        return False

I did this and confirmed it fixed the problem, and the timeout still worked as expected after 5 minutes. I'd make a PR but I don't see the NFAuthenticationKey.py file in github.

Additional context

Tested on Fedora 39.

python 3.12.0 python3-pycryptodomex-3.19.0 python3-websocket-client-1.3.3-5

Screenshots

No response

rptaylor commented 2 months ago

FYI @CastagnaIT this is an easy one-line fix.

CastagnaIT commented 2 months ago

thanks change added however the NFAuth is on my repository list just find it

rptaylor commented 2 months ago

Thanks! Oh okay, I found it here: https://github.com/CastagnaIT/NFAuthenticationKey/commit/9d455f02af0a61aa1159ba9bbe5ab219736ed1ab