Skip to content

fix(cli): exit with the script's status when crewai train fails - #9572

Merged
Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
crewAIInc:mainfrom
anilkr09:fix/train-exit-status
Nov 19, 2026
Merged

Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
crewAIInc:mainfrom
anilkr09:fix/train-exit-status

Conversation

@anilkr09

Copy link
Copy Markdown
Contributor

Related issue

Fixes #9571

Summary

crewai train -n N runs uv run train …; when the project's training script fails, train_crew prints the error and returns, so the command exits 0 and a CI job sees a success. The except branches only echo (lib/cli/src/crewai_cli/train_crew.py:29-34, main at 5f3fe6ff08).

  • Re-raise as SystemExit after the existing messages: with the script's own exit code for a failed script, and with 1 when uv cannot be started, as crewai run (lib/cli/src/crewai_cli/run_crew.py:1375-1383) and crewai install (lib/cli/src/crewai_cli/cli.py:717-720, and lib/cli/src/crewai_cli/cli.py:722 for the 1) do.
  • The two existing tests that called train_crew and expected a normal return now expect SystemExit.
Case (crewai … in a throwaway project) main 5f3fe6ff08 this PR
train -n 1, script exits 1 (A) exit 0 exit 1
train -n 1, script exits 42 (F) exit 0 exit 42
train -n 1, uv cannot be started (I) exit 0 exit 1
train -n 1, script SIGKILLs itself (K) exit 0 exit 137
train -n 1 in an empty folder, real uv (N) exit 0 exit 2
train -n 1, script exits 0 (H, control) exit 0 exit 0
train -n 0, invalid argument (M, control) exit 1 exit 1
run, script exits 42 (G, control) exit 42 exit 42

Not changed: crewai replay and crewai test have the same pattern and still exit 0 here (rows B, C); issues #9456 and #9457 track them. Scripts that relied on crewai train exiting 0 after a failed script will now see a non-zero code, as with crewai run.

Verification

  • Tests added or updated for the changed behavior

  • Relevant tests and quality checks pass locally

  • lib/cli/tests/test_train_crew.py: 4 of its 9 tests fail with main's source (3 with DID NOT RAISE SystemExit; the train command test with assert 0 == 42), all 9 pass here in file order on macOS, Python 3.13.15 and 3.10.21, and with the default random order and xdist on Python 3.13.15. No network or LLM.

  • Neighbouring suites: lib/cli/tests/test_cli.py, test_crew_test.py, test_install_crew.py, test_run_crew.py: 198 passed.

  • ruff check, ruff format --check and mypy on lib/cli/src/crewai_cli/train_crew.py: clean.

  • Not run locally: the full suite, Linux and Windows; CI runs Python 3.10–3.13.

Additional context

None.

Written with an AI coding agent (Claude Code). CONTRIBUTING asks for the llm-generated label on AI-generated PRs; I can't add labels, so could a maintainer add it?

@anilkr09
anilkr09 marked this pull request as ready for review November 19, 2026 09:57
@strix-security

strix-security Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Strix Security Review

✅ No open security findings.

PR summary: Makes `crewai train` propagate a non-zero exit status: `train_crew` now raises `SystemExit` with the training script's own return code when `uv run train` fails, and `SystemExit(1)` when the subprocess cannot be started, matching how `crewai run` and `crewai install` behave. Two existing tests were updated to expect `SystemExit`, and new tests cover the exit codes through the `train` click command.

Review summary

The change is two lines: lib/cli/src/crewai_cli/train_crew.py now raises SystemExit(e.returncode) after logging a failed uv run train, and SystemExit(1) after logging an unexpected exception, with the remaining changes limited to tests. I traced the value from the subprocess result to the process exit status, confirmed the train command (cli.py:415-426) calls train_crew as its final statement with no wrapping handler that could swallow the exit, and confirmed the function performs no cleanup or post-call work that the new path skips. No security issue was found: the exit status is consumed only by the invoking developer's own shell or CI, no privileged action, data access, or cross-user operation depends on it, and the codebase already uses the same SystemExit(returncode) pattern in the neighboring command modules.

Ruled out (1)
  • Can a failed `crewai train` still report success because the raw subprocess return code is passed to `SystemExit` without normalization?
    A child exiting 256 is reported by the OS as status 0 and so raises no `CalledProcessError` at all (unchanged by this PR), and a signal-killed child's negative return code yields a non-zero status (247) that only differs cosmetically from the 137 convention used elsewhere; the value is controlled b…

Review 1 on this PR · f36a38b · requested Nov 19 09:57 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings · Try a deeper review with @strix-security /deep

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this is a clean fix. Re-raising SystemExit(e.returncode) / SystemExit(1) in lib/cli/src/crewai_cli/train_crew.py matches how crewai install and crewai run behave, and the messages are unchanged.

I checked the new tests on both sides: with the PR's test file on the base branch 6 tests fail (including the CLI-level exit-42 test); on this branch all pass, and the success path still exits 0. Test coverage through the train click command is a nice touch.

@Vidit-Ostwal
Vidit Ostwal (Vidit-Ostwal) merged commit 8f7801a into crewAIInc:main Nov 19, 2026
56 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your first contribution to CrewAI, anilkr09!

We really appreciate the time and care you put into this PR. Your work is now part of CrewAI — welcome to the contributor community.

Want to share your contribution? Totally optional:

Share on X · Share the PR on LinkedIn

If the links don't work, feel free to copy and personalize this:

I just made my first contribution to @crewAIInc!

Excited to help build the future of AI agents with CrewAI.

https://github.com/crewAIInc/crewAI/pull/9572

#OpenSource #AI #CrewAI

If you share, we'd love to see it — tag crewAI (@crewAIInc) on X and CrewAI on LinkedIn.

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.

[BUG] crewai train exits 0 when the training script fails

2 participants