-
Notifications
You must be signed in to change notification settings - Fork 174
fix(BA-6062): Wrap legacy string based start_command via shell -c instead of shlex.split
#11648
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
Open
seedspirit
wants to merge
3
commits into
main
Choose a base branch
from
fix/BA-6062
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−70
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Wrap legacy string `start_command` in model definitions as `[shell, '-c', value]` to preserve shell semantics, with a data migration that repairs previously-broken single-token argv rows |
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
78 changes: 78 additions & 0 deletions
78
...d/manager/models/alembic/versions/70ffcaaa5f5c_rewrap_legacy_string_start_command_via_.py
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """rewrap legacy string start_command via shell -c | ||
|
|
||
| Migration ``8c1f7d3a9e2b`` (Part of: 26.5.0) wrapped any legacy ``str`` | ||
| ``start_command`` as a one-item argv list (e.g. ``"python service.py"`` | ||
| became ``["python service.py"]``). That value is meaningless at exec | ||
| time -- ``execve`` would look for a binary literally named | ||
| ``"python service.py"`` -- and it loses the shell semantics the user | ||
| originally intended. | ||
|
|
||
| This migration repairs those rows by replacing single-element argv | ||
| lists whose only token still looks like a shell command (contains | ||
| whitespace) with ``[shell, "-c", token]``, where ``shell`` comes from | ||
| the sibling ``service.shell`` field and falls back to ``/bin/bash`` to | ||
| match ``ai.backend.common.config.DEFAULT_SHELL``. Multi-token argv | ||
| lists and single-token lists without whitespace are left untouched, | ||
| so the migration is safe to re-apply. | ||
|
|
||
| Revision ID: 70ffcaaa5f5c | ||
| Revises: ba42cb865efe | ||
| Create Date: 2026-05-18 | ||
|
|
||
| """ | ||
|
|
||
| import json | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from sqlalchemy.engine import Connection | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "70ffcaaa5f5c" | ||
| down_revision = "ba42cb865efe" | ||
| # Part of: 26.5.1 | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| DEFAULT_SHELL = "/bin/bash" | ||
|
|
||
|
|
||
| def _rewrap_model_definition(conn: Connection, table: str, column: str) -> None: | ||
| rows = conn.execute( | ||
| sa.text(f"SELECT id, {column} FROM {table} WHERE {column} IS NOT NULL") | ||
| ).fetchall() | ||
|
|
||
| for row_id, model_definition in rows: | ||
| changed = False | ||
| for model in model_definition.get("models") or []: | ||
| service = model.get("service") or {} | ||
| start_command = service.get("start_command") | ||
| if ( | ||
| isinstance(start_command, list) | ||
| and len(start_command) == 1 | ||
| and " " in start_command[0] | ||
|
seedspirit marked this conversation as resolved.
seedspirit marked this conversation as resolved.
|
||
| ): | ||
| # Case when the start_command is a single string that looks like a shell command. | ||
| # e.g. ["python service.py"] -> ["/bin/bash", "-c", "python service.py"] | ||
| shell = service.get("shell") or DEFAULT_SHELL | ||
| service["start_command"] = [shell, "-c", start_command[0]] | ||
| changed = True | ||
|
|
||
| if changed: | ||
| conn.execute( | ||
| sa.text(f"UPDATE {table} SET {column} = CAST(:definition AS JSONB) WHERE id = :id"), | ||
| {"definition": json.dumps(model_definition), "id": row_id}, | ||
| ) | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| conn = op.get_bind() | ||
| _rewrap_model_definition(conn, "deployment_revisions", "model_definition") | ||
| _rewrap_model_definition(conn, "deployment_revision_presets", "model_definition") | ||
| _rewrap_model_definition(conn, "runtime_variants", "default_model_definition") | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # No-op: the ``[shell, "-c", token]`` form is also valid argv under | ||
| # any prior schema. | ||
| pass | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.