Recent

Author Topic: Potential resource leak in TThread on Unix - no EndThread called  (Read 1935 times)

abouchez

  • Full Member
  • ***
  • Posts: 135
    • Synopse
Potential resource leak in TThread on Unix - no EndThread called
« on: February 09, 2018, 09:53:19 pm »
I've observed a weird behavior with FPC 3.1.1 trunk and Linux.
Using htop + user threads showing on a server program, a lot of threads are listed, even if they are not active any more.

Looking in details, I discovered that in rtl/unix/tthread.inc, ThreadFunc() doesn't call EndThread if FreeOnTerminate=false.

Code: Pascal  [Select][+][-]
  1. function ThreadFunc(parameter: Pointer): ptrint;
  2. var
  3. ....
  4.   LFreeOnTerminate := LThread.FreeOnTerminate;
  5.   LThread.DoTerminate;
  6.   LThread.FFinished := True;
  7.   if LFreeOnTerminate then
  8.     begin
  9.       WRITE_DEBUG('Thread ',ptruint(lthread),' should be freed');
  10.       LThread.Free;
  11.       WRITE_DEBUG('Thread freed');
  12.       WRITE_DEBUG('thread func calling EndThread');
  13.       // we can never come here if the thread has already been joined, because
  14.       // this function is the thread's main function (so it would have terminated
  15.       // already in case it was joined)
  16.       EndThread(Result);
  17.     end;
  18. end;
  19.  

So no pthread_detach/pthread_exit() seems to be called.

What did I miss?
I can't find the source of this resource leak, which could be very difficult with serious multithreaded process on a Linux server...

abouchez

  • Full Member
  • ***
  • Posts: 135
    • Synopse
Re: Potential resource leak in TThread on Unix - no EndThread called
« Reply #1 on: February 09, 2018, 10:27:37 pm »
I missed something for sure.

There is no resource leak if I look closer with strace, but I can't say honnestly why it works!
Any idea?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Potential resource leak in TThread on Unix - no EndThread called
« Reply #2 on: February 10, 2018, 01:45:11 am »
You are looking at OS specific code. Try the common code ThreadProc that is the real proc used when BeginThread is called:
Code: Pascal  [Select][+][-]
  1. //\rtl\objpas\classes\classes.inc
  2. function ThreadProc(ThreadObjPtr: Pointer): PtrInt;
  3.   var
  4.     FreeThread: Boolean;
  5.     Thread: TThread absolute ThreadObjPtr;
  6.   begin
  7. ....
  8.     FreeThread := Thread.FFreeOnTerminate;
  9.     Result := Thread.FReturnValue;
  10.     Thread.FFinished := True;
  11.     Thread.DoTerminate;
  12.     if FreeThread then
  13.       Thread.Free;
  14.     EndThread(Result);
  15.   end;

1st Edit:
Spoke to soon, Unix does not use ThreadProc.

2nd Edit:
Using cthreads:
calls CBeginThread and passes ThreadMain:
Code: Pascal  [Select][+][-]
  1. //\rtl\unix\cthreads.pp
  2.     function ThreadMain(param : pointer) : pointer;cdecl;
  3. ...
  4.         pthread_exit(ThreadMain);
  5.       end;
« Last Edit: February 10, 2018, 02:13:27 am by engkin »

abouchez

  • Full Member
  • ***
  • Posts: 135
    • Synopse
Re: Potential resource leak in TThread on Unix - no EndThread called
« Reply #3 on: February 10, 2018, 10:03:36 am »
Sounds fair enough.

This code is difficult to follow, with at least TjreadProc/ThreadFunc/ThreadMain involved... ;)

Thanks for the input!

 

TinyPortal © 2005-2018