Skip to content

Fix logstash.bat not setting exit code#12948

Merged
andsel merged 1 commit intoelastic:masterfrom
dionrhys:fix-bat-exit-code
Jun 21, 2021
Merged

Fix logstash.bat not setting exit code#12948
andsel merged 1 commit intoelastic:masterfrom
dionrhys:fix-bat-exit-code

Conversation

@dionrhys
Copy link
Contributor

Release notes

Fixed Windows logstash.bat not setting exit code

What does this PR do?

This PR makes the Windows logstash.bat exit with the last %ERRORLEVEL% at the end, so that any error in running Logstash will get propagated back to the command line.

Before this change, logstash.bat would always exit with code 0 - success (when doing cmd.exe /C logstash.bat), even if the java.exe process exited with a non-zero code (e.g. due to Logstash throwing an error at runtime).

Why is it important/What is the impact to the user?

Without this change, abnormal exits from Logstash are not detected as such by an owning process (such as service wrappers NSSM or WinSW, or the Windows Task Scheduler). This means recovery actions (like restarting the process or sending a notification) will not get triggered.

I use WinSW as a service wrapper to "cmd.exe /C logstash.bat", and it was blissfully accepting of a runtime Logstash error that caused the process to exit, because the exit code being returned was 0. With this change added locally, it can now trigger its recovery actions when the error happens.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files (and/or docker env variables)
  • I have added tests that prove my fix is effective or that my feature works

Author's Checklist

  • Does the label bug fit with this change, or should it be enhancement?
  • Consider should this have tests added?

How to test this PR locally

This is a Windows-only change.

  1. Open cmd.exe to the bin folder
  2. Enter cmd.exe /C logstash.bat (This invokes the script as an external process)
  3. In another command prompt window, type taskkill /F /IM java.exe (or target the PID with /PID if you don't want to disrupt other java.exe processes)(/F is not optional)
  4. In the original command prompt window, observe that the command has now ended
  5. In the original command prompt window, enter echo %ERRORLEVEL%

Before the change in this PR, the last step would print out 0. With the change in this PR, the last step prints out 1 instead.

Logs

Before PR:

C:\Elastic\logstash\bin>cmd.exe /C logstash.bat
Using JAVA_HOME defined java: C:\Program Files\Java\jre1.8.0_171\
WARNING, using JAVA_HOME while Logstash distribution comes with a bundled JDK

~~java.exe is manually killed here~~~

C:\Elastic\logstash\bin>echo %errorlevel%
0

After PR:

C:\Elastic\logstash\bin>cmd.exe /C logstash.bat
Using JAVA_HOME defined java: C:\Program Files\Java\jre1.8.0_171\
WARNING, using JAVA_HOME while Logstash distribution comes with a bundled JDK

~~java.exe is manually killed here~~~

C:\Elastic\logstash\bin>echo %errorlevel%
1

@logstashmachine
Copy link
Contributor

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

@andsel
Copy link
Contributor

andsel commented Jun 17, 2021

Hi @dionrhys ,
thank's for this PR good catch. It's good to be and will be merged after another upcoming PR which fixes a build problem on Windows

@andsel
Copy link
Contributor

andsel commented Jun 17, 2021

The fix PR is #13000

Copy link
Contributor

@andsel andsel left a comment

Choose a reason for hiding this comment

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

LGTM

@andsel andsel force-pushed the fix-bat-exit-code branch from c3091a1 to b4bea62 Compare June 21, 2021 08:32
@andsel andsel merged commit 1f9ef97 into elastic:master Jun 21, 2021
andsel pushed a commit to andsel/logstash that referenced this pull request Jun 21, 2021
This PR makes the Windows logstash.bat exit with the last %ERRORLEVEL% at the end, so that any error in running Logstash will get propagated back to the command line.

Before this change, logstash.bat would always exit with code 0 - success (when doing cmd.exe /C logstash.bat), even if the java.exe process exited with a non-zero code (e.g. due to Logstash throwing an error at runtime).

(cherry picked from commit 1f9ef97)
andsel added a commit that referenced this pull request Jun 21, 2021
This PR makes the Windows logstash.bat exit with the last %ERRORLEVEL% at the end, so that any error in running Logstash will get propagated back to the command line.

Before this change, logstash.bat would always exit with code 0 - success (when doing cmd.exe /C logstash.bat), even if the java.exe process exited with a non-zero code (e.g. due to Logstash throwing an error at runtime).

(cherry picked from commit 1f9ef97)

Co-authored-by: Dion Williams <dionrhys1@gmail.com>
kares added a commit to kares/logstash that referenced this pull request Jul 1, 2021
* master: (41 commits)
  Test: resolve integration failure due ECS mode (elastic#13044)
  Feat: event factory support (elastic#13017)
  Doc: Add geoip database API to node stats (elastic#13019)
  Add geoip database metrics to /node/stats API (elastic#13004)
  ecs: on-by-default plus docs (elastic#12830)
  ispec: fix cross-spec leak from fatal error integration specs (elastic#13002)
  Fix UBI source URL (elastic#13008)
  update fpm to allow pkg creation on jdk11+jruby 9.2 (elastic#13005)
  Add unit test to grant that production aliases correspond to a published RubyGem (elastic#12993)
  Fix logstash.bat not setting exit code (elastic#12948)
  Use the OS separator to invoke gradlew from Rake script (elastic#13000)
  Allow per-pipeline config of ECS Compatibility mode via Central Management (elastic#12861)
  Update jinja2 dependency in docker build (elastic#12994)
  fix database manager with multiple pipelines (elastic#12862)
  Fix Reflections stack traces when process yml files in classpath and debug is enabled (elastic#12991)
  Fix/log4j routing to avoid create spurious file (elastic#12965)
  Deps: update JRuby to 9.2.19.0 (elastic#12989)
  Doc: Add tip for checking for existing field (elastic#12899)
  Added test to cover the installation of aliased plugins (elastic#12967)
  CI: Update logstash_release.json after 7.3.12 (elastic#12986)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants