Skip to content
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

redis-cli: make returning error exit code possible when command execution fails #8136

Merged
merged 3 commits into from
Dec 13, 2020

Conversation

hwware
Copy link
Collaborator

@hwware hwware commented Dec 4, 2020

This PR fixes #8127, by providing -e option, user can control whether return error code if the command execution fails. @oranagra please take a look and let me know if i miss anything, thanks

Example:

./redis-cli config set requirepass 1234
OK
./redis-cli -e flushdb
NOAUTH Authentication required.
echo $?
1

oranagra
oranagra previously approved these changes Dec 4, 2020
@oranagra oranagra added release-notes indication that this issue needs to be mentioned in the release notes state:major-decision Requires core team consensus state:to-be-merged The PR should be merged soon, even if not yet ready, this is used so that it won't be forgotten labels Dec 4, 2020
@oranagra
Copy link
Member

oranagra commented Dec 4, 2020

thanks @hwware.

the -e option certainly makes sense (to terminate the execution as soon as one error happens).

but i think it might also be useful to have the return code represent a presence of an error even if we don't abort the execution (i.e. in case the user executed a pipeline of commands and one failed).
if we have that, maybe it should even be the default behavior, and then, for the case of a simple single command it's all the same (so users will benefit from that without the need of adding any cli flag).

@redis/core-team please approve the new flag, and share your opinion about the above.

@itamarhaber
Copy link
Member

Yep, also very related to #3475

Copy link
Member

@yossigo yossigo left a comment

Choose a reason for hiding this comment

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

@oranagra I agree it can be useful but I don't think we should change the default behavior now as it may have a big negative impact on compatibility.

While at it - we should probably also consider the different high level error cases and map them to error codes.

Comment on lines +1271 to +1272
fprintf(stderr,"%s\n",reply->str);
exit(1);
Copy link
Member

Choose a reason for hiding this comment

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

@hwware In addition to aborting with an error code, this also introduces other changes as the error is now sent to stderr rather than stdout and it does not conform to the cliFormatReply formatting. Is that intentional? We should consider maintaining compatibility here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hi @yossigo , thanks for your comments, yes this is intentional, since when cli abort with error code, the stderr will always been set based on other parts of code, for example when context error happens and we have to exit with an error: https://github.com/redis/redis/blob/unstable/src/redis-cli.c#L873 . the cliFormatReply based on my understanding is formatting reply for "no error", even though this function contains a error case we still don't consider the execution fails and the exit code is still 0.

src/redis-cli.c Outdated Show resolved Hide resolved
@oranagra
Copy link
Member

oranagra commented Dec 4, 2020

@yossigo by different high level errors, you mean an error to establish connection or authentication error (which means the command wasn't at all executed), should be distinguished from an error response from the command?
or did you also mean to distinguish between -OOM, -WRONGTYPE, and such?

i understand why my idea of changing the default behavior is wrong, i just think it's a shame that for a single command the default is to return 0 even though it failed.
anyway, let's assume we don't make it the default, do you think we should add such a thing (run the pipeline of commands to completion but reflect and error if one failed)? in a later PR or now?

@hwware
Copy link
Collaborator Author

hwware commented Dec 4, 2020

Hi @oranagra , I agree with @yossigo 's points regarding the default behaviour when command execution fails. Since this may cause big compatability issue if user already starting using current version. But we can centainy provide another flag controlling the return and it won't break the backward compatability (maybe we have too many flags IMHO and you don't think we should do this) Or we can think about this later, maybe we need to refactor the cli code for error handling part..

Copy link
Contributor

@madolson madolson left a comment

Choose a reason for hiding this comment

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

LGTM

@yossigo yossigo added the approval-needed Waiting for core team approval to be merged label Dec 10, 2020
@oranagra oranagra merged commit bde3350 into redis:unstable Dec 13, 2020
@oranagra oranagra mentioned this pull request Jan 13, 2021
JackieXie168 pushed a commit to JackieXie168/redis that referenced this pull request Mar 2, 2021
…fails (redis#8136)

without this option, redis-cli returns 0 even if command fails. kept this for backwards compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval-needed Waiting for core team approval to be merged release-notes indication that this issue needs to be mentioned in the release notes state:major-decision Requires core team consensus state:to-be-merged The PR should be merged soon, even if not yet ready, this is used so that it won't be forgotten
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[NEW] redis-cli should exit != 0 when NOPERM
5 participants