Program hanging on exit...but why?

Just starting out? Need help? Post your questions and find answers here.
bsilver
User
User
Posts: 27
Joined: Tue Jun 27, 2023 3:36 pm

Program hanging on exit...but why?

Post 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?
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
bsilver
User
User
Posts: 27
Joined: Tue Jun 27, 2023 3:36 pm

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

Post 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?
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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?
bsilver
User
User
Posts: 27
Joined: Tue Jun 27, 2023 3:36 pm

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

Post 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?
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post by mk-soft »

With windows these are rare. With macOS, the threads must be terminated.

Search for Mini Thread Control
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

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

Post 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, ... 
Olli
Addict
Addict
Posts: 1202
Joined: Wed May 27, 2020 12:26 pm

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

Post by Olli »

Add any Delay(1) :
- in the thread
- in the "server" main loop
Post Reply