Skip to content

Conversation

@samlurye
Copy link
Contributor

@samlurye samlurye commented Dec 5, 2025

Stack from ghstack (oldest at bottom):

NB: this is mostly vibe-coded since I'm not really familiar with pytest's internals.

This diff adds a new pytest marker pytest.mark.spawn_isolate(timeout=..., env=...) that provides similar functionality as the pytest-isolate package, except that it launches subprocesses by spawning them rather than forking (since the latter is incompatible with torch). Output from the child process is continuously piped back to the parent process so that if the child exits unexpectedly, we can still observe what happened. Child output is marked clearly, surrounded by orange headers/footers that look like:

vvvvvvv spawn_isolate FAIL for {test name} (child output) vvvvvvv

{child output}

^^^^^ end spawn_isolate FAIL for {test name} (child output) ^^^^^

The contents of {child output} are colored tan where it otherwise would have been white to easily distinguish where the child output starts and stops, but it maintains any explicit coloration set by pytest.

There are some tests where this marker is necessary -- for example, test_debugger.py requires each test to be isolated because each test needs to independently reconfigure/make assertions about the global debug controller actor.

However, I would further argue that this marker should be used for most or all of our tests. For better or worse, monarch makes substantial use of global state, and it is too easy for one test to interfere with another test in the same process. For example, a test failure could put a global actor into a bad/failed state in a way that is consistent with the intended behavior of the system; however, other tests in the same process that try to use this global actor would then fail, which just adds noise to an already difficult debugging experience.

Moreover, running each test in its own subprocess will make it much easier to ensure that resources aren't leaked across tests.

Differential Revision: D88395512

NB: this is mostly vibe-coded since I'm not really familiar with pytest's internals.

This diff adds a new pytest marker `pytest.mark.spawn_isolate(timeout=..., env=...)` that provides similar functionality as the `pytest-isolate` package, except that it launches subprocesses by spawning them rather than forking (since the latter is incompatible with torch). Output from the child process is continuously piped back to the parent process so that if the child exits unexpectedly, we can still observe what happened. Child output is marked clearly, surrounded by orange headers/footers that look like:
```
vvvvvvv spawn_isolate FAIL for {test name} (child output) vvvvvvv

{child output}

^^^^^ end spawn_isolate FAIL for {test name} (child output) ^^^^^
```
The contents of `{child output}` are colored tan where it otherwise would have been white to easily distinguish where the child output starts and stops, but it maintains any explicit coloration set by pytest.

There are some tests where this marker is necessary -- for example, `test_debugger.py` requires each test to be isolated because each test needs to independently reconfigure/make assertions about the global debug controller actor.

However, I would further argue that this marker should be used for most or all of our tests. For better or worse, monarch makes substantial use of global state, and it is too easy for one test to interfere with another test in the same process. For example, a test failure could put a global actor into a bad/failed state in a way that is consistent with the intended behavior of the system; however, other tests in the same process that try to use this global actor would then fail, which just adds noise to an already difficult debugging experience.

Moreover, running each test in its own subprocess will make it much easier to ensure that resources aren't leaked across tests.

Differential Revision: [D88395512](https://our.internmc.facebook.com/intern/diff/D88395512/)

[ghstack-poisoned]
samlurye added a commit that referenced this pull request Dec 5, 2025
NB: this is mostly vibe-coded since I'm not really familiar with pytest's internals.

This diff adds a new pytest marker `pytest.mark.spawn_isolate(timeout=..., env=...)` that provides similar functionality as the `pytest-isolate` package, except that it launches subprocesses by spawning them rather than forking (since the latter is incompatible with torch). Output from the child process is continuously piped back to the parent process so that if the child exits unexpectedly, we can still observe what happened. Child output is marked clearly, surrounded by orange headers/footers that look like:
```
vvvvvvv spawn_isolate FAIL for {test name} (child output) vvvvvvv

{child output}

^^^^^ end spawn_isolate FAIL for {test name} (child output) ^^^^^
```
The contents of `{child output}` are colored tan where it otherwise would have been white to easily distinguish where the child output starts and stops, but it maintains any explicit coloration set by pytest.

There are some tests where this marker is necessary -- for example, `test_debugger.py` requires each test to be isolated because each test needs to independently reconfigure/make assertions about the global debug controller actor.

However, I would further argue that this marker should be used for most or all of our tests. For better or worse, monarch makes substantial use of global state, and it is too easy for one test to interfere with another test in the same process. For example, a test failure could put a global actor into a bad/failed state in a way that is consistent with the intended behavior of the system; however, other tests in the same process that try to use this global actor would then fail, which just adds noise to an already difficult debugging experience.

Moreover, running each test in its own subprocess will make it much easier to ensure that resources aren't leaked across tests.

Differential Revision: [D88395512](https://our.internmc.facebook.com/intern/diff/D88395512/)

ghstack-source-id: 327474510
Pull Request resolved: #2076
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants