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

[executeCommandLine] should return STDERR if STDOUT is empty #2114

Merged
merged 4 commits into from
Jan 16, 2021

Conversation

andrewfg
Copy link
Contributor

@andrewfg andrewfg commented Jan 14, 2021

NOTE: this relates to Issue #2033 (comment)

If ErrorCode == 0 then executeCommandLineAndWaitResponse() would always return the response from STDOUT.

However on some Unix commands (e.g. wget, curl) if ErrorCode == 0 there is no response on STDOUT, but the commands return valid information on STDERR (even though there was not an error).

In this PR, if ErrorCode == 0, the return value of executeCommandLineAndWaitResponse() is !"".equals(STDOUT) ? STDOUT : STDERR

Signed-off-by: Andrew Fiddian-Green [email protected]

@andrewfg
Copy link
Contributor Author

@kaikreuzer @cpmeister

@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/oh3-migrated-rule-executecommandline-command-fails/114027/10

@cpmeister
Copy link
Contributor

LGTM

@NorbertHD
Copy link

@andrewfg at https://community.openhab.org/t/oh3-migrated-rule-executecommandline-command-fails/114027/5 you wrote that on OH2 you got the output from wget. I have looked at the code before #1700 but there is no access to getErrorStream(), so how could the stderr output of wget be read?

About your PR, I would prefere to set the ProcessBuilder redirectErrorStream property to true. Then stdout and stderr outputs are merged in the correct chronology.

@andrewfg
Copy link
Contributor Author

how could the stderr output of wget be read?

I have no idea. All I can say is that on OH2 it returns the message, and on OH3 it does not. Maybe it is related to the change of Java version, or something like that??

would prefere to set the ProcessBuilder redirectErrorStream property to true

Ok. Currently I have no idea how to do that though. But I will look into it tomorrow. However it might therefore require reverting the prior commit related to STDERR and STDOUT when ResultCode != 0. Since if both streams are merged in STDOUT then there's no need to look at STDERR anymore at all. Or??

@NorbertHD
Copy link

NorbertHD commented Jan 14, 2021

I have no idea.

I was hoping that someone with more java (ProcessBuilder) knowledge than me knows the answer.

Ok. Currently I have no idea how to do that though

You have to remove the errorFuture code and change

Process process = processTemp = new ProcessBuilder(commandLine).start();

to

Process process = processTemp = new ProcessBuilder(commandLine).redirectErrorStream(true).start();

then there's no need to look at STDERR anymore at all?

Yes

@andrewfg
Copy link
Contributor Author

@NorbertHD many thanks for the tips. I have made the changes that you suggest.
Note: it works perfectly now in my environment. And the JUnit tests all passed.

@cpmeister it is ready to go (again) so can you please have a look at it (again) :)

Copy link
Contributor

@cpmeister cpmeister left a comment

Choose a reason for hiding this comment

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

LGTM

@cweitkamp cweitkamp added bug An unexpected problem or unintended behavior of the Core rebuild Triggers the Jenkins PR build and removed rebuild Triggers the Jenkins PR build labels Jan 16, 2021
@andrewfg
Copy link
Contributor Author

I think it would be useful if I add an extra JUnit test to confirm the STDERR redirection.

@cweitkamp cweitkamp added rebuild Triggers the Jenkins PR build and removed rebuild Triggers the Jenkins PR build labels Jan 16, 2021
@cweitkamp
Copy link
Contributor

Unit tests are always appreciated. Can you do that quickly? Otherwise a follow-up PR may be okay too.

I would like to merge this into https://github.com/openhab/openhab-core/tree/3.0.x before building a 3.0.1 patch release for this weekend.

@andrewfg
Copy link
Contributor Author

I will try it today.

@andrewfg
Copy link
Contributor Author

add an extra JUnit test to confirm the STDERR redirection

@cweitkamp done :)

@kaikreuzer
Copy link
Member

I would like to merge this into https://github.com/openhab/openhab-core/tree/3.0.x before building a 3.0.1 patch release for this weekend.

@cweitkamp Afaik, this is an improvement, not a bug fix.
The regression in comparison to 2.5 was fixed by #2104 and is already cherry-picked to 3.0.1.

@kaikreuzer kaikreuzer added enhancement An enhancement or new feature of the Core and removed bug An unexpected problem or unintended behavior of the Core labels Jan 16, 2021
@andrewfg
Copy link
Contributor Author

yhis is an improvement, not a bug fix.

@kaikreuzer hmm, not really. The prior PR only partly solved the regression from v2.5 (the case where errorCode != 0), but it did not resolve the case where errorCode == 0 and (yet) there was still data on STDERR. But I think we are talking of nuances here..

@kaikreuzer
Copy link
Member

Are you really sure that STDERR was considered in 2.5? I wouldn't understand why this would have been the case - from the code it looks as if clearly only STDOUT was returned.

@kaikreuzer
Copy link
Member

Ok, diving a bit deeper, it seems you are right - the variable name in 2.5 was chosen misleadingly. The PumpStreamHandler indeed combines the output of STDOUT and STDERR into a single stream.

@kaikreuzer kaikreuzer added bug An unexpected problem or unintended behavior of the Core and removed enhancement An enhancement or new feature of the Core labels Jan 16, 2021
@andrewfg
Copy link
Contributor Author

indeed combines the output of STDOUT and STDERR into a single stream.

Yes. And the test case is the 'wget' command which just returns an empty string in STDOUT, and returns the full connection status trace with its HTTP response headers in STDERR. The 'curl' command is similar..

@kaikreuzer
Copy link
Member

I would like to merge this into https://github.com/openhab/openhab-core/tree/3.0.x before building a 3.0.1 patch release for this weekend.

Go ahead with this then @cweitkamp - I leave it to you :-)

Copy link
Member

@kaikreuzer kaikreuzer left a comment

Choose a reason for hiding this comment

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

Seems you are busy, so I'll take care of it.

@kaikreuzer kaikreuzer merged commit 127724c into openhab:master Jan 16, 2021
@kaikreuzer kaikreuzer added the patch A PR that has been cherry-picked to a patch release branch label Jan 16, 2021
kaikreuzer pushed a commit that referenced this pull request Jan 16, 2021
@kaikreuzer kaikreuzer added this to the 3.1 milestone Jan 16, 2021
@cweitkamp
Copy link
Contributor

Yes, I was. Thanks for taking care.

soenkekueper pushed a commit to soenkekueper/openhab-core that referenced this pull request Jan 17, 2021
splatch pushed a commit to ConnectorIO/copybara-hab-core that referenced this pull request Jul 11, 2023
@andrewfg andrewfg deleted the fix-exec branch August 25, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of the Core patch A PR that has been cherry-picked to a patch release branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants