rajucomp opened a new pull request, #1846: URL: https://github.com/apache/commons-lang/pull/1846
Fixes [LANG-1839](https://issues.apache.org/jira/browse/LANG-1839). ## Problem Since 3.21.0, `ToStringStyle.VISITED` is only cleared in `unregister()` when `REGISTRY` becomes empty. A `ReflectionToStringBuilder` registers its root object in its constructor, so a builder that is created but never stringified (e.g. `log.debug("...", new ReflectionToStringBuilder(...))` with debug disabled) leaves a stale entry in `REGISTRY` for the life of the thread. `REGISTRY` then never empties, `VISITED` is never cleared, and it grows on every later `toString()` with `RecursiveToStringStyle`, eventually causing an `OutOfMemoryError`. ## Fix Clear `VISITED` in a `finally` block when the outermost `ReflectionToStringBuilder.toString()` completes. A nested call always starts after its value was marked visited, so an empty `VISITED` on entry identifies the top-level call and no new state is needed. The stale one-entry `REGISTRY` leak from an orphaned builder is a separate, older issue and is left unchanged. ## Tests Added to `ReflectionToStringBuilderTest`: - an orphaned builder followed by normal `toString()` calls leaves `VISITED` empty (fails without the fix: 10,000 entries) - a plain `toString()` leaves both maps empty ## Checklist - [x] Read the contribution guidelines - [x] Read the ASF Generative Tooling Guidance - [x] I used AI to create part of this pull request: Claude Code (Claude Sonnet 5.5) helped analyse the issue, write the fix and draft the tests. I reviewed and tested the changes. - [x] `checkstyle:check` passes with 0 violations, and the `org.apache.commons.lang3.builder` tests pass locally with Maven (927 run, 0 failures). I have not run the full default `mvn` goal. - [x] Unit tests fail if the runtime change is not applied - [x] Description is detailed - [x] Commit has a meaningful subject and body 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
