Skip to content

8298783: java/lang/ref/FinalizerHistogramTest.java failed with "RuntimeException: MyObject is not found in test output" #24143

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 9 commits into from

Conversation

bchristi-git
Copy link
Member

@bchristi-git bchristi-git commented Mar 20, 2025

I propose some cleanups to FinalizerHistogramTest.java to hopefully clear up the intermittent failures:

  • run with othervm: this test blocks the (global) finalizer thread, and also requires the (global) finalizer thread to enter the test's finalize() method
  • The test uses volatile ints, but sets them based on their current value, which is not reliable; convert to AtomicInteger
  • use PhantomReferences to ensure that at least two MyObjects have become unreachable. If one is stuck in finalize(), at least one is still waiting to be finalized and should show up in the histogram.

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-8298783: java/lang/ref/FinalizerHistogramTest.java failed with "RuntimeException: MyObject is not found in test output" (Bug - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24143

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 20, 2025

👋 Welcome back bchristi! 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
Copy link

openjdk bot commented Mar 20, 2025

@bchristi-git 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:

8298783: java/lang/ref/FinalizerHistogramTest.java failed with "RuntimeException: MyObject is not found in test output"

Reviewed-by: kbarrett, jpai

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 62 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 rfr Pull request is ready for review label Mar 20, 2025
@openjdk
Copy link

openjdk bot commented Mar 20, 2025

@bchristi-git The following label will be automatically applied to this pull request:

  • core-libs

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

@mlbridge
Copy link

mlbridge bot commented Mar 20, 2025

@jaikiran
Copy link
Member

The FinalizerHistogramTest tests the java.lang.ref.FinalizerHistogram class. To do that it sets up a situation where it generates several MyObject instances and allows (hopes) that they are GCed. In the finalize() method of the MyObject class a forever blocking operation is intentionally done, so that the java.lang.ref.Finalizer class which is responsible for calling the finalize() method on this class becomes blocked when it calls MyObject.finalize() on one such MyObject instance. That way, the internal queue that the Finalizer class maintains accumulates additional objects whose finalize() needs to be called.

The test then prints out the contents of this internal queue and expects that there be at least one more MyObject instance in this queue awaiting their finalize() to be invoked. Most of the times this all works fine and the test sees the MyObject instance(s) in that queue and passes.

In the cases where this intermittently fails, what seems to be happening is that, out of the numerous MyObject instances that the test hopes will be GCed, just one of them gets GCed and the Finalizer calls that instance's finalize() method. The test notices that the finalize() has been invoked and immediately goes on to print out the contents of the internal queue of the Finalizer expecting to see more MyObject instances in it. It doesn't find any and ends up failing.

As noted, this is a test bug because in its current form, there's no guarantee that the internal queue would have (at that moment) any more instances queued up when the first GCed MyObject.finalize() method invoked.

The proposed change here updates the test in such a way that we wait for at least 2 instances of MyObject to be picked up for GC (and thus end up in that internal queue) and the MyObject.finalize() of the first instance blocks forever, thus providing a guarantee that when the test access that queue, it will find at least the second instance of MyObject in it.

I think the overall idea/change to this test looks good to me. I do have a comment about the implementation detail of this change which I've added inline.

Copy link
Member

@jaikiran jaikiran left a comment

Choose a reason for hiding this comment

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

Thank you Brent for the updates. Good catch on the PhantomReference usage in the test. The latest changes look good to me.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 28, 2025
lock.lock();
}
}

public static void main(String[] argvs) {
try {
lock.lock();
for(int i = 0; i < objectsCount; ++i) {
refQForTwo = new RefQForTwo(new MyObject(), new MyObject());
for(int i = 2; i < OBJECTS_COUNT; ++i) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for(int i = 2; i < OBJECTS_COUNT; ++i) {
for (int i = 2; i < OBJECTS_COUNT; ++i) {

Copy link
Member

@jaikiran jaikiran left a comment

Choose a reason for hiding this comment

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

Still looks good to me.

}
System.out.println("ref1Cleared: " + refQForTwo.ref1Cleared);
System.out.println("ref2Cleared: " + refQForTwo.ref2Cleared);
System.out.println("trappedCount.intValue(): " + trappedCount.intValue());

Choose a reason for hiding this comment

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

This could be made completely reliable, and much simpler, by using WhiteBox.

Don't introduce RefQForTwo. (It doesn't provide reliable information.)

Use WhiteBox::fullGC() to trigger finalization, instead of ForceGC.

Use (new) WhiteBox::waitForReferenceProcessing() to wait until the
FinalReferences have been enqueued (and mostly trapped in the queue).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Apr 29, 2025
do {
refProResult = wb.waitForReferenceProcessing();
System.out.println("waitForReferenceProcessing returned: " + refProResult);
} while (refProResult);

Choose a reason for hiding this comment

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

Why the spammy output? Why not just

while (wb.waitForReferenceProcessing()) {}

Copy link
Member Author

Choose a reason for hiding this comment

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

Right now, the test just it prints a single line:
waitForReferenceProcessing returned: false
and I expect this to continue to be true.

For intermittently failing tests, I'm inclined to add a little extra output as long as I'm fiddling with it anyway. That way, if the test ever starts failing again, we have some (hopefully useful) clues about what happened.

Choose a reason for hiding this comment

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

Right now, the test just it prints a single line: waitForReferenceProcessing returned: false and I expect this to continue to be true.

If reference processing is being slow because of load, that will be different.

For intermittently failing tests, I'm inclined to add a little extra output as long as I'm fiddling with it anyway. That way, if the test ever starts failing again, we have some (hopefully useful) clues about what happened.

OK.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 4, 2025
@bchristi-git
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented May 6, 2025

Going to push as commit fe29cad.
Since your change was applied there have been 107 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 May 6, 2025
@openjdk openjdk bot closed this May 6, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 6, 2025
@openjdk
Copy link

openjdk bot commented May 6, 2025

@bchristi-git Pushed as commit fe29cad.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs [email protected] integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

5 participants