Sublime Forum

EventListener.on_exit() not called on SIGTERM

#1

Problem: I want to execute a function before ST process terminates. I have successfully used EventListener.on_exit(), but that is not called if the process is terminated via SIGTERM signal (I’m on Linux).

In order to do so I’ve tried to both register a signal handler and to use atexit stdlib module, but neither worked. Any advice?

Thanks in advance!

import atexit
import signal
import sublime_plugin


def cleanup():                                   # <-- never called
    ...


class InMemoryDBOnExit(sublime_plugin.EventListener):
    def on_exit(self):                           # <-- never called
        cleanup()


def sigterm_handler(signum, frame):
    cleanup()


atexit.register(cleanup)                          # <-- doesn't work
signal.signal(signal.SIGTERM, sigterm_handler)    # <-- doesn't work
0 Likes

#2

Works on my side. I can’t find it on docs now but iirc, you can’t use any ST API in on_exit if that matters.

from pathlib import Path
import sublime_plugin
import time


class TestOnExit(sublime_plugin.EventListener):
    def on_exit(self) -> None:
        (Path.home() / "Desktop/hello.txt").write_text(time.ctime())
0 Likes

#3

Thanks. My fault: I was using a script which SIGTERMinated the parent sublime process + all of its children (including plugin_host-3.8). This way on_exit is not executed. If I use pkill subl though, the exit function is executed, even though with a considerable delay… like 8 secs.

This introduces another problem though, because I have to take that into account if I restart ST in the meantime.

I can’t find it on docs now but iirc, you can’t use any ST API in on_exit if that matters.

It didn’t matter but it’s definitively good to know… because I didn’t know =).
Thanks.

0 Likes