Skip to content

[Engine] close the six handles the sampler and the HIP probe leak (Fix #4654) - #4791

Open
XiaoBinGan wants to merge 1 commit into
Niko1221:mainfrom
XiaoBinGan:fix/close-leaked-handles
Open

XiaoBinGan wants to merge 1 commit into
Niko1221:mainfrom
XiaoBinGan:fix/close-leaked-handles

Conversation

@XiaoBinGan

Copy link
Copy Markdown
Contributor

Problem and result

The Minimal serving runtime job prints the same ResourceWarning lines on every run (#4654). Six open() calls sat
inside an expression, so no close() ever ran:

  • serve/telemetry.py — the physical_package_id glob (one handle per CPU), /proc/cpuinfo, /proc/stat,
    /proc/meminfo
  • serve/server.py — the two properties files the HIP probe reads

Each now runs under a with statement (or through a one-line _read_lines() helper for the two properties reads).
No value, threshold or return path changes: the same figures are parsed from the same lines in the same order.

Related discussion

#4654 — the report with the exact warning lines and the two sites in server.py. A pytest run with
-W error::ResourceWarning on the CI job is what the report suggests; this PR removes the leaks it names and does not
change the job.

Approach

Only the ownership of the handles changes:

with open(f, encoding="utf-8") as fh:
    packages.add(fh.read().strip())
...
with open("/proc/cpuinfo", encoding="utf-8", errors="replace") as fh:
    for line in fh:
        if line.startswith("model name"):
            return line.split(":", 1)[1].strip()

and for the HIP probe:

def _read_lines(path: str):
    """The lines of `path`, with the file closed when the caller is done (the HIP properties probe reads two)."""
    with open(path) as fh:
        return fh.readlines()

The early return inside the cpuinfo loop is kept; the with closes the file on the way out, which is the bug the
report describes (_cpu_name() reports at the return, not at the open()).

Validation

Passed: python -m pytest serve/test_monitor.py -q on this checkout (Python 3.11.16, Windows): 10 passed, 8
subtests passed
— the module these helpers live in imports and its monitor tests are unchanged.

Passed: import serve.telemetry and import serve.server both succeed after the change, and a grep for an open(
still inside an expression in serve/telemetry.py now finds none.

Failed: none.

Not run: the /proc and /sys branches themselves cannot be exercised on this Windows checkout (they are Linux
paths), so the warnings the job reports are verified by reading the diff, not by reproducing them here. A Linux run with
-W error::ResourceWarning would settle it, and the issue's own job is the natural place.

Limits

  • No behaviour change is intended, which also means no performance claim: this is about closing handles.
  • The two properties reads are now one small helper rather than two identical comprehensions; if the reviewer prefers
    pathlib.Path(...).read_text(...) inline, that is a one-line change each.

…e open inside an expression

Six open() calls sat inside an expression, so nothing ever called close() on them and the Minimal serving
runtime job printed the same ResourceWarning lines on every run: the physical_package_id glob, /proc/cpuinfo,
/proc/stat and /proc/meminfo in serve/telemetry.py, and the two properties files the HIP probe reads in
serve/server.py.

Each now runs under a with statement: telemetry.py reads the cpuinfo, stat and meminfo files through one, and
server.py reads the two properties files through a small _read_lines() helper. No value, threshold or return
path changes; the figures are the same ones in the same order.
@XiaoBinGan
XiaoBinGan force-pushed the fix/close-leaked-handles branch from 36b5768 to 74c0f11 Compare October 31, 2026 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant