-
-
Notifications
You must be signed in to change notification settings - Fork 16.6k
Revert "[Bugfix] Fix spawn_new_process_for_each_test silently swallowing test failures" (#41423) #41887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+245
−306
Closed
Revert "[Bugfix] Fix spawn_new_process_for_each_test silently swallowing test failures" (#41423) #41887
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| import time | ||
| import warnings | ||
| from collections.abc import Callable, Iterable, Sequence | ||
| from contextlib import ExitStack, contextmanager | ||
| from contextlib import ExitStack, contextmanager, suppress | ||
| from multiprocessing import Process | ||
| from pathlib import Path | ||
| from typing import Any, Literal | ||
|
|
@@ -1512,65 +1512,52 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None: | |
|
|
||
|
|
||
| def spawn_new_process_for_each_test(f: Callable[_P, None]) -> Callable[_P, None]: | ||
| """Decorator to spawn a new process for each test function. | ||
|
|
||
| Uses subprocess with cloudpickle to serialize the test function and | ||
| propagates exceptions back to the parent, so test failures are never | ||
| silently swallowed (fixes https://github.com/vllm-project/vllm/issues/41415). | ||
| """ | ||
| """Decorator to spawn a new process for each test function.""" | ||
|
|
||
| @functools.wraps(f) | ||
| def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None: | ||
| with tempfile.NamedTemporaryFile(delete=False, suffix=".tb", mode="wb") as tmp: | ||
| tb_file = tmp.name | ||
| # Check if we're already in a subprocess | ||
| if os.environ.get("RUNNING_IN_SUBPROCESS") == "1": | ||
| # If we are, just run the function directly | ||
| return f(*args, **kwargs) | ||
|
|
||
| try: | ||
| # Serialize the function + args with cloudpickle so closures work | ||
| payload = cloudpickle.dumps((f, args, kwargs, tb_file)) | ||
|
|
||
| child_script = ( | ||
| "import sys, cloudpickle, traceback\n" | ||
| "try:\n" | ||
| " from _pytest.outcomes import Skipped\n" | ||
| "except ImportError:\n" | ||
| " class Skipped(BaseException): pass\n" | ||
| "f, args, kwargs, tb_file = " | ||
| "cloudpickle.loads(sys.stdin.buffer.read())\n" | ||
| "try:\n" | ||
| " f(*args, **kwargs)\n" | ||
| "except Skipped:\n" | ||
| " sys.exit(0)\n" | ||
| "except BaseException:\n" | ||
| " open(tb_file, 'w').write(traceback.format_exc())\n" | ||
| " sys.exit(1)\n" | ||
| ) | ||
| import torch.multiprocessing as mp | ||
|
|
||
| with suppress(RuntimeError): | ||
| mp.set_start_method("spawn") | ||
|
|
||
| # Get the module | ||
| module_name = f.__module__ | ||
|
|
||
| # Create a process with environment variable set | ||
| env = os.environ.copy() | ||
| env["RUNNING_IN_SUBPROCESS"] = "1" | ||
|
|
||
| with tempfile.TemporaryDirectory() as tempdir: | ||
| output_filepath = os.path.join(tempdir, "new_process.tmp") | ||
|
|
||
| # `cloudpickle` allows pickling complex functions directly | ||
| input_bytes = cloudpickle.dumps((f, output_filepath)) | ||
|
|
||
| repo_root = str(VLLM_PATH.resolve()) | ||
| env = os.environ.copy() | ||
|
|
||
| env = dict(env or os.environ) | ||
| env["PYTHONPATH"] = repo_root + os.pathsep + env.get("PYTHONPATH", "") | ||
|
|
||
| result = subprocess.run( | ||
| [sys.executable, "-c", child_script], | ||
| input=payload, | ||
| capture_output=True, | ||
| env=env, | ||
| cmd = [sys.executable, "-m", f"{module_name}"] | ||
|
|
||
| returned = subprocess.run( | ||
| cmd, input=input_bytes, capture_output=True, env=env | ||
| ) | ||
|
Comment on lines
+1540
to
1551
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation of
|
||
|
|
||
| if result.returncode != 0: | ||
| # Read traceback written by child, fall back to stderr | ||
| tb = "" | ||
| if os.path.exists(tb_file) and os.path.getsize(tb_file) > 0: | ||
| with open(tb_file) as fp: | ||
| tb = fp.read() | ||
| else: | ||
| tb = result.stderr.decode() | ||
| # check if the subprocess is successful | ||
| try: | ||
| returned.check_returncode() | ||
| except Exception as e: | ||
| # wrap raised exception to provide more information | ||
| raise RuntimeError( | ||
| f"Test subprocess '{f.__name__}' failed " | ||
| f"(exit code {result.returncode}):\n{tb}" | ||
| ) | ||
| finally: | ||
| with contextlib.suppress(OSError): | ||
| os.remove(tb_file) | ||
| f"Error raised in subprocess:\n{returned.stderr.decode()}" | ||
| ) from e | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The decorator does not correctly handle
asynctest functions. Iffis an asynchronous function, the callf(*args, **kwargs)returns a coroutine that must be awaited. Without an event loop to run the coroutine in the subprocess, the test body will never execute. This is a critical issue for tests liketest_custom_logitsprocsintests/v1/logits_processors/test_custom_online.py, which have been converted toasyncin this PR.