Page 1 of 1

Program hanging on exit...but why?

Posted: Thu Oct 19, 2023 6:48 pm
by bsilver
I'm at a bit of a loss in figuring out what's happening here and hope someone has some insight on diagnosing it.

The code is a bit much to paste here without narrowing down where something is going wrong, so I'll try to describe it as best as I can and post more details if someone can point out where to look.

To start off, there's one source file that at the bottom consists of three statements:

Code: Select all

LaunchClient()
Debug "LaunchClient() done"
End
Immediately above that is the definition for LaunchClient().

LaunchClient() defines some protected variables, launches a thread for listening to UDP packets, opens a form and sets some initial settings for that form's values.

It then starts a "Repeat...Until intEvent = #PB_Event_CloseWindow Or intQuit = 1" loop.

I click on the form's "Quit" button, and I see this series of events via Debug:
intEvent = WindowEvent()
intEvent is "PB_EVENT_GADGET"
Case Button_Quit
Locks a mutex, checks some settings from a struct, unlocks mutex.
Sets intQuit = 1
Just before the "Until..." statement, it printed a Debug message to show it exited all the select/if/etc. statements in the loop.
Printed a message that it's out of the Until loop
The next line is EndProcedure

Which then goes to the previously mentioned last three lines of the source code; LaunchClient(), prints "LaunchClient() done", then End. At that point the application hangs with a spinning beach ball and I have to force-quit, and PureBasic comes up with a debugged application unexpectedly quit message.

So something is hanging at the "End", I think? What could PB be doing at that point to close out the application, or where can I get more information on where a loop or error is happening?

Re: Program hanging on exit...but why?

Posted: Thu Oct 19, 2023 6:53 pm
by jacdelad
bsilver wrote: Thu Oct 19, 2023 6:48 pm ...launches a thread for listening to UDP packets...
At what point do you quit your thread? I suspect it is still running and becomes unresponsible, because you don't handle it anymore.

Re: Program hanging on exit...but why?

Posted: Thu Oct 19, 2023 7:01 pm
by bsilver
The thread is just looping and listening for UDP packets and until recently, clicking quit just closed everything out, including the thread. But I'll add a check to the thread so it exits or is terminated at that point and see if...for some reason...it's preventing the executable from closing.

Maybe there was a change in the 6.03 latest release that changed behavior?

Re: Program hanging on exit...but why?

Posted: Thu Oct 19, 2023 7:25 pm
by infratec
Is thread safe enabled?

Use WaitThread() to check if the thread is finished. There should be a variable which can terminate the thread.

But without the code ...

Can you reduce it to minimum?

Re: Program hanging on exit...but why?

Posted: Thu Oct 19, 2023 9:10 pm
by bsilver
This is kind of surprising, but after adding some logic to the thread for exiting I'm having trouble reproducing the hanging behavior. Before, it always exited without caring about that thread. The only change to the source involved editing it to use a combobox on a different form and was working on adding/reading items in the drop list when a test run ran into the hangs. I don't know if I triggered something in the course of editing the form or if 6.03 update had a change that I didn't run into until just now.

Either way...I guess PB now kind of requires threads to have housekeeping cleanup before exiting completely?

Re: Program hanging on exit...but why?

Posted: Thu Oct 19, 2023 9:48 pm
by mk-soft
With windows these are rare. With macOS, the threads must be terminated.

Search for Mini Thread Control

Re: Program hanging on exit...but why?

Posted: Thu Oct 19, 2023 11:13 pm
by mestnyi
If this is on Windows, then I had the same behavior if the window is created with #pb_any.

Code: Select all

OpenWindow( #PB_Any, ... 

Re: Program hanging on exit...but why?

Posted: Fri Oct 20, 2023 11:11 pm
by Olli
Add any Delay(1) :
- in the thread
- in the "server" main loop