Repository navigation
Fix dependency resolution for nested JARs (with fix for Spring Boot) - #6720
Conversation
BenchmarksStartupParameters
See matching parameters
SummaryFound 0 performance improvements and 0 performance regressions! Performance is the same for 43 metrics, 11 unstable metrics. LoadParameters
See matching parameters
SummaryFound 0 performance improvements and 0 performance regressions! Performance is the same for 12 metrics, 14 unstable metrics. Request duration reports for insecure-bankgantt
title insecure-bank - request duration [CI 0.99] : candidate=1.31.0-SNAPSHOT~6c582788d6, baseline=1.31.0-SNAPSHOT~a718db98d8
dateFormat X
axisFormat %s
section baseline
no_agent (367.919 µs) : 348, 388
. : milestone, 368,
iast (469.793 µs) : 449, 490
. : milestone, 470,
iast_FULL (532.882 µs) : 512, 553
. : milestone, 533,
iast_GLOBAL (503.251 µs) : 481, 526
. : milestone, 503,
iast_HARDCODED_SECRET_DISABLED (471.104 µs) : 451, 491
. : milestone, 471,
iast_INACTIVE (448.893 µs) : 428, 469
. : milestone, 449,
iast_TELEMETRY_OFF (470.709 µs) : 450, 492
. : milestone, 471,
tracing (445.491 µs) : 425, 466
. : milestone, 445,
section candidate
no_agent (366.407 µs) : 346, 386
. : milestone, 366,
iast (477.585 µs) : 457, 498
. : milestone, 478,
iast_FULL (537.312 µs) : 517, 558
. : milestone, 537,
iast_GLOBAL (507.966 µs) : 485, 531
. : milestone, 508,
iast_HARDCODED_SECRET_DISABLED (468.446 µs) : 448, 489
. : milestone, 468,
iast_INACTIVE (445.544 µs) : 425, 466
. : milestone, 446,
iast_TELEMETRY_OFF (464.909 µs) : 444, 486
. : milestone, 465,
tracing (445.649 µs) : 425, 466
. : milestone, 446,
Request duration reports for petclinicgantt
title petclinic - request duration [CI 0.99] : candidate=1.31.0-SNAPSHOT~6c582788d6, baseline=1.31.0-SNAPSHOT~a718db98d8
dateFormat X
axisFormat %s
section baseline
no_agent (1.351 ms) : 1332, 1370
. : milestone, 1351,
appsec (1.804 ms) : 1780, 1828
. : milestone, 1804,
iast (1.523 ms) : 1500, 1547
. : milestone, 1523,
profiling (1.544 ms) : 1521, 1568
. : milestone, 1544,
tracing (1.505 ms) : 1481, 1528
. : milestone, 1505,
section candidate
no_agent (1.346 ms) : 1327, 1365
. : milestone, 1346,
appsec (1.785 ms) : 1761, 1809
. : milestone, 1785,
iast (1.519 ms) : 1496, 1542
. : milestone, 1519,
profiling (1.585 ms) : 1561, 1610
. : milestone, 1585,
tracing (1.513 ms) : 1489, 1536
. : milestone, 1513,
|
1c81258 to
9e899dc
Compare
e70344e to
37f798c
Compare
There was a problem hiding this comment.
What is the reason of doing it like this instead of:
File f = Paths.get(uri).toFile();I think this part is not covered by any test 😢
There was a problem hiding this comment.
The background story here is tricky, so I'm not touching this logic until I can test it properly. We'll probably require Windows tests for it. See #4820
There was a problem hiding this comment.
Out of curiosity, but is it a thing to have more levels of nested jars?
There was a problem hiding this comment.
I have seen at least 1 user report at Spring Boot asking about a third level of nesting, but Spring Boot does not support that. It would definitely be possible to do it with a custom URLConnection handler. But until we find some real scenario, I would rather not implement it.
Also note that, while it should be relatively easy to add support for an arbitrary nesting depth, we would always set a limit to nesting for performance reasons since each additional level of nesting needs to decompress a stream.
37f798c to
4637775
Compare
4637775 to
6c58278
Compare
Motivation: `DocStringExtractor` and `XdsResourceReader` scan jars with shaded Reflections 0.9.11, which obtains the jar through `JarURLConnection.getJarFile()` with caching enabled and closes it after scanning. The closed `JarFile` can be the cached instance shared with a class loader. We observed rare `ClassNotFoundException`s in applications using `DocService` (Spring Boot 3.2.2 with Armeria 1.27.1 in production, and Spring Boot 3.5.15 with Armeria 1.34.2 in development). In the [reproduction](https://github.com/cprayer/armeria-docservice-jar-repro/tree/3a26552b77288b50e7492c4884fee8b4e649d263) (Armeria 1.42.0, Spring Boot 3.5.16), starting a `Server` with `DocService` once did not trigger the failure. Repeating `DocStringExtractor` scans while other threads read from the same jar made 173 of 174 `armeria-grpc` classes fail to load in most runs with a `ClassNotFoundException` without a cause, thrown through the same Spring Boot class loader frames as the observed failures. With this change, no class failed. A likely mechanism is a race with Spring Boot's nested jar loader. Closing a cached `JarFile` normally removes it from the cache, so later lookups open a new one. However, [`JarUrlConnection.connect()`](https://github.com/spring-projects/spring-boot/blob/v3.5.16/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/jar/JarUrlConnection.java) gets the cached `JarFile`, looks up the entry and then calls `cacheIfAbsent()`. If the scan closes the jar after the entry lookup and before `cacheIfAbsent()`, the closed `JarFile` is cached again and class loading from that jar fails until the JVM restarts. We did not observe this interleaving directly. This affects Spring Boot 3.2 through 4.1, which use the nested jar loader introduced in 3.2. Spring Boot 4.2 is not released yet, but [spring-boot#52246](spring-projects/spring-boot#52246) (4.2.0-RC1) looks up classes directly in nested jars, which appears to take class loading out of this race, although that was not its intent. On a 4.2.0-SNAPSHOT build, no class failed to load in the reproduction, but the scan itself still failed with `Zip file closed`. The same ownership problem was fixed in [Elastic APM](elastic/apm-agent-java#4543) (identified in [spring-boot#50925](spring-projects/spring-boot#50925 (comment))) and [Datadog](DataDog/dd-trace-java#6720). Reflections itself [disabled caching in its jar URL handler](ronmamo/reflections@e56cd65) in 0.9.12. The 0.9.11 pin was added for [a `JarFile.stream()` failure](ronmamo/reflections#279) caused by Spring Boot's loader, which was [fixed in Spring Boot 2.3.6.RELEASE](spring-projects/spring-boot#23821). Modifications: - Upgrade Reflections from 0.9.11 to 0.9.12 and remove the pin comment. - Adapt the gRPC utility to JDK predicates and the builder integration test to the updated Store API. - Add tests for shared `JarFile` ownership, filename filtering and xDS type discovery. - Note that Reflections 0.9.12 throws `ReflectionsException` when a scanner finds nothing. `DocStringExtractor` already guards against this, and `XdsResourceReader` always finds the xDS API types. Result: - Reflections scans no longer close a cached `JarFile` shared with a class loader. - The new regression test fails with `zip file closed` on 0.9.11 and passes on 0.9.12. - With the upgraded core, gRPC and xDS shaded jars in Spring Boot executable jars (Java 25), `JarFile.stream()`, `DocStringExtractor` and `XdsResourceReader` work and leave the shared jar open on Spring Boot 2.3.12.RELEASE, 2.7.18, 3.5.16 and 4.1.1. Spring Boot 2.3.4.RELEASE fails with `zip file closed` as expected ([local check](https://github.com/cprayer/armeria-docservice-jar-repro/blob/3a26552b77288b50e7492c4884fee8b4e649d263/VALIDATION.md), [Reflections-only compatibility repro](https://github.com/cprayer/armeria-docservice-jar-repro/tree/3a26552b77288b50e7492c4884fee8b4e649d263/compatibility)). - Targeted core, gRPC and xDS tests and checkstyle pass on JDK 25. AI-assisted: AI tools were used to assist with implementation, validation, and drafting this PR description.
What Does This Do
Motivation
Additional Notes
Jira ticket: APPSEC-51864
Original error reproduction
In the intermediate commits, you will find a test case reproducing the original error as well as a naive fix:
JarUrlConnectioncachingJarFileinstances. Under certain order of actions, we would close the sameJarFileinstance while the application is using it to read something from it, causing an exception. Now we force disable the use of caching on our side withsetUseCaches(false).A test failure here would look like this (e.g. if you revert the last commit in this PR, which contains the actual fix):
Benchmarks
Master, no full scanning of nested dependencies:
Naive solution, re-using Spring's JAR URLConnection handler:
Final state, with our own JAR reader: