Skip to content

Simplify waitForCloseablesToClose() to focus on waiting semantics#843

Open
peter-lawrey wants to merge 2 commits into
developfrom
adv/waitForCloseablesToClose
Open

Simplify waitForCloseablesToClose() to focus on waiting semantics#843
peter-lawrey wants to merge 2 commits into
developfrom
adv/waitForCloseablesToClose

Conversation

@peter-lawrey

@peter-lawrey peter-lawrey commented Mar 16, 2026

Copy link
Copy Markdown
Member

Refactor CloseableUtils.waitForCloseablesToClose(long) so its behaviour matches its name and return type more clearly: it now only waits for tracked closeables to enter the closing state within the given timeout and returns true or false accordingly.

Motivation

waitForCloseablesToClose(long) previously mixed two responsibilities:

  • waiting for closeables to close within a timeout
  • performing assertion-style validation and exception throwing on unreleased resources

That made the method harder to read and blurred the distinction between it and assertCloseablesClosed(), which is the method intended for strict validation and failure reporting.

This change makes the contract clearer:

  • waitForCloseablesToClose(long) is a polling/wait helper
  • assertCloseablesClosed() remains the diagnostic/assertion path

Change

The method has been simplified to:

  • invoke pending cleanup work up front

    • CleaningThreadLocal.cleanupNonCleaningThreads()
    • BackgroundResourceReleaser.releasePendingResources()
  • check whether all tracked ManagedCloseable instances are already closing

  • return true once they are

  • return false if the timeout expires

  • pause briefly between checks rather than re-running assertion-oriented logic

The call in finalize() has also been updated to use the local method directly, improving readability.

What was removed

The previous implementation included behaviour that was more appropriate for assertion/debug flows, including:

  • disabling single-threaded checks on AbstractCloseable
  • calling throwExceptionIfNotReleased() on ReferenceCountedTracer
  • retrying around IllegalStateException

That logic obscured the purpose of the method and overlapped with assertCloseablesClosed().

Behavioural intent

This is primarily a clarity and responsibility-separation change:

  • waitForCloseablesToClose(long) now has straightforward wait semantics
  • assertCloseablesClosed() remains the place for stronger verification and failure signalling

Benefits

  • clearer API intent
  • simpler control flow
  • easier distinction between “wait and report success/failure” and “assert and diagnose”
  • reduced surprise from a boolean-returning method throwing assertion-related failures during polling

Notes

A @see link has been added in both directions to make the relationship between the two methods explicit.

@peter-lawrey
peter-lawrey requested a review from tgd March 16, 2026 10:33
@peter-lawrey
peter-lawrey changed the base branch from ea to develop March 16, 2026 10:38
@peter-lawrey peter-lawrey changed the title Clear separation of roles between waitForCloseableToClose and assertC… Simplify waitForCloseablesToClose() to focus on waiting semantics Mar 16, 2026
@sonarqubecloud

Copy link
Copy Markdown

@peter-lawrey
peter-lawrey requested a review from tgd June 2, 2026 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants