smirgol / plugin.video.crunchyroll

Watch videos from the anime platform Crunchyroll.com on Kodi
GNU Affero General Public License v3.0
51 stars 11 forks source link

asyncio error: set_wakeup_fd only works in main thread #44

Closed Yavos closed 8 months ago

Yavos commented 9 months ago

With the current version of the plugin on Windows, Nexus I get the following error whenever it tries to load something from crunchyroll:

2024-02-11 12:38:15.582 T:16580    info <general>: [PLUGIN] Crunchyroll: add_listables: Starting to retrieve data async
2024-02-11 12:38:15.588 T:16580   error <general>: Exception ignored in: 
2024-02-11 12:38:15.588 T:16580   error <general>: <function BaseEventLoop.__del__ at 0x00000160D7F1C3A0>
2024-02-11 12:38:15.588 T:16580   error <general>: 

2024-02-11 12:38:15.588 T:16580   error <general>: Traceback (most recent call last):

2024-02-11 12:38:15.588 T:16580   error <general>:   File "C:\Kodi\system\python\Lib\asyncio\base_events.py", line 656, in __del__

2024-02-11 12:38:15.588 T:16580   error <general>:     
2024-02-11 12:38:15.588 T:16580   error <general>: self.close()
2024-02-11 12:38:15.588 T:16580   error <general>: 

2024-02-11 12:38:15.588 T:16580   error <general>:   File "C:\Kodi\system\python\Lib\asyncio\proactor_events.py", line 679, in close

2024-02-11 12:38:15.589 T:16580   error <general>:     
2024-02-11 12:38:15.589 T:16580   error <general>: signal.set_wakeup_fd(-1)
2024-02-11 12:38:15.589 T:16580   error <general>: 

2024-02-11 12:38:15.589 T:16580   error <general>: ValueError
2024-02-11 12:38:15.589 T:16580   error <general>: : 
2024-02-11 12:38:15.589 T:16580   error <general>: set_wakeup_fd only works in main thread
2024-02-11 12:38:15.589 T:16580   error <general>: 

2024-02-11 12:38:15.594 T:16580   error <general>: sys:1: RuntimeWarning: coroutine 'complement_listables' was never awaited
                                                   RuntimeWarning: Enable tracemalloc to get the object allocation traceback

2024-02-11 12:38:15.595 T:16580   error <general>: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                                    - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                                   Error Type: <class 'ValueError'>
                                                   Error Contents: set_wakeup_fd only works in main thread
                                                   Traceback (most recent call last):
                                                     File "C:\Kodi\portable_data\addons\plugin.video.crunchyroll\default.py", line 37, in <module>
                                                       crunchyroll.main(sys.argv)
                                                     File "C:\Kodi\portable_data\addons\plugin.video.crunchyroll\resources\lib\crunchyroll.py", line 93, in main
                                                       return check_mode(args, api)
                                                     File "C:\Kodi\portable_data\addons\plugin.video.crunchyroll\resources\lib\crunchyroll.py", line 141, in check_mode
                                                       controller.list_filter(args, api)
                                                     File "C:\Kodi\portable_data\addons\plugin.video.crunchyroll\resources\lib\controller.py", line 328, in list_filter
                                                       view.add_listables(
                                                     File "C:\Kodi\portable_data\addons\plugin.video.crunchyroll\resources\lib\view.py", line 261, in add_listables
                                                       complement_data = asyncio.run(complement_listables(args, api, listables))
                                                     File "C:\Kodi\system\python\Lib\asyncio\runners.py", line 39, in run
                                                       loop = events.new_event_loop()
                                                     File "C:\Kodi\system\python\Lib\asyncio\events.py", line 758, in new_event_loop
                                                       return get_event_loop_policy().new_event_loop()
                                                     File "C:\Kodi\system\python\Lib\asyncio\events.py", line 656, in new_event_loop
                                                       return self._loop_factory()
                                                     File "C:\Kodi\system\python\Lib\asyncio\windows_events.py", line 310, in __init__
                                                       super().__init__(proactor)
                                                     File "C:\Kodi\system\python\Lib\asyncio\proactor_events.py", line 632, in __init__
                                                       signal.set_wakeup_fd(self._csock.fileno())
                                                   ValueError: set_wakeup_fd only works in main thread
                                                   -->End of Python script error report<--

2024-02-11 12:38:15.745 T:5792    error <general>: XFILE::CDirectory::GetDirectory - Error getting plugin://plugin.video.crunchyroll/menu/popular/anime/category/popularity
2024-02-11 12:38:15.773 T:24872   error <general>: CGUIMediaWindow::GetDirectory(plugin://plugin.video.crunchyroll/menu/popular/anime/category/popularity) failed

Supposedly according to the internet that's a python error on windows fixed in 3.8.2 (mine is running 3.8.15 though).

Adding

import sys

if sys.platform == "win32" and sys.version_info >= (3, 8, 0):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

to view.py seems to have solved that problem for me. Not sure if it's necessary to add in videostream.py as well but so far it seems to work properly.

smirgol commented 9 months ago

Hm, that's interesting. :) Thank you for providing a solution to this, too. I think I can add that in the next update.

smirgol commented 8 months ago

Should be fixed in the staging branches. If you could test that and report back, that'd be great! :)

Yavos commented 8 months ago

With the current version kodi crashes whenever it calls asyncio.run() a second time. Moving the code to view.py resolves that apparently.

I presume this is because if initialized in view.py it's a new session every time. My guess is that .run() is not the correct function to use multiple times in a single session.

smirgol commented 8 months ago

Ah, it was you who had this problem, that explains the crashes you had in the other issue :D

I thought it might be a good idea to have it in the main file, so it would affect the whole app, but apparently that doesn't work. Where do you move that call to inside the view.py precisely? Just at the top of the file, after the imports?

Yavos commented 8 months ago

Yup, just at the top after the imports.

There might be a more clean way to do this with global initialization. But I don't know enough about that. (Also the app crashed without any errors right after calling .run(). So it was hard to figure out what happened.)

smirgol commented 8 months ago

I've added it to the default.py to make it globally available, as this is the main function for the app. Not sure why this does not work. Did you reboot your system after updating the plugin? I've noticed that Kodi somehow seems to "cache" this file, so any changes to it are effective only after a reboot.

Yavos commented 8 months ago

Nope. Didn't reboot. Just restart Kodi itself. I'll try your suggestion later and report back if it worked.

I'm sure the change was working, though. Since the first asyncio.run() works without failure which it doesn't on my system if that fix is not applied at all.

Yavos commented 8 months ago

Same problem after reboot. First .run() works, second call of it crashes Kodi without any logging.

smirgol commented 8 months ago

Hm okay. I've moved it to view.py now.

Yavos commented 8 months ago

With the changes up to current commit 60684a7 everything seems to be working fine.