Skip to content

Avoid shell in async command runner#1225

Open
NguyenCong2k wants to merge 2 commits into
bentoml:mainfrom
NguyenCong2k:fix-async-run-command-shell
Open

Avoid shell in async command runner#1225
NguyenCong2k wants to merge 2 commits into
bentoml:mainfrom
NguyenCong2k:fix-async-run-command-shell

Conversation

@NguyenCong2k
Copy link
Copy Markdown

Summary

  • replace asyncio.create_subprocess_shell() with asyncio.create_subprocess_exec() in async_run_command()
  • keep command arguments as separate argv entries instead of joining them into a shell string
  • avoid sending SIGINT after the subprocess has already exited
  • add regression coverage for shell metacharacters in command arguments

Test

$env:PYTHONPATH='src'
python -m pytest tests\test_common.py -q

Copilot AI review requested due to automatic review settings May 11, 2026 03:28
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens openllm.common.async_run_command() by avoiding shell invocation when running async subprocesses, reducing the risk of shell metacharacters being interpreted and improving cleanup behavior.

Changes:

  • Switch from asyncio.create_subprocess_shell() to asyncio.create_subprocess_exec() and pass argv entries directly.
  • Avoid sending SIGINT to the subprocess if it has already exited (via returncode check).
  • Add a regression test to ensure shell metacharacters in arguments don’t trigger command chaining.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/openllm/common.py Uses create_subprocess_exec(*cmd) and adds a returncode guard before sending SIGINT during teardown.
tests/test_common.py Adds regression coverage to ensure async_run_command() doesn’t interpret shell metacharacters as command separators.
Comments suppressed due to low confidence (1)

src/openllm/common.py:440

  • Even with the returncode check, there is still a race where the process can exit but returncode hasn't been updated yet; send_signal() can raise ProcessLookupError in that window. To avoid intermittent failures during context manager teardown, catch/ignore ProcessLookupError (or similar) around proc.send_signal(signal.SIGINT).


def md5(*strings: str) -> int:
  m = hashlib.md5()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_common.py
Comment on lines +3 to +14

from openllm.common import async_run_command


def test_async_run_command_does_not_invoke_shell(tmp_path):
marker = tmp_path / 'shell_injection_marker.txt'
if os.name == 'nt':
separator = '&'
payload = ['cmd', '/c', f'echo PWNED>{marker}']
else:
separator = ';'
payload = ['sh', '-c', f'echo PWNED > {marker}']
Comment thread tests/test_common.py
async def run() -> None:
cmd = ['python', '-c', 'pass', separator, *payload]
async with async_run_command(cmd, cwd=str(tmp_path), silent=True) as proc:
await proc.wait()
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.

2 participants