Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-12

TProcess Running is unreliable on Linux

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anthony Walter

On Linux using 3.3.1 build of FPC I've found that in many situations the Running property of TProcess is unreliable. Is this a problem with which anyone is familiar? I've searched around and haven't found any discussions related to this problem.

In my experience, I've found situations where the Running property always returns True even when the process spawned by TProcess is definitely not running. As an example, I have a timer component checking every 100ms if a '/bin/bash' process I launched and am using with redirection of input / ouput pipes has exited.

procedure TMyForm.MyTimerTimer(Sender: TObject);
begin
  if not MyProcess.Running then
    Close;
end;

And is some cases even though I am sure the bash instance associated with MyProcess has exited, the above code will never close the form. I do not even have to do with the bash instance. I can just wait 1 minute, close it, and MyProcess still thinks it's running. This happens occasionally and randomly and is a bit frustrating to deal with.

Any insight as to what is happening would be appreciated.

Michael Van Canneyt
to Anthony Walter
Anthony Walter wrote
On Linux using 3.3.1 build of FPC I've found that in many situations the Running property of TProcess is unreliable. Is this a problem with which anyone is familiar? I've searched around and haven't found any discussions related to this problem.

I've never known it to report falsely.

Reading 'Running' finally does a WaitPID call and this is a kernel call:

Function TProcess.PeekExitStatus : Boolean;
var
   res: cint;
begin
   repeat
     res:=fpWaitPid(Handle,pcint(@FExitCode),WNOHANG);
   until (res-1) or (fpgeterrnoESysEINTR);
   result:=res=Handle;

if peekexitstatus returns true (i.e. the process has exited) then running is set to false.

Try running your program under strace
strace -f --trace=%process yourprogram
(so it only shows process management calls) and see what waitpid returns.

Michael.