Prozessnamen mit abgeschnittenem UTF-8 tolerant lesen

Der Kernel kürzt comm byteweise auf 15 Bytes; ein halber Umlaut löste
UnicodeDecodeError aus und beendete den Dienst.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main
AmiPoke 1 month ago
parent 66587b1872
commit d6fa6d1542
  1. 5
      src/ramscreen/meminfo.py
  2. 10
      tests/test_meminfo.py

@ -63,12 +63,13 @@ def _app_name(pid_dir: Path) -> str:
return name return name
except OSError: except OSError:
pass pass
return (pid_dir / "comm").read_text().strip() # errors="replace": der Kernel kürzt comm byteweise, auch mitten in einem UTF-8-Zeichen
return (pid_dir / "comm").read_text(errors="replace").strip()
def _field_kb(path: Path, field: str) -> int | None: def _field_kb(path: Path, field: str) -> int | None:
try: try:
for line in path.read_text().splitlines(): for line in path.read_text(errors="replace").splitlines():
if line.startswith(field): if line.startswith(field):
return int(line.split()[1]) return int(line.split()[1])
except OSError: except OSError:

@ -103,6 +103,16 @@ def test_top_apps_versionsnummer_als_dateiname_nutzt_comm(tmp_path: Path):
assert top_apps(5, tmp_path) == [App("claude", 500 * 1024)] assert top_apps(5, tmp_path) == [App("claude", 500 * 1024)]
def test_top_apps_abgeschnittenes_utf8_in_comm_und_status(tmp_path: Path):
# Der Kernel kürzt comm byteweise auf 15 Bytes – auch mitten in einem Umlaut
make_proc(tmp_path, 1, exe=None, comm=None, rss_kb=100)
(tmp_path / "1" / "comm").write_bytes(b"abcdefghijklmn\xc3\n")
(tmp_path / "1" / "status").write_bytes(b"Name:\tabcdefghijklmn\xc3\nVmRSS:\t100 kB\n")
apps = top_apps(5, tmp_path)
assert [a.size for a in apps] == [100 * 1024]
assert apps[0].name.startswith("abcdefghijklmn")
def test_top_apps_ueberspringt_kernel_threads_und_verschwundene(tmp_path: Path): def test_top_apps_ueberspringt_kernel_threads_und_verschwundene(tmp_path: Path):
# Kernel-Thread: kein exe, leeres smaps_rollup, kein VmRSS # Kernel-Thread: kein exe, leeres smaps_rollup, kein VmRSS
make_proc(tmp_path, 2, exe=None, comm="kworker/0:1", smaps_text="") make_proc(tmp_path, 2, exe=None, comm="kworker/0:1", smaps_text="")

Loading…
Cancel
Save