increase OOM kill-score for FFmpeg and mtp's;

discourage Linux from killing innocent processes
when FFmpeg decides to allocate 1 TiB of RAM
This commit is contained in:
ed 2024-01-07 17:52:10 +00:00
parent dee0950f74
commit dc8e621d7c
3 changed files with 17 additions and 6 deletions

View file

@ -118,7 +118,7 @@ def ffprobe(
b"--", b"--",
fsenc(abspath), fsenc(abspath),
] ]
rc, so, se = runcmd(cmd, timeout=timeout, nice=True) rc, so, se = runcmd(cmd, timeout=timeout, nice=True, oom=200)
retchk(rc, cmd, se) retchk(rc, cmd, se)
return parse_ffprobe(so) return parse_ffprobe(so)
@ -564,6 +564,7 @@ class MTag(object):
args = { args = {
"env": env, "env": env,
"nice": True, "nice": True,
"oom": 300,
"timeout": parser.timeout, "timeout": parser.timeout,
"kill": parser.kill, "kill": parser.kill,
"capture": parser.capture, "capture": parser.capture,

View file

@ -467,9 +467,9 @@ class ThumbSrv(object):
cmd += [fsenc(tpath)] cmd += [fsenc(tpath)]
self._run_ff(cmd, vn) self._run_ff(cmd, vn)
def _run_ff(self, cmd: list[bytes], vn: VFS) -> None: def _run_ff(self, cmd: list[bytes], vn: VFS, oom: int = 400) -> None:
# self.log((b" ".join(cmd)).decode("utf-8")) # self.log((b" ".join(cmd)).decode("utf-8"))
ret, _, serr = runcmd(cmd, timeout=vn.flags["convt"], nice=True) ret, _, serr = runcmd(cmd, timeout=vn.flags["convt"], nice=True, oom=oom)
if not ret: if not ret:
return return
@ -623,7 +623,7 @@ class ThumbSrv(object):
fsenc(tmp_opus) fsenc(tmp_opus)
] ]
# fmt: on # fmt: on
self._run_ff(cmd, vn) self._run_ff(cmd, vn, oom=300)
# iOS fails to play some "insufficiently complex" files # iOS fails to play some "insufficiently complex" files
# (average file shorter than 8 seconds), so of course we # (average file shorter than 8 seconds), so of course we
@ -647,7 +647,7 @@ class ThumbSrv(object):
fsenc(tpath) fsenc(tpath)
] ]
# fmt: on # fmt: on
self._run_ff(cmd, vn) self._run_ff(cmd, vn, oom=300)
elif want_caf: elif want_caf:
# simple remux should be safe # simple remux should be safe
@ -665,7 +665,7 @@ class ThumbSrv(object):
fsenc(tpath) fsenc(tpath)
] ]
# fmt: on # fmt: on
self._run_ff(cmd, vn) self._run_ff(cmd, vn, oom=300)
if tmp_opus != tpath: if tmp_opus != tpath:
try: try:

View file

@ -2557,6 +2557,7 @@ def runcmd(
argv: Union[list[bytes], list[str]], timeout: Optional[float] = None, **ka: Any argv: Union[list[bytes], list[str]], timeout: Optional[float] = None, **ka: Any
) -> tuple[int, str, str]: ) -> tuple[int, str, str]:
isbytes = isinstance(argv[0], (bytes, bytearray)) isbytes = isinstance(argv[0], (bytes, bytearray))
oom = ka.pop("oom", 0) # 0..1000
kill = ka.pop("kill", "t") # [t]ree [m]ain [n]one kill = ka.pop("kill", "t") # [t]ree [m]ain [n]one
capture = ka.pop("capture", 3) # 0=none 1=stdout 2=stderr 3=both capture = ka.pop("capture", 3) # 0=none 1=stdout 2=stderr 3=both
@ -2587,6 +2588,14 @@ def runcmd(
argv = [NICES] + argv argv = [NICES] + argv
p = sp.Popen(argv, stdout=cout, stderr=cerr, **ka) p = sp.Popen(argv, stdout=cout, stderr=cerr, **ka)
if oom and not ANYWIN and not MACOS:
try:
with open("/proc/%d/oom_score_adj" % (p.pid,), "wb") as f:
f.write(("%d\n" % (oom,)).encode("utf-8"))
except:
pass
if not timeout or PY2: if not timeout or PY2:
bout, berr = p.communicate(sin) bout, berr = p.communicate(sin)
else: else:
@ -2734,6 +2743,7 @@ def _parsehook(
sp_ka = { sp_ka = {
"env": env, "env": env,
"nice": True, "nice": True,
"oom": 300,
"timeout": tout, "timeout": tout,
"kill": kill, "kill": kill,
"capture": cap, "capture": cap,