Recent

Author Topic: openDocument and openURL leave zombie processes  (Read 1008 times)

fedkad

  • Full Member
  • ***
  • Posts: 118
openDocument and openURL leave zombie processes
« on: May 02, 2019, 06:50:50 pm »
Each time I call openDocument to open a file or openURL to open a web address in the default browser, I see accumulating defunct processes. The parent process of these is my calling program.

Code: [Select]
myuser    17616 13139  0 19:36 pts/2    00:00:00 /home/xxx/test5
myuser    17626 17616  0 19:37 pts/2    00:00:00 [xdg-open] <defunct>
myuser    17809 17616  0 19:39 pts/2    00:00:00 [xdg-open] <defunct>

Assuming that a program makes hundreds of such calls, there will be unnecessary process table space used in the system until my program ends.

john horst

  • Jr. Member
  • **
  • Posts: 53
    • JHorst
Re: openDocument and openURL leave zombie processes
« Reply #1 on: May 02, 2019, 07:57:15 pm »
Free the open document before calling the next. A child process is the responsibility of the parent. Defunct is something that was completed and left running by your program in this case.

Without knowing much more, maybe you need to use xdg-open directly and wait() close() or even launch to the background &.

fedkad

  • Full Member
  • ***
  • Posts: 118
Re: openDocument and openURL leave zombie processes
« Reply #2 on: May 03, 2019, 01:25:05 pm »
The openDocument function uses TProcessUTF8 under the hood. The code of openDocument first tries to locate a launcher (xdg-open in my case) and then it creates a command line like this:

Code: Pascal  [Select]
  1. BrowserProcess.CommandLine := '/usr/bin/xdg-open ''/home/myuser/Documents/mydocument.txt'''

Then it calls:

Code: Pascal  [Select]
  1. BrowserProcess.Execute;

When I run the same command (/usr/bin/xdg-open '/home/myuser/Documents/mydocument.txt') under bash, I see that it returns immediately (with return status of 0) even after it starts the appropriate text editor in the background as expected. So, the Execute method of TProcessUTF8 should wait and read the return status of the xdg-open ... command. However, I think is does not. I was not able to trace this, because the debugger did not enter into the source code (?) of  TProcessUTF8.Execute (that is, the statement, BrowserProcess.Execute).
« Last Edit: May 03, 2019, 01:41:43 pm by fedkad »

sash

  • Sr. Member
  • ****
  • Posts: 275
Re: openDocument and openURL leave zombie processes
« Reply #3 on: May 05, 2019, 11:46:19 pm »
Code: Pascal  [Select]
  1.   OpenDocument('/home/me/Documents/text.txt');

works ok on my system.

xdg-open starts, then text editor opens, then xdg-open finishes. Everything is as intended.
Lazarus 2.0.6 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 19.10 XFCE

BeniBela

  • Hero Member
  • *****
  • Posts: 684
    • homepage
Re: openDocument and openURL leave zombie processes
« Reply #4 on: May 06, 2019, 12:00:22 am »
You need to kill the Night King. That gets rid of all the whitewalkers and zombies

fedkad

  • Full Member
  • ***
  • Posts: 118
Re: openDocument and openURL leave zombie processes
« Reply #5 on: May 06, 2019, 09:33:46 am »
Code: Pascal  [Select]
  1.   OpenDocument('/home/me/Documents/text.txt');

works ok on my system.
xdg-open starts, then text editor opens, then xdg-open finishes. Everything is as intended.

Please, test this: After the editor starts, but before closing your calling Lazarus program, do you have any zombie processes in the system?

Code: [Select]
$ ps -ef | grep -i defunct
mysuser    12903 12887  0 10:30 pts/1    00:00:00 [xdg-open] <defunct>

fedkad

  • Full Member
  • ***
  • Posts: 118
Re: openDocument and openURL leave zombie processes
« Reply #6 on: May 06, 2019, 09:40:30 am »
You need to kill the Night King. That gets rid of all the whitewalkers and zombies

There shouldn't be a need to kill the Night King (=my calling Lazarus program).
Just the Night King needs to listen the last words of its offsprings, and not leave them become whitewalkers.

To solve this, I added the following line to file components/lazutils/utf8process.pp, line 234 (at procedure RunCmdFromPath):

Code: Pascal  [Select]
  1. BrowserProcess.Options := BrowserProcess.Options + [poWaitOnExit];  //FEDON

However, I am not sure of other consequences that this "patch" may have.  :-\
« Last Edit: May 06, 2019, 09:54:37 am by fedkad »

sash

  • Sr. Member
  • ****
  • Posts: 275
Re: openDocument and openURL leave zombie processes
« Reply #7 on: May 06, 2019, 11:25:36 am »
After the editor starts, but before closing your calling Lazarus program, do you have any zombie processes in the system?

As I said before there's no defunct processes left: xdg-open finishes itself after associated text editor started. I watched this in htop.
But some googling reveals there are possible bugs in xdg-open itself: probably it depends on system configuration or launched process.
Lazarus 2.0.6 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 19.10 XFCE

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7504
Re: openDocument and openURL leave zombie processes
« Reply #8 on: May 06, 2019, 11:40:39 am »
Maybe that bug is something to do with console handles. Try adding poshell.

fedkad

  • Full Member
  • ***
  • Posts: 118
Re: openDocument and openURL leave zombie processes
« Reply #9 on: May 06, 2019, 04:28:38 pm »
Maybe that bug is something to do with console handles. Try adding poshell.
There is no poShell here:
Code: Pascal  [Select]
  1. Type
  2.   TProcessOption = (poRunSuspended,poWaitOnExit,
  3.                     poUsePipes,poStderrToOutPut,
  4.                     poNoConsole,poNewConsole,
  5.                     poDefaultErrorMode,poNewProcessGroup,
  6.                     poDebugProcess,poDebugOnlyThisProcess);    


As I said before there's no defunct processes left: xdg-open finishes itself after associated text editor started. I watched this in htop.
But some googling reveals there are possible bugs in xdg-open itself: probably it depends on system configuration or launched process.
Maybe your version of xdg-open is different. Mine is: xdg-open 1.1.3

sash

  • Sr. Member
  • ****
  • Posts: 275
Re: openDocument and openURL leave zombie processes
« Reply #10 on: May 10, 2019, 11:05:31 pm »
Maybe your version of xdg-open is different. Mine is: xdg-open 1.1.3

Mine is the same. And it looks like I have the same behavior of xdg-open (it seems like I didn't get you for the first time).
This is what I've got:
Defunct processes (or whatever it is) is shown by ps, but not in htop, until I close my program. Then all is cleaned up.

I have no idea (actually a time to dig deeper now) why.
Lazarus 2.0.6 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 19.10 XFCE

john horst

  • Jr. Member
  • **
  • Posts: 53
    • JHorst
Re: openDocument and openURL leave zombie processes
« Reply #11 on: May 11, 2019, 01:36:57 am »
I don't think it's a bug.. Your program is Spawning children and not waiting (I didn't take it as blocking / non blocking) just like the description of openDocument() says.

For instance, if you gdb -p $(pidof yourapp) then waitpid(pidof Zombie, 0, 0) it will clean up.
« Last Edit: May 11, 2019, 01:41:57 am by john horst »