Skip to content

Exit code is always 0 for non-interactive runnerΒ #1339

Closed

Description

@ffried

The NonInteractiveShellRunner silently swallows error, leading to a 0 exit code even though the command failed:

https://github.com/spring-projects/spring-shell/blob/main/spring-shell-core/src/main/java/org/springframework/shell/core/NonInteractiveShellRunner.java#L158

We have jobs that rely on the exit code being non-zero on failures to e.g. be restarted or properly handled.
For that, we also have custom exit code mappers to make sure the exit code is set accordingly.

Activity

  1. fmbenhassine commented on Apr 21, 2026

    @fmbenhassine
    Contributor

    This is a valid issue. Thank you for raising it. However, the issue only happens when returning an ExitCode from the command. Throwing an exception from the command yields a non-zero exit as expected:

    package org.springframework.shell.samples.noninteractive;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.shell.core.command.*;
    
    @SpringBootApplication
    public class SpringShellApplication {
    
    	public static void main(String[] args) {
    		System.setProperty("spring.shell.interactive.enabled", "false");
    		SpringApplication.run(SpringShellApplication.class, "fail");
    		// same output with the following:
    		//System.exit(SpringApplication.exit(SpringApplication.run(SpringShellApplication.class, "fail")));
    	}
    
    	@Bean
    	public Command fail() {
    		return new AbstractCommand("fail", "A command that always fails") {
    
    			@Override
    			public ExitStatus doExecute(CommandContext commandContext) throws Exception {
    				// throwing an exception will cause the app to exit with a non-zero exit code
    				// throw new RuntimeException("Unable to execute command fail");
    
    				// however, returning an exit status with a non-zero code will not cause the app to exit with that code
    				return new ExitStatus(42, "Command execution failed");
    			}
    		};
    	}
    
    }

    Moreover, I see that it is not possible to return a value from ShellRunner#run (this design seems to have been always like that by looking at previous versions, and I guess it was designed in a similar fashion to Boot's ApplicationRunner#run / CommandLineRunner#run). Customizing the exit code is exception-based by design, through ExitCodeExceptionMapper:

    package org.springframework.shell.samples.noninteractive;
    
    import org.springframework.boot.ExitCodeExceptionMapper;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.shell.core.command.*;
    
    @SpringBootApplication
    public class SpringShellApplication {
    
    	public static void main(String[] args) {
    		System.setProperty("spring.shell.interactive.enabled", "false");
    		SpringApplication.run(SpringShellApplication.class, "fail");
    		// same output with the following:
    		//System.exit(SpringApplication.exit(SpringApplication.run(SpringShellApplication.class, "fail")));
    	}
    
    	@Bean
    	public ExitCodeExceptionMapper exitCodeExceptionMapper() {
    		return exception -> {
    			if (exception instanceof CommandExecutionException e) {
    				return e.getExitCode(); // this is missing as of v4.0.1, but should be possible
    			}
    			return 0;
    		};
    	}
    
    	@Bean
    	public Command fail() {
    		return new AbstractCommand("fail", "A command that always fails") {
    
    			@Override
    			public ExitStatus doExecute(CommandContext commandContext) throws Exception {
    				// with an exitCodeExceptionMapper, returning an exit status with a non-zero code should cause the app to exit with that code
    				return new ExitStatus(42, "Command execution failed");
    			}
    		};
    	}
    
    }

    Therefore, the shell should indeed throw an exception in this case to be able to intercept it in any custom exception mapper.

  2. added and removed
    status/need-triageTeam needs to triage and take a first look
    on Apr 21, 2026
  3. added this to the 4.0.2 milestone on Apr 21, 2026
  4. self-assigned this
    on Apr 21, 2026
  5. added a commit that references this issue on Apr 21, 2026
    8a52662
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions