How to know if a thread is active - application locks up lazarus / 21-General ---------------------------------------------------------------------- BosseB · 2021-03-25 18:11 I am trying to port a TService application from Windows/Delphi to Linux/FreePascal. On Linux I created a FreePascal simple program with no GUI ties and when testing it on Linux I just run it in a terminal. Eventually it is supposed to become a systemd service... On Delphi a number of timers were used in order to start processing that would not block other functions from being processed. In the original no threads were used. However I could not keep this structure on Linux because the execution inside a timer event blocks reception of serial data coming in via com port events. So instead of running the process inside the startexecution timer event until the process is complete I created a thread to start the process inside the timer event and when it has been created and therefore the processing started in the execute of the thread the timer event ends. (I had to add a "wait for completion" loop in the caller) This made the serial port events come through as they did on Delphi. So far so good, but I have a problem here that is probably due to the handling of the thread itself: The thread is declared in the object private section: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TTaskExecThread = class(TThread)  protected    procedure Execute; override;  public    Owner: TSSRemoteServer;    Task: TScheduledTask;    CancelOP: boolean;  end; procedure TTaskExecThread.Execute;begin  if not Assigned(Owner) then exit;  if not Assigned(Task) then exit;  {Here is where the execution is done, return when finished}  if not Owner.ExecuteTask(Task) then    Owner.LogDebug('Execution of task failed, quitting..')  else    Owner.LogDebug('Quit thread after OK task execution');end;    TSSRemoteServer = class  private    FOwner: TObject;    FSSConnection: TSSConnection;    FCurrentCmdFile: TSSCommandFile;    FCurrentMeasureFile: TSSMeasureFile;    FExecTaskThr: TTaskExecThread;  <== Declared thread here    .... And here is the timer event that triggers the creation of the thread: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TSSRemoteServer.OnExecuteTimer(Sender: TObject);begin  FTimExecute.Enabled := false; //No more executions of the timer  {Create and start the task execution thread:}  FExecTaskThr := TTaskExecThread.Create(true);  FExecTaskThr.Owner := Self;  FExecTaskThr.Task := FCurrentTask;  FExecTaskThr.FreeOnTerminate := true;  FExecTaskThr.CancelOP := false;  FExecTaskThr.Start;  //This is the old execution way:  //if not ExecuteTask(FCurrentTask) then   //  LogDebug('Execution of task failed, quitting OnExecuteTimer..')  //else  //  LogDebug('Quitting OnExecuteTimer after task execution');end;  So now I have a thread that runs what was previously processed *inside* the timer event and the timer event now exits *ahead* of the task execution being done which is needed in order to get the serial comm events through to their handler. The serial comm is towards the external equipment while it runs a task. This was what made the system work and really run a task. So the concept works OK to execute one run of a task... But now the problem is that when I need to later run a new task the system locks up somewhere and I cannot figure out why... I have a suspicion that the thread previously used is not really properly disposed of at the end even though I had set this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FExecTaskThr.FreeOnTerminate := true; So I would like to know how I can check if the FExecTaskThr object is still running/existing to avoid trying to re-create it or even worse recreate another instance re-using the same thread variable... Does the FreeOnTerminate make sure to set the thread variable to NIL so I could check that? ---------------------------------------------------------------------- Fred vS · 2021-03-25 18:33 Hello. Maybe nothing related, but you may use a loop in your thread.execute and not re-create always the same thread: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i vardofire: boolean = false; ... procedure TTaskExecThread.Execute;begin while not terminated dobegin  if (dofire) and (Assigned(Owner)) and (Assigned(Task)) then   begin    {Here is where the execution is done, return when finished}    if not Owner.ExecuteTask(Task) then    Owner.LogDebug('Execution of task failed, quitting..')  else    Owner.LogDebug('Quit thread after OK task execution');    dofire := false;  end;   sleep(1);  // or use a PRTLEvent to pause the threadend; ---------------------------------------------------------------------- engkin · 2021-03-25 19:02 Use TThread.OnTerminate to, for instance, assign nil to the thread variable. FreeOnTerminate does *not* assign nil to your variable, instead it calls Free when it exits the Execute procedure. ---------------------------------------------------------------------- BosseB · 2021-03-25 19:47 Thanks both! Do I get this right? I will use a thread, which is created in the TSSRemoteServer .Create constructor. 1) Create the thread already in the TSSRemoteServer constructor, but let it just idle-loop for the time being. 2) When a task is to be executed then set the thread properties as needed for handling the task and then start it off by setting the dofire value to true. 3) If I make dofire a public property I can check if it is still processing the task by reading its value, right? 4) When I want to close the server I shut down the thread by using Terminate. If it is running a task at the time I have to use CancelOP first to make it abort the task and then use Terminate, but that is a deeper issue... I will look at how I can implement this and test it.  BTW: The target is a RaspberryPi4 and I am using Lazarus 2.0.8 with FPC 3.0.4 at the moment. Will migrate to 2.0.10/3.2.0 when this project works. ---------------------------------------------------------------------- Fred vS · 2021-03-25 19:50 Quote from: BosseB on March 25, 2021, 08:47:13 pmThanks both! Do I get this right? I will use a thread, which is created in the TSSRemoteServer .Create constructor. 1) Create the thread already in the TSSRemoteServer constructor, but let it just idle-loop for the time being. 2) When a task is to be executed then set the thread properties as needed for handling the task and then start it off by setting the dofire value to true. 3) If I make dofire a public property I can check if it is still processing the task by reading its value, right? 4) When I want to close the server I shut down the thread by using Terminate. If it is running a task at the time I have to use CancelOP first to make it abort the task and then use Terminate, but that is a deeper issue... I will look at how I can implement this and test it. It is how I see it, yes. ---------------------------------------------------------------------- avra · 2021-03-26 08:43 Quote from: BosseB on March 25, 2021, 07:11:19 pmBut now the problem is that when I need to later run a new task the system locks up somewhere and I cannot figure out why...Maybe serial is still locked and can not be reopened? Do you check for that? I suggest you to log every action so that you can track what exact action made logging stopped. ---------------------------------------------------------------------- BosseB · 2021-03-26 10:04 Quote from: avra on March 26, 2021, 09:43:04 amQuote from: BosseB on March 25, 2021, 07:11:19 pmBut now the problem is that when I need to later run a new task the system locks up somewhere and I cannot figure out why...Maybe serial is still locked and can not be reopened? Do you check for that? I suggest you to log every action so that you can track what exact action made logging stopped. I do have logging in my application, in fact it is "everywhere" so the logfiles get really big quickly... What I see is that the log *ends* at a point where it says that it has sent the DLE char to the equipment and normally there would be a log line immediately following showing the response from teh equipment on connection via serial. Not so here, the log from now on is totally empty and I have to use kill -9 on the server application to be able to start over. No logging when I disconnect the client over here from the server, which is typically handled by the application main loop. So it is really locked up hard somewhere and not responding to a Ctrl-C in the terminal where it runs or a simple kill ! I have to use kill -9 to stop it... The thread count for the application in this state is 7 where I would assume it should be 5 or 6 depending on the state of the execution thread discussed here. I probaly should try adding some more try-except blocks to capture whatever is blowing up... I hope to get time today to make the changes suggested here and resume the hardware testing. The test system is some 8500 km away so I can only do what is possible using VPN, VNC and SSH... ---------------------------------------------------------------------- Fred vS · 2021-03-26 10:21 Quote from: BosseB on March 25, 2021, 08:47:13 pm 1) Create the thread already in the TSSRemoteServer constructor, but let it just idle-loop for the time being. 2) When a task is to be executed then set the thread properties as needed for handling the task and then start it off by setting the dofire value to true. Hello. You may use a PRTLEvent instead of sleep(1): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     TTaskExecThread = class(TThread)  protected    procedure Execute; override;  public    Owner: TSSRemoteServer;    Task: TScheduledTask;    CancelOP: boolean;    evPause: PRTLEvent;// for pausing  end;       ...      procedure TTaskExecThread.Execute;  begin     evPause := RTLEventCreate;      while not terminated do       begin         if (Assigned(Owner)) and (Assigned(Task)) then           begin           {Here is where the execution is done, return when finished}          if not Owner.ExecuteTask(Task) then            Owner.LogDebug('Execution of task failed, quitting..')           else           Owner.LogDebug('Quit thread after OK task execution');       end;       RTLeventResetEvent(evPause); // pause thread     end;     RTLeventdestroy(evPause);  end;     ...       RTLeventSetEvent(evPause); // to fire ExecuteTask = disable pause It will not solve your problem (but, who knows, it may help). Fre;D PS: My english is poor, but, imho, the name used for the RTLevent properties should be reverse: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i RTLeventSetEvent(evPause); // to enable pause and Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i RTLeventResetEvent(evPause);; // to disable pause ---------------------------------------------------------------------- BosseB · 2021-03-26 12:54 Quote from: BosseB on March 26, 2021, 11:04:29 amI probably should try adding some more try-except blocks to capture whatever is blowing up... I hope to get time today to make the changes suggested here and resume the hardware testing. The test system is some 8500 km away so I can only do what is possible using VPN, VNC and SSH... I decide to put a try-except into the ExecuteTask procedure and log if there is an exception there. No other changes. But it behaves the same way as before and there is no exception... I checked the thread count too: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 5 - Before commanding start of task7 - When preparing data for the task (sending to the equipment by serial)8 - When the task is running (progress reports received by serial)6 - After task finished and power switched off (short intermission)7 - When power is again switched on to retrieve the data (no communication) And here it stops because if fails to connect. Somewhere else there is something that causes the failure.... So I need to again look at the thread system and also the serial comm... ---------------------------------------------------------------------- BosseB · 2021-03-28 10:18 Result feedback: 1) New thread structure I have changed my thread to look as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  { TTaskExecThread }   TTaskExecThread = class(TThread)  protected    procedure Execute; override;  private    FTaskRunning: boolean;    FTask: TScheduledTask;  public    Owner: TSSRemoteServer;    property TaskRunning: boolean read FTaskRunning;    function StartTask(Task: TScheduledTask): boolean;    function StopTask: boolean;  end; procedure TTaskExecThread.Execute;var  sMsg: string;begin  FTaskRunning := false;  FTask := NIL;  while not Terminated do  begin    if FTaskRunning and Assigned(FTask) then //Execute task    begin    if not Owner.ExecuteTask(FTask) then  {Here is where the execution is done, return when finished}      begin        sMsg := 'Execution of task failed, quitting..';        Owner.LogDebug(sMsg);        Debug_Writeln(sMsg);      end      else      begin        sMsg := 'Retrieve the result file from SS.';        Owner.LogDebug(sMsg);      Debug_Writeln(sMsg);        Owner.FinishMeasureCycle;        sMsg := 'Quitting after successful task execution';        Owner.LogDebug(sMsg);        Debug_Writeln(sMsg);      end;      FTaskRunning := false;      FTask := NIL;    end;    sleep(10);  end;end;  2) Thread execution start/stop functions I have made the thread permanent for the life of the server object and instead I'm flagging the execution start via a thread start function, and I added a StopTask function in order to be able to interrupt an execution: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TTaskExecThread.StartTask(Task: TScheduledTask): boolean;var  sMsg: string;begin  Result := false;  if FTaskRunning then  begin    sMsg := 'Cannot start new task, already running ' + FTask.Name;    Debug_Writeln(sMsg);    Owner.LogDebug(sMsg);  end  else  begin    FTask := Task;    FTaskRunning := true;    Result := true;  end;end; function TTaskExecThread.StopTask: boolean;begin  Result := Owner.CancelOp;end;  3) Found the lock-up culprit.. My lockup problem, which caused me to start this thread in the first place, has now been located! It is inside the Owner.ExecuteTask() function which is called in the thread Execute. What happens is that the task preparation and data transfer is done and then a start command is sent to the external equipment. Then the equipment starts working and sends progress reports via serial comm. This did not work until I put the execution into the thread as described above because the serial comm reports were not properly received and handled. But while looking for the cause of my problems I had inadvertently removed the break condition for a wait loop inside the ExecuteTask function. This is the wait loop, breaks if the measure cycle is finished or if forced to cancel: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   while FMeasureCycleRunning and not FCancelOp do  begin    Sleep(100);  end;  But the FMeasureCycleRunning flag clearing was missing in the serial message handler... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if PopCommand(Cmd, Data) then  begin    if Cmd = 'MEASDONE' then    begin      Callback('MEASDONE=' + Data);      FMeasureCycleRunning := false;  //<== This was missing      Debug_Writeln('MEASDONE=' + Data + ' Now download result')    end  End result was that the ExecuteTask function never returned and the code blocked after starting the first execution. Now working fine, though!