From e988573cb4a1afc1dec83bac254c562264a7589f Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Mon, 8 Jun 2020 13:53:27 +0200 Subject: [PATCH] hard exit when memory is more than 300MB --- app/pgp_utils.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/pgp_utils.py b/app/pgp_utils.py index 45497988..eb3c0e8a 100644 --- a/app/pgp_utils.py +++ b/app/pgp_utils.py @@ -1,3 +1,4 @@ +import os from io import BytesIO import gnupg @@ -24,11 +25,22 @@ def load_public_key(public_key: str) -> str: raise PGPException("Cannot load key") from e +def hard_exit(): + pid = os.getpid() + LOG.warning("kill pid %s", pid) + os.kill(pid, 9) + + def encrypt_file(data: BytesIO, fingerprint: str) -> str: LOG.d("encrypt for %s", fingerprint) - mem_usage = memory_usage(-1, interval=1, timeout=1) + mem_usage = memory_usage(-1, interval=1, timeout=1)[0] LOG.d("mem_usage %s", mem_usage) + # todo + if mem_usage > 300: + LOG.error("Force exit") + hard_exit() + r = gpg.encrypt_file(data, fingerprint, always_trust=True) if not r.ok: LOG.error("Try encrypt again %s", fingerprint)