Skip to content

test: fix flaky ReferenceCountingResourceHolderTest cleaner checks - #20317

Open
FrankChen021 wants to merge 1 commit into
apache:masterfrom
FrankChen021:flaky/refcount-cleaner
Open

FrankChen021 wants to merge 1 commit into
apache:masterfrom
FrankChen021:flaky/refcount-cleaner

Conversation

@FrankChen021

@FrankChen021 FrankChen021 commented Sep 10, 2026

Copy link
Copy Markdown
Member

Related to #20312 (item 2).

Description

ReferenceCountingResourceHolderTest.testResourceHandlerClearedByJVM fails intermittently on master with:

expected: <1> but was: <3>
  at ReferenceCountingResourceHolderTest.verifyCleanerRun(ReferenceCountingResourceHolderTest.java:126)

Example: https://github.com/apache/druid/actions/runs/34432737655/job/102731457738.

ReferenceCountingResourceHolder.LEAKED_RESOURCES is a JVM-global counter, and surefire runs with reuseForks=true. verifyCleanerRun loops on System.gc() until the counter changes and then asserts an exact +1. Holders leaked by other test classes in the same fork are collected by the same forced GC, so the delta can be larger than 1. Because the counter is incremented before the closer runs, the loop can also exit before this test's own closer has executed, which would make the released assertion fail too.

Changes

  • Loop until this test's own released flag is set, which is the completion signal that belongs to this test.
  • Assert released, and only assert a lower bound (>= initial + 1) on the global counter.

Verified locally with mvn -pl processing test -Dtest=ReferenceCountingResourceHolderTest (3 tests pass).


Key changed/added classes in this PR
  • ReferenceCountingResourceHolderTest

This PR has:

  • been self-reviewed.
  • added or updated unit tests.

Additional CI evidence

The same global-counter/cleaner race reappeared as a recovered flaky failure in both dependency-update PRs:

These occurrences were recorded as flaky failures; they are independent of the dependency changes in those PRs.

The leaked resource counter is global to the JVM and is incremented before the
closer runs. With reuseForks=true, the forced GC loop can be ended by a holder
leaked by another test class in the same fork, before this test's closer has
run, so the exact-count assertion and the released check fail intermittently.

Wait for this test's own released flag instead, and only assert a lower bound
on the global counter.
Copilot AI lite review requested due to automatic review settings September 10, 2026 15:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The change is narrowly scoped to test logic, aligns with the flakiness root cause, and reduces reliance on JVM-global state without affecting production code.

Pull request overview

This PR fixes flakiness in ReferenceCountingResourceHolderTest.testResourceHandlerClearedByJVM caused by relying on a JVM-global leaked-resource counter in a test environment where Maven Surefire reuses forks, allowing unrelated leaked holders from other tests to affect the assertion.

Changes:

  • Update verifyCleanerRun to wait for this test’s own completion signal (released flag) rather than waiting for a change in the global leak counter.
  • Relax the leaked-resource counter assertion from exact +1 to a lower bound (>= initial + 1) to tolerate additional leaked holders being collected in the same JVM.
File summaries
File Description
processing/src/test/java/org/apache/druid/collections/ReferenceCountingResourceHolderTest.java Makes the cleaner/GC verification logic deterministic by waiting on the test-specific released flag and loosening the global counter expectation.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@FrankChen021 FrankChen021 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🟢 Approval recommended

No actionable issues found in this review. The test now waits for its own closer completion signal, while treating the JVM-global leak counter as a lower bound, which matches the cleaner ordering and avoids interference from unrelated leaked holders.

Reviewed 1 of 1 changed files.


This is an automated review by Codex GPT-5.6-Luna(max)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants