openDocument and openURL leave zombie processes lazarus / 32-Linux ---------------------------------------------------------------------- fedkad · 2019-05-02 16:50 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] myuser    17809 17616  0 19:39 pts/2    00:00:00 [xdg-open] 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 · 2019-05-02 17:57 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 · 2019-05-03 11:25 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i BrowserProcess.CommandLine := '/usr/bin/xdg-open ''/home/myuser/Documents/mydocument.txt''' Then it calls: 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 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). ---------------------------------------------------------------------- sash · 2019-05-05 21:46 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   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. ---------------------------------------------------------------------- BeniBela · 2019-05-05 22:00 You need to kill the Night King. That gets rid of all the whitewalkers and zombies ---------------------------------------------------------------------- fedkad · 2019-05-06 07:33 Quote from: sash on May 05, 2019, 11:46:19 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   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] ---------------------------------------------------------------------- fedkad · 2019-05-06 07:40 Quote from: BeniBela on May 06, 2019, 12:00:22 amYou 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i BrowserProcess.Options := BrowserProcess.Options + [poWaitOnExit];  //FEDON However, I am not sure of other consequences that this "patch" may have. ---------------------------------------------------------------------- sash · 2019-05-06 09:25 Quote from: fedkad on May 06, 2019, 09:33:46 amAfter 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. ---------------------------------------------------------------------- marcov · 2019-05-06 09:40 Maybe that bug is something to do with console handles. Try adding poshell. ---------------------------------------------------------------------- fedkad · 2019-05-06 14:28 Quote from: marcov on May 06, 2019, 11:40:39 amMaybe that bug is something to do with console handles. Try adding poshell. There is no poShell here: 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 Type  TProcessOption = (poRunSuspended,poWaitOnExit,                    poUsePipes,poStderrToOutPut,                    poNoConsole,poNewConsole,                    poDefaultErrorMode,poNewProcessGroup,                    poDebugProcess,poDebugOnlyThisProcess);     Quote from: sash on May 06, 2019, 11:25:36 amAs 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 · 2019-05-10 21:05 Quote from: fedkad on May 06, 2019, 04:28:38 pmMaybe 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. ---------------------------------------------------------------------- john horst · 2019-05-10 23:36 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.