Skip to content

8303133: Update ProcessTools.startProcess(...) to exit early if process exit before linePredicate is printed. #12751

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

Closed
wants to merge 6 commits into from

Conversation

lmesnik
Copy link
Member

@lmesnik lmesnik commented Feb 24, 2023

The solution proposed by Stefan K

The startProcess() might wait forever for the expected line if the process exits (failed to start). It makes sense to just fail earlier in such cases.

The fix also move
'output = new OutputAnalyzer(this.process);'
in method xrun() to be able to try to print them in waitFor is failed/interrupted.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8303133: Update ProcessTools.startProcess(...) to exit early if process exit before linePredicate is printed.

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/12751/head:pull/12751
$ git checkout pull/12751

Update a local copy of the PR:
$ git checkout pull/12751
$ git pull https://git.openjdk.org/jdk pull/12751/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 12751

View PR using the GUI difftool:
$ git pr show -t 12751

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/12751.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 24, 2023

👋 Welcome back lmesnik! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 24, 2023
@openjdk
Copy link

openjdk bot commented Feb 24, 2023

@lmesnik The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added hotspot-runtime hotspot-runtime-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels Feb 24, 2023
@mlbridge
Copy link

mlbridge bot commented Feb 24, 2023

Webrevs

@dcubed-ojdk
Copy link
Member

I fixed a typo in the bug's synopsis line so you'll need to update the PR's title.
Use "/issue JDK-8303133" as the quickest way to set it.

@lmesnik
Copy link
Member Author

lmesnik commented Feb 24, 2023

/issue JDK-8303133
Thanks

@openjdk openjdk bot changed the title 8303133: Update ProcessTols.startProcess(...) to exit early if process exit before linePredicate is printed. 8303133: Update ProcessTools.startProcess(...) to exit early if process exit before linePredicate is printed. Feb 24, 2023
@openjdk
Copy link

openjdk bot commented Feb 24, 2023

@lmesnik This issue is referenced in the PR title - it will now be updated.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

I have a number of issues with the changes. I think you have basically addressed the problem of waiting forever if no predicate line was forthcoming, but I think you have introduced a race with normal termination of the process.

boolean succeeded = Utils.waitForCondition(() -> {
//Fail if process finished before printed expected string
if (!p.isAlive()) {
latch.countDown();
Copy link
Member

Choose a reason for hiding this comment

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

This countdown serves no purpose. The latch is used to coordinate the current thread and the stream pumper thread: the current thread calls await and the streamPumper calls countDown. Here the current thread is not going to call await and it doesn't need to countDown to release itself.

if (!latch.await(Utils.adjustTimeout(timeout), unit)) {
throw new TimeoutException();
// Every second check if process is still alive
boolean succeeded = Utils.waitForCondition(() -> {
Copy link
Member

Choose a reason for hiding this comment

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

This use of waitForCondition with a sleep time of zero confused me quite a bit. Now I realize that you are putting waitForCondition into a potential busy-poll loop, but then you introduce the one-second timed await as part of the condition, thus effectively checking the condition once a second. This seems somewhat convoluted compared to just using a sleep time of 1000ms and changing the await to a call to getCount() > 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using await() is more effective than checking every second. However, might be it is not essential for multi-process synchronization.

//Fail if process finished before printed expected string
if (!p.isAlive()) {
latch.countDown();
throw new RuntimeException("Started process " + name + " is not alive.");
Copy link
Member

Choose a reason for hiding this comment

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

This seems problematic. The process has terminated but you don't know why - it may have completed normally and produced all the output such that the await below would return immediately with true, but you are now going to throw an exception. ???

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! You are right, we need to check first if the line has been printed.
However, there is an interesting question the process can exit before streams are read and the latch is set to zero. The documentation says that linePredicate should detect if the process is warmed-up. So it is not expected that it should exit right after the start.

Copy link
Member

@dholmes-ora dholmes-ora Feb 27, 2023

Choose a reason for hiding this comment

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

Yes there remains a race, if the process exits but the streamPumper has not yet had the chance to call countDown then we would again throw the exception. Perhaps all we can do is give a little extra time after detecting a dead process?

if (!p.isAlive()) {
  if (latch.getCount() > 0) {
    // Give some extra time for the StreamPumper to run after the process completed
   Thread.sleep(1000); 
   if (latch.getCount() > 0) {
    throw new RuntimeException("Started process " + name + " but it terminated without the expected output");
  }
}

this.process = ProcessTools.startProcess(name, processBuilder, waitfor);
// Release when process is started
latch.countDown();
String name = Thread.currentThread().getName();
Copy link
Member

Choose a reason for hiding this comment

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

The original code passes the name of the ProcessRunnable to startProcess, not the name of the current thread. It is not obvious/apparent that they are the same. But if they are then it is cheaper to use name than do a dynamic query on the current thread.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Fixed, it is a mistake.

throw new RuntimeException("Started process " + name + " is not alive.");
// Give some extra time for the StreamPumper to run after the process completed
Thread.sleep(1000);
if (latch.getCount() > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: we use > 0 here but != 0 above.

Copy link
Member Author

Choose a reason for hiding this comment

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

let use > in all places

// Give some extra time for the StreamPumper to run after the process completed
Thread.sleep(1000);
if (latch.getCount() > 0) {
throw new RuntimeException("Started process " + name + " is not alive.");
Copy link
Member

Choose a reason for hiding this comment

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

Nit: the message is not very informative - we expect the process to die, the problem is it died before giving the necessary output. Suggestion:

"Started process " + name + "terminated before producing the expected output"

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

This seems okay to me now. Thanks.

@openjdk
Copy link

openjdk bot commented Feb 28, 2023

@lmesnik This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8303133: Update ProcessTools.startProcess(...) to exit early if process exit before linePredicate is printed.

Reviewed-by: dholmes, rriggs

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 36 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Feb 28, 2023
@lmesnik
Copy link
Member Author

lmesnik commented Feb 28, 2023

@dholmes-ora, Thank you for your comments.

@lmesnik
Copy link
Member Author

lmesnik commented Mar 1, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Mar 1, 2023

Going to push as commit 1fdaf25.
Since your change was applied there have been 40 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Mar 1, 2023
@openjdk openjdk bot closed this Mar 1, 2023
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Mar 1, 2023
@openjdk openjdk bot removed the rfr Pull request is ready for review label Mar 1, 2023
@openjdk
Copy link

openjdk bot commented Mar 1, 2023

@lmesnik Pushed as commit 1fdaf25.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@dholmes-ora
Copy link
Member

We are seeing large numbers of failures after this change was integrated, so it will likely need to be backed out.

@lmesnik lmesnik deleted the 8303133 branch June 5, 2024 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

4 participants