question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ungraceful shutdown of python script can leave orphan processes

See original GitHub issue

🐛 Bug

CompilerGym uses a client/service architecture. Every time a CompilerEnv object is created, a CompilerService subprocess is started. The lifetime of the subprocess is managed by the CompilerEnv. Calling CompilerEnv.close() terminates the service:

CompilerGym

If for some reason CompilerEnv.close() is not called (either through a system or user error), the CompilerService will not be killed and will remain dormant indefinitely.

To Reproduce

In one terminal, open a python interpreter and start a CompilerGym environment. Make a note of the interpreter and the environment’s service process IDs:

In [1]: import os

In [2]: os.getpid()
Out[2]: 5425

In [3]: import compiler_gym

In [4]: env = compiler_gym.make("llvm-v0")

In [5]: env.service.connection.process
Out[5]: <subprocess.Popen at 0x7fbe10855790>

In [6]: env.service.connection.process.pid
Out[6]: 5809

In another terminal, kill the interpreter process, and observe that the CompilerGym environment’s service is still running:

$ kill -9 5425

$ ps aux | grep 5809
cummins           6087   0.0  0.0  4408696    864 s002  S+    1:16PM   0:00.00 grep --color=auto 5809
cummins           5809   0.0  0.0  4499680  14628 s000  S     1:15PM   0:00.02 ./compiler_gym-llvm-service --working_dir=/Users/cummins/.cache/compiler_gym/s/0720T131545-167414-6660

That process will remain dormant until explicitly killed, or the machine is rebooted.

Expected behavior

After some period of inactivity, the service should realize that it is no longer being used and should gracefully shutdown.

To the best of my understanding, it is not possible to guarantee that a subprocess shutdown routine can be called by the parent process, so the proposed workaround is to have a ‘time to live’ timer on each subprocess which will shut itself down if that period of inactivity is reached.

Workaround

If you suspect that there are dormant LLVM CompilerGym services and you are not currently running any CompilerGym python scripts, you can manually kill them using:

ps aux | grep compiler_gym-llvm-service | grep -v grep | awk '{print $2}' | xargs --no-run-if-empty kill

although this does not tidy up any temporary cache files that the environments have created.

Environment

Please fill in this checklist:

  • CompilerGym: v0.1.9
  • How you installed CompilerGym (conda, pip, source): n/a
  • OS: n/a
  • Python version: n/a

Additional context

See the documentation for more background on CompilerGym’s client/service architecture.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ChrisCumminscommented, May 10, 2022

How about the orphan executables like these?

Yes, those are safe to kill. Those processes are cBench binaries which are used to compute runtime. If they refuse to complete within a specified timeout (default 5 min), the process will be abandoned. Adding proper subprocess timeout would be a nice feature. The code is here:

https://github.com/facebookresearch/CompilerGym/blob/development/compiler_gym/util/Subprocess.cc#L62-L66

Cheers, Chris

1reaction
udusecommented, Feb 7, 2022

I noticed this problem when I implemented an algorithm that env.fork() a lot and sometimes I forgot to env.close() or the program is interrupted. What I really want to address this problem (indirectly) is to have a context manager for compiler environment class.

e.g.,

with compiler_gym.make("llvm-v0") as env:
    # do something here
    with env.fork() as fork:
        # do more things here

contextlib.closing can partly do this job already, but it would be nice to have native support. If we have such support, it should be the recommended way of managing compiler gym environments, like with open(...) as ... is THE way of opening files in Python.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow a custom "stop-command" to manage graceful shutdown ...
I setup a Python script on Jenkins to check for draining targets on a LB. This allows me to see the instances/dockers that...
Read more >
Orphaned Processes in Python
An orphan process is created when the parent of a child process is terminated. This can happen for many reasons, both intentional and...
Read more >
Clean shutdown of a Python script - Stack Overflow
It seems to me that your are trying to implement a daemon. There is various references about daemon implementations in python :.
Read more >
Untitled
Cod2 key code, Neutral and ground wires, Kasai procedure biliary atresia wiki, ... A-one 31277 word, Cesba queretaro calendario, Left behind 2 cast, ......
Read more >
Automatically Detecting Missing Cleanup for Ungraceful Exits
Nginx will exit ungracefully because the children processes are not cleaned up and thus become orphan processes. As a result, users.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found