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

Closed
opened 2026-11-07 16:33:16 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4791
Author: @XiaoBinGan
Created: 10/31/2026
Status: ❌ Closed

Base: main ← Head: fix/close-leaked-handles


📝 Commits (1)

  • 74c0f11 Fix #4654: close the six handles the sampler and the HIP probe open inside an expression

📊 Changes

2 files changed (+18 additions, -8 deletions)

View changed files

📝 serve/server.py (+8 -2)
📝 serve/telemetry.py (+10 -6)

📄 Description

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.

#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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4791 **Author:** [@XiaoBinGan](https://github.com/XiaoBinGan) **Created:** 10/31/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/close-leaked-handles` --- ### 📝 Commits (1) - [`74c0f11`](https://github.com/Niko1221/Strata/commit/74c0f11014018a8d5729aa9ee9d0cd5e1fcfd1ed) Fix #4654: close the six handles the sampler and the HIP probe open inside an expression ### 📊 Changes **2 files changed** (+18 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `serve/server.py` (+8 -2) 📝 `serve/telemetry.py` (+10 -6) </details> ### 📄 Description ## 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: ```python 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: ```python 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 16:33:16 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5181
No description provided.