Skip to content

Commit

Permalink
fix(python): leftover jsii-kernel-* directories in TMPDIR (#2100)
Browse files Browse the repository at this point in the history
The Python runtime did not properly signal shutdown to the `node`
process hosting the *jsii kernel*, causing it to be abruptly killed on
Python interpreter exit. This means the *jsii kernel*'s own clean-up
code was not allowed to run.

This adds an `atexit` hook which will properly close the `node` process'
`STDIN` file descriptor, effectively signaling it should shut down. This
then gives the process 5 seconds to clean up after itself and finish
before sending it `SIGTERM` (at which point it is given 5 seconds to
terminate after which it will be terminated).
  • Loading branch information
RomainMuller authored Oct 14, 2020
1 parent 29478ba commit c119994
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import datetime
import contextlib
import enum
Expand Down Expand Up @@ -275,16 +276,26 @@ def start(self):
stdout=subprocess.PIPE,
env=environ,
)

# Clean this process up at exit, so it terminates "gracefully"
atexit.register(self.stop)

self.handshake()

def stop(self):
# TODO: We can write an empty string here instead?
self._process.terminate()
# This process is closing already, un-registering the hook to not fire twice
atexit.unregister(self.stop)

# Close the process' STDIN, singalling we are done with it
self._process.stdin.close()

try:
self._process.wait(timeout=5)
except subprocess.TimeoutExpired:
self._process.kill()
try:
self._process.terminate(timeout=5)
except subprocess.TimeoutExpired:
self._process.kill()

self._ctx_stack.close()

Expand Down

0 comments on commit c119994

Please sign in to comment.