Skip to content

fix(common): strip leading whitespace in ClassPreLoader#2560

Open
phipag wants to merge 4 commits into
mainfrom
fix/classpreloader-strip-leading-whitespace
Open

fix(common): strip leading whitespace in ClassPreLoader#2560
phipag wants to merge 4 commits into
mainfrom
fix/classpreloader-strip-leading-whitespace

Conversation

@phipag

@phipag phipag commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Changes

ClassPreLoader parsed classesloaded.txt with line.stripTrailing(), which removes trailing whitespace but keeps leading whitespace. Modules that ship the file with a leading space on every line, such as powertools-tracing, passed names like " java.lang.Object" to Class.forName. That call throws ClassNotFoundException, and the loader swallows it, so SnapStart class priming loaded 0 classes. powertools-tracing ships this format today, so its automatic priming is a no-op in released versions.

This change replaces stripTrailing() with strip(), which removes leading and trailing whitespace. strip() handles both file formats, so no resource files need to change.

I added a regression test that references a class with a leading space in the test classesloaded.txt. The test fails on the current code and passes with the fix. I also confirmed the fix on a deployed Lambda: preloaded classes went from 0 to 4062.

Issue number: #2559


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

ClassPreLoader used line.stripTrailing() when parsing classesloaded.txt,
which keeps leading whitespace. Modules that ship the file with a leading
space on every line (for example powertools-tracing) passed names such as
" java.lang.Object" to Class.forName, which throws ClassNotFoundException,
so SnapStart class priming loaded 0 classes.

Use line.strip() so both leading and trailing whitespace are removed. Add a
regression test that references a class with a leading space.

Closes #2559
…ng test isolation

Harden ClassPreLoader to only pass well-formed binary class names to
Class.forName. Some modules ship classesloaded.txt with malformed entries
such as runtime-synthetic lambda classes and lines that glue a class name to
a file path, none of which are loadable. Add a debug log reporting how many
classes were preloaded.

Fixing the leading-whitespace parsing means the well-formed classes in
powertools-tracing now actually load during priming. That exposed a
pre-existing test isolation defect: PowerTracerToolEnabledForResponseWithCustomMapper
set the static TracingUtils.objectMapper from a static initializer, which
leaked into later tests in the same JVM fork and changed how errors were
serialized. Set the mapper from the constructor instead and reset it in the
aspect test teardown.

Relates to #2559
Replace the nested-quantifier regex with a single linear character class.
SonarCloud flagged the previous pattern for possible catastrophic
backtracking on large inputs. The character class rejects the same malformed
entries (paths, URL-encoded spaces, synthetic lambda names) without the
backtracking risk.
@phipag

phipag commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

The PMD CloseResource finding on ClassPreLoader.java:59 is a false positive on pre-existing code, because the stream is closed by the try (is; ...) block in preloadClassesFromStream.

Remove the binary class name pattern matching. Class.forName already rejects
malformed entries by throwing ClassNotFoundException, which is caught and
ignored, so the extra pattern check only duplicated that rejection and added
complexity. The tracing test failures were caused by test ordering (a leaked
static ObjectMapper), not by malformed entries, and that fix is retained.
@sonarqubecloud

sonarqubecloud Bot commented Jul 3, 2026

Copy link
Copy Markdown

@phipag phipag requested a review from svozza July 3, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: SnapStart class priming loads 0 classes when classesloaded.txt has leading whitespace

1 participant