Skip to content

[4.x] Keep exit codes in the valid range and honour an error exit code of 0 - #37

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-exit-code-range
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-exit-code-range

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Two inconsistencies in the exit code handling:

  1. execute() only limited exit codes above 255. A negative command return value or Throwable code was passed to exit() unchanged, where the operating system wraps it modulo 256: -1 became 255, and -256 became 0, i.e. success. Every value outside 0–255 now becomes 255 (which is also what -1 produced before).
  2. ApplicationErrorEvent::getExitCode() used ?:, so setExitCode(0) was ignored and the event reported the Throwable's code (or 1) instead. CommandErrorEvent already checked for null; both now do. execute() still turns an APPLICATION_ERROR exit code of 0 into 1, so the process exit code for an unhandled error does not change — only listeners that read the event's exit code see the 0 they set.

Not changed: setExitCode() in both error events still writes the exit code into the Throwable's code property via reflection. Symfony's ConsoleErrorEvent::setExitCode() does the same, and it is how an exit code set by a COMMAND_ERROR listener reaches the APPLICATION_ERROR event and the process exit code (the new testCommandErrorExitCodeIsSetByEventListener pins this). Dropping it would need another way to carry the code from one event to the other.

Testing Instructions

  • ApplicationTest::testExitCodeIsLimitedToTheValidRange (return values 0, 255, 256, -1, -256) and testErrorExitCodeIsLimitedToTheValidRange (exception code -256): the negative cases fail on 4.x-dev (close() receives -1/-256).
  • tests/Event/ApplicationErrorEventTest.php checks default, explicit and zero exit codes for both error events; setExitCode(0) fails for ApplicationErrorEvent on 4.x-dev.
  • ApplicationTest::testCommandErrorExitCodeIsSetByEventListener covers the COMMAND_ERROR path (passes before and after).

Documentation Changes Required

docs/overview.md: new "Exit codes" section in "Application Events" describing the range, the APPLICATION_ERROR default and the COMMAND_ERROR handling.

🤖 Generated with Claude Code

Only exit codes above 255 were limited. A negative return value or
error code was passed to exit() as is and wrapped around, -256 even to
0, reporting success. Map every value outside 0-255 to 255.

ApplicationErrorEvent::getExitCode() used ?: and so ignored
setExitCode(0), while CommandErrorEvent checks for null. Use the null
check in both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hackwar

Hackwar commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Approved

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.

1 participant