r0x0r / pywebview

Build GUI for your Python program with JavaScript, HTML, and CSS
https://pywebview.flowrl.com
BSD 3-Clause "New" or "Revised" License
4.62k stars 537 forks source link

Deadlock while closing the window with persistent threads running on Windows (again) #1439

Open gitillusion09 opened 1 month ago

gitillusion09 commented 1 month ago

Hello @r0x0r :) When closing, the app keeps hanging forever. I know there was already a similar issue back in 2017 (https://github.com/r0x0r/pywebview/issues/138), but I think the fix does not work (anymore ?) on Windows 11.
I work with pywebview 5.1. I have a renderer thread that in some cases, calls the evaluate_js method periodically. If the application closes at this exact moment, and even though the thread has been joined, the app keeps hanging forever. Does anyone have the issue on Windows?

I am not sure to have the skills to work on this issue myself...

r0x0r commented 1 month ago

Do you happen to have a standalone example that would reproduce the bug?

gitillusion09 commented 1 month ago

Yes, here you go: https://github.com/gitillusion09/pywebview-test

It is a basic (very basic) example but you got the idea. I took as reference your Serverless example, and tweaked it a bit to reproduce the bug: just launch it, the thread will start printing current date on the UI. When closing the app (with the window's cross), the app hangs on forever, even though I terminate the thread from the on_closing() method.

Thanks a lot for your time

gitillusion09 commented 1 month ago

@r0x0r It seems to me that the following line should_cancel = self.closing.set() in the on_closing() method is what is blocking the app

r0x0r commented 1 month ago

I modified event handler logic so that instead of launching a thread that is blocked via a semaphore, it executes event handlers synchronously. The fix concerns only the closing event. This fixes the problem in your example. The fix could be found in the master branch.

Another fix would be making your background thread daemon and getting rid of the join.

gitillusion09 commented 1 month ago

Thanks a lot for your fix ! Appreciate it :)

github-actions[bot] commented 3 days ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.