Skip to content

Studio: find the backend's child processes from /proc on Linux for /api/system - #15592

Merged
danielhanchen merged 5 commits into
mainfrom
studio-system-process-tree
Oct 28, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio-system-process-tree

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

/api/system reports the backend's process-tree memory, and to find the backend's child processes it asks psutil, which reads every process on the host. On a busy machine that is tens to hundreds of milliseconds per call, and the floating monitor polls this route every 5 seconds for as long as Studio is open.

What changes

  • New studio/backend/utils/process_tree.py, linux_descendant_pids(pid): on Linux, walks /proc/<pid>/task/<tid>/children for every thread of every process in the tree (so a child started from a non-main thread is found) and skips a descendant process that has exited. It returns None, and the existing psutil code answers, when the platform is not Linux, the kernel has no children files, the root process is gone, a thread vanishes while its children file is read (an exiting thread reparents its live children to a sibling thread that may already have been read, so the walk could miss them), or anything else fails (permissions included).
  • main.py _process_tree_rss_mb uses the walk when the process has an integer pid; otherwise it runs the psutil code unchanged.

Results

  • Live backend (about 8,500 processes on the host): the process-tree memory step goes from 86.5 ms to 0.39 ms per call.
  • Synthetic tree: psutil 118.9 ms, the walk 0.07 ms, identical pid and parent-pid sets.

Same answer:

  • A backend with real children (a spawn worker with its own child, a nested Python, a shell with background jobs, a child started from a non-main thread; 8 descendants): all 20 samples gave identical child sets and identical memory totals with the walk and with psutil.
  • An idle backend: 206 samples over 150 s, no mismatches.

Tests

  • New studio/backend/tests/test_process_tree_fast_path.py (7): the walk finds what psutil finds on a real process tree, other platforms defer to psutil, a kernel without children files defers, a vanished root defers, a descendant exiting mid-walk is skipped, a thread vanishing mid-walk defers to psutil, and _process_tree_rss_mb uses the walk and matches the forced psutil fallback.
  • The 16 test files that reference /api/system or _process_tree_rss_mb, plus the new file, on this branch rebased on current main: 1,152 passed, 103 skipped.
  • Staging CI on ubuntu-latest, windows-latest and macos-15 (Apple Silicon): Ubuntu ran the /proc tests (4 passed, 2 skipped); macOS and Windows skip the five Linux-only tests and pass the remaining one, since they keep the psutil walk.

Not covered

  • macOS and Windows keep psutil (no /proc); only the Linux path is faster.

danielhanchen added a commit that referenced this pull request Oct 28, 2026
…, and name any test that still does (#15626)

## Summary

Backend CI (`(Python 3.13, a-f)`) and `Repo tests (CPU, rest)` have been
failing intermittently with dozens of errors like

```
AttributeError: torch._dynamo.config.recompile_limit does not exist
AttributeError: torch._dynamo.config.suppress_errors does not exist
AttributeError: torch._inductor.config.emulate_precision_casts does not exist
```

Seen on #15586, #15589, #15590, #15592, #15593, #15596 and #15601.
Locally, on pytest 9.1.1, it never reproduced.

### Root cause

CI resolves pytest 9.2.0, which changed `MonkeyPatch.setattr`
(pytest-dev/pytest#10644).
- For a non-class target it now records the old value from the instance
`__dict__`. Before, it used `getattr`.
- A torch `ConfigModule` (`torch._dynamo.config`,
`torch._inductor.config`, and its `SubConfigProxy` children such as
`torch._inductor.config.triton`) serves its entries from `__getattr__`.
They are never in `__dict__`, so pytest records "not set".
- On undo it therefore calls `delattr`. `ConfigModule.__delattr__` marks
the entry hidden, and every later read in that process raises `... does
not exist`.

So every `monkeypatch.setattr(torch._dynamo.config, ...)` in the suite
now deletes the entry it patched. Which shard fails depends on which
xdist worker such a test lands on.

Minimal reproduction:

```python
def test_patch(monkeypatch):
    monkeypatch.setattr(torch._dynamo.config, "suppress_errors", True)

def test_after():
    assert torch._dynamo.config.suppress_errors is False
```

On pytest 9.1.1 both pass. On 9.2.0 `test_after` fails with
`torch._dynamo.config.suppress_errors does not exist`.

## Change

`tests/_shared/torch_config_guard.py`, installed from
`tests/conftest.py` and `studio/backend/tests/conftest.py`:
- **`install_monkeypatch_fix()`:** wraps `pytest.MonkeyPatch.setattr`.
When the target is a torch `ConfigModule` or `SubConfigProxy`, including
the dotted-string form, it records the real value that was replaced, so
`undo()` restores it with `setattr` instead of deleting the entry. Every
other target goes through pytest unchanged. The fix is a no-op until
torch is imported, and it never imports torch itself.
- **`_no_deleted_torch_config_entries` autouse fixture:** any test that
still leaves a config entry deleted, by any route, errors at its own
teardown with the stack of the delete. The entry is then restored with
its previous value, so the rest of the worker is not poisoned. Cost is
about 24 us per test.

The guard is what found the cause. On the first push (guard only),
staging CI named 15 deletes, and every recorded stack ended in
`_pytest/monkeypatch.py, line 437, in undo: delattr(obj, name)`.

## Testing

**Under pytest 9.2.0** (CI's version):
- `tests/test_torch_config_guard.py`: 10 passed. Two tests pin the fix:
- `test_monkeypatch_undo_restores_a_config_entry_instead_of_deleting_it`
covers `torch._dynamo.config`, `torch._inductor.config` and
`torch._inductor.config.triton`.
- `test_monkeypatch_undo_restores_a_config_entry_named_by_import_path`
covers the dotted-string form.
- **Sabotage:** with `install_monkeypatch_fix` turned into a no-op,
those 4 cases fail and the guard errors at teardown.
- `tests/test_block_compile.py`: 13 failed on main (`suppress_errors` /
`recompile_limit does not exist`, the CI signature), 42 passed with this
change.

**Under pytest 9.1.1:**
- `tests/test_torch_config_guard.py`: 10 passed, and
`tests/test_block_compile.py`: 42 passed, on both trees.
- `studio/backend/tests/test_diffusion_*.py` (about 17,400 tests, run in
parallel with main on one loaded host): the guard fired 0 times. The
branch had 67 failed / 14 errors against 79 / 26 on main. The 3 failures
seen only on the branch pass when rerun alone, so they are load flakes.
- Meta-tests that read conftests and `tests/_shared`, plus
`test_ci_backend_pytest_shards.py`: 604 passed.
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-28T06:31:06.458406Z 5b5520a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 593b1e2e6e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen marked this pull request as ready for review October 28, 2026 06:30
@danielhanchen
danielhanchen merged commit 7b81949 into main Oct 28, 2026
16 of 65 checks passed
@danielhanchen
danielhanchen deleted the studio-system-process-tree branch October 28, 2026 08:13
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