Recent

Author Topic: [SOLVED] TThread.WaitFor locks forever  (Read 4558 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1156
  • Professional amateur ;-P
[SOLVED] TThread.WaitFor locks forever
« on: September 04, 2023, 03:35:45 am »
Hey Y'all,

NOTE: Putting this here since I was only able to test this on Linux(Ubuntu 23.04 64b).

I'm exploring all the possibilities of threads since I have a project that will heavily rely on them.
In order to do that I've been following the Multithreaded Application Tutorial entry on the wiki.
One of the See Also links mentioned is the Manager Worker Threads System.
I downloaded the archived files from Source Forge: Manager Worker Thread Example.

Attached is the same example with some alterations:
  • Added an event to log stuff on the main forms ( Not using it since I needed to use WriteLn to debug the Destroy parts )
  • Corrected a small bug that would only add one worker thread to the pool.

Now, here's the problem:
  When the application ends, it tries to Terminate->WaitFor->FreeAndNil each of the created worker threads.
  That's expected and it should be ok for all the created threads!! ( In the example I use a queue of 3 )

But in my Linux environment, it will do that for Worker 0, but then it will get stuck on the WaitFor of Worker 1 forever.
Since all 3 workers are created equal and they don't differ in anything obvious that I can see, I'm completely baffled and in need of help from the community!!

Many thanks in advance!!

Cheers,
Gus
« Last Edit: September 05, 2023, 08:14:45 pm by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

440bx

  • Hero Member
  • *****
  • Posts: 4890
Re: TThread.WaitFor locks forever
« Reply #1 on: September 04, 2023, 04:20:15 am »
Just out of curiosity... is the code supposed to work on Windows ? and if it is then have you tried the code in Windows ?  if yes, does the problem show up there too ?


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1156
  • Professional amateur ;-P
Re: TThread.WaitFor locks forever
« Reply #2 on: September 04, 2023, 04:29:48 am »
Hey 440bx,

Just out of curiosity... is the code supposed to work on Windows ?

I think, but I'm not quite sure, that the original coder did work in Windows, so I'm kinda assuming a YES here.

and if it is then have you tried the code in Windows ? 

The only way I can test under Windows is with wine.
All my attempts doing so have resulted in the app claiming the error depicted on the attached image.
I've done what I know to remove any files being loaded at start, but I've failed.

if yes, does the problem show up there too ?

As you can see by the above error in wine, I haven't been able to even get to the Close App part of the test :(

Cheers,
Gus
« Last Edit: September 04, 2023, 04:32:36 am by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

sikale@aliyun.com

  • Newbie
  • Posts: 1
Re: TThread.WaitFor locks forever
« Reply #3 on: September 04, 2023, 05:56:17 am »
Unable to close in windows OS


paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: TThread.WaitFor locks forever
« Reply #4 on: September 04, 2023, 07:02:51 am »
Remove SetSize(0) from CriticalSection (in procedureTManagerThread.Clear) because "GetNextItem" already blocks CS. 
And why so many critical sections? 
   
Tested on Windows 10 and Debian 11
Best regards / Pozdrawiam
paweld

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1156
  • Professional amateur ;-P
Re: TThread.WaitFor locks forever
« Reply #5 on: September 05, 2023, 01:05:43 am »
Hey paweld !!

Remove SetSize(0) from CriticalSection (in procedureTManagerThread.Clear) because "GetNextItem" already blocks CS. 

This is awesome !!! SOOOO MANY THANKS !!
I never suspected a deadlock and so I never went and find out :)

And why so many critical sections? 

I asked myself the same question, but since the code is not mine and I dunno how to contact the author, I can't give you a good answer, sorry!!!
Some of them kinda make sense when you're moving items from the Waiting to the Processing, And the Processing to the Finished.
But with so many of them, one race condition would inevitably slip through!!
   
Tested on Windows 10 and Debian 11

Again, many, so many thanks for this!!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

440bx

  • Hero Member
  • *****
  • Posts: 4890
Re: TThread.WaitFor locks forever
« Reply #6 on: September 05, 2023, 01:30:14 am »
I never suspected a deadlock and so I never went and find out :)
Just as a matter of course, when you have something that uses more than one (1) synchronization object, the possibility of a deadlock should always be a consideration.

In this particular case, the last message is "waiting for worker 1" in spite of the fact that worker 1 has ended.  This strongly suggests that worker 1 ended without releasing a sync object.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1156
  • Professional amateur ;-P
Re: TThread.WaitFor locks forever
« Reply #7 on: September 05, 2023, 08:12:43 pm »
Hey 440bx,

I never suspected a deadlock and so I never went and find out :)
Just as a matter of course, when you have something that uses more than one (1) synchronization object, the possibility of a deadlock should always be a consideration.

You're absolutely correct on this one!!
When I saw that many CSs I asked the same question as @paweld, "Why so many?", and the next thing that came to mind was "This is probably cause some issues if not well managed".
But then I completely forgot about it and just kinda panicked when the thing hung !!

In this particular case, the last message is "waiting for worker 1" in spite of the fact that worker 1 has ended.  This strongly suggests that worker 1 ended without releasing a sync object.

From what @paweld mentioned on the above message, I think it's more of a re-entrant issue than something not releasing a CS.
I haven't had the opportunity to read @paweld's corrected code.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

440bx

  • Hero Member
  • *****
  • Posts: 4890
Re: TThread.WaitFor locks forever
« Reply #8 on: September 05, 2023, 08:34:14 pm »
Hi Gus,

From what @paweld mentioned on the above message, I think it's more of a re-entrant issue than something not releasing a CS.
That's possible too.  The last message displayed indicates there is, most likely, something "suspicious" going on with the sync objects.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018