trackma icon indicating copy to clipboard operation
trackma copied to clipboard

Launching player from trackma leaves zombie process on linux.

Open ahmubashshir opened this issue 3 years ago • 12 comments

Steps to reproduce

  • Open trackma.
  • Play something.
  • Close player.
  • Check for zombie process with zps -l
sh-5.0$ zps -l
PID   	PPID  	STATE	            NAME COMMAND
4097215	4088507	Z 	             vlc 
sh-5.0$ ps -p 4088507
    PID TTY          TIME CMD
4088507 pts/3    00:00:01 trackma-gtk

Cause

Player was started using subprocess.Popen(), but not calling .wait() or .poll() on Popen()ed process leaves a zombie.

Possible workarounds: [downsides may be caused by my implementation]
  • Double fork then os.execvp
    • Upside: launches the player as a child of init, so it is reaped as soon as the process ends.
    • Downside: leaves a fork()ed zombie process* * fixed after calling os.waitpid on first fork
  • subprocess.Popen().wait() in a thread
    • Upside: After launching, the thread waits for the child to terminate, then reaps the child.
    • Downside: Can't Close the UI unless palyer is closed too.
  • Ignore SIGCHLD

ahmubashshir avatar Aug 26 '20 19:08 ahmubashshir

I went with 3rd method because it was easier and cleaner.

ahmubashshir avatar Oct 14 '20 15:10 ahmubashshir

I found a bug which occurs if we ignore SIGCHLD. If parent exits while child is running, child exits too. So I used fork then waitpid; fork again in forked child then os.execv to prevent zombie process without relying on ignoring SIGCHLD. I'll create a new pr with this fix.

ahmubashshir avatar Oct 17 '20 18:10 ahmubashshir

Did some testing and using Popen the zombie process gets reaped within seconds of being closed. Apparently Python 3 should automatically reap zombie processes whenever they get garbage collected. What Python version are you using? Do you get a different behavior?

z411 avatar Nov 21 '20 19:11 z411

I'm using python 3.8.6, on manjaro.

By the way... they were never reaped :|. Whenever I tried to reap all zombies with zps, trackma was killed too, even after closing the player a few hours before running zps. And from this issue, subprocess only reaps previous process on new Popen

image On afeaef7ee034fe937019822c9f52b9e8ae84432f after closing vlc [closed on 01:22].

ahmubashshir avatar Nov 21 '20 19:11 ahmubashshir

Python 3.8.6 here and Arch Linux here. Weird since our configuration is similar, I tried with vlc and it gets reaped after around 5-10 seconds. This must be why I never noticed it. That said I guess we shouldn't have a zombie process in the first place at all, so I'll look into fixing this.

z411 avatar Nov 22 '20 06:11 z411

I tried with the ignore SIGCLD method again (after moving processes out of the engine) and it works perfectly for me. I don't get the bug you described and I don't get a zombie process either. Could you try my prevent-zombie branch?

z411 avatar Nov 22 '20 06:11 z411

Player dies as soon as trackma quits if sigchld is ignored.

On Sat, Nov 21, 2020 at 22:50, z411 [email protected] wrote:

I tried with the ignore SIGCLD method again (after moving processes out of the engine) and it works perfectly for me. I don't get the bug you described and I don't get a zombie process either. Could you try my the prevent-zombie branch?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/z411/trackma/issues/508#issuecomment-731707126, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF5WM3X5366CPYQKCWNOR5DSRCYCRANCNFSM4QME4NKQ.

ahmubashshir avatar Nov 22 '20 07:11 ahmubashshir

1 Peek 2020-11-22 13-05

ahmubashshir avatar Nov 22 '20 07:11 ahmubashshir

I seriously can't reproduce this...

https://streamable.com/3p345m

Could you try with a clean config, and ideally with the tracker disabled? I'll try to find other people to try and see if they can reproduce it.

z411 avatar Nov 22 '20 07:11 z411

Though I Started trackma with XDG_CONFIG_HOME=/tmp/ch XDG_DATA_HOME=/tmp/dd trackma-gtkand tracker was disabled, Vlc again closed on its own after closing trackma.

On Sat, Nov 21, 2020 at 23:28, z411 [email protected] wrote:

I seriously can't reproduce this...

https://streamable.com/3p345m

Could you try with a clean config, and ideally with the tracker disabled?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/z411/trackma/issues/508#issuecomment-731709950, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF5WM3XUN2QNPXOWGP44ECLSRC4RHANCNFSM4QME4NKQ.

ahmubashshir avatar Nov 22 '20 07:11 ahmubashshir

Running just python -m trackma.ui.cli play 2 should be enough to reproduce the second case, right? Because that simply opens mpv for me and does not kill it.

I can not reproduce the first case as well

Open trackma. Play something. Close player. Check for zombie process with zps -l

zps -l output is empty. On the prevent-zombie branch ofc, since I can't play anything on master due to #525.

Also Arch, btw.

FichteFoll avatar Nov 28 '20 12:11 FichteFoll

Running just python -m trackma.ui.cli play 2 should be enough to reproduce the second case, right? Because that simply opens mpv for me and does not kill it.

I can not reproduce the first case as well

Open trackma. Play something. Close player. Check for zombie process with zps -l

zps -l output is empty. On the prevent-zombie branch ofc, since I can't play anything on master due to #525.

Also Arch, btw.

python -m trackma.ui.cli play 2 quits after launching player, doesn't it? so child process is reparented to init(1) that's why zps -l is empty :3 btw... I confirmed now... This only happens in Gtk UI. https://streamable.com/l887xl

ahmubashshir avatar Nov 28 '20 18:11 ahmubashshir