ffang commented on PR #3752:
URL: https://github.com/apache/cxf/pull/3752#issuecomment-7114730129

   > Thanks for this, the shared-ownership approach looks good overall. A few 
things I spotted:
   > 
   > 1. **Ehcache detection can blow up** 
(`JCacheReplayCache.configuration()`): the check is on the provider's class 
name only. In OSGi with the optional `org.ehcache*` import not wired, or with 
Ehcache in the webapp and CXF in a shared lib, loading `EhcacheReplayExpiry` 
throws `NoClassDefFoundError`. The constructor only catches `Exception`, so 
building the replay cache fails. If CXF and the provider have separate copies 
of Ehcache, the provider may also reject the `Eh107Configuration` CXF builds. 
Could we guard it and fall back to the plain `MutableConfiguration`?
   > 2. **Non-Ehcache providers keep replay IDs 12h instead of 1h**: the 
fallback switched from a 1h creation expiry to `ModifiedExpiryPolicy(MAX_TTL)`. 
Expired entries only go away when the same ID is looked up again, so in an 
unbounded cache, unique nonces/timestamps pile up 12× longer. Worth keeping the 
1h bound for entries with no explicit expiry, or at least documenting it.
   > 3. **OAuth constructors leak on failure**: `JCacheOAuthDataProvider` (and 
the `grantCache` creation in `JCacheCodeDataProvider`) don't release anything 
if a `createCache` call throws, for example a `ClassCastException` on 
mismatched types. The manager count never reaches zero, so the disk store stays 
locked, which is the redeploy problem this PR fixes. The other classes in the 
PR already clean up in this case.
   > 4. **STS/XKMS weren't migrated** (pre-existing, but related): they now go 
through `JCacheSupport` → `JCacheUtils` reference counting, but their `close()` 
still calls `destroyCache` and `cacheManager.close()` directly. XKMS closes the 
provider's _default_ manager, which `JCacheUtils` says is never closed and 
which `JCacheReplayCache` relies on, so closing an XKMS client cache kills the 
replay caches.
   > 5. **Static strong refs in `JCacheUtils`** (nit): `CACHES`/`MANAGERS` hold 
managers and caches strongly in a shared class. If an app undeploys without 
releasing (STS/XKMS never do), its class loader stays pinned. Weak references 
might be safer.
   > 6. **NPE after close** (nit): `JCacheCodeDataProvider.close()` nulls 
`grantCache`, so a request still in flight gets an NPE instead of the old 
`IllegalStateException`. A `closed` flag like the parent's would avoid that.
   > 7. **Possibly flaky test** (nit): `testEhcacheEvictsAtEntryExpiry` uses a 
1s expiry and `Thread.sleep(1500)` on the real clock. It could fail on a slow 
CI box, and using `TestClock` would make it deterministic.
   
   Thanks @coheigea, all good points. Points 1–4, 6 and 7 are addressed in 
4502cead5c; on point 5 I'd suggest keeping strong references, see below.
   
   **1. Ehcache detection.** `JCacheReplayCache` now uses the Ehcache expiry 
only when `Eh107Configuration` and `org.ehcache.expiry.ExpiryPolicy` resolve to 
the same classes from CXF's and the provider's class loaders. Any 
`LinkageError`, `IllegalArgumentException` or `ClassCastException` while 
building it falls back to a plain `MutableConfiguration` with the default TTL. 
An existing cache that wasn't created with the Ehcache expiry is detected 
(`EhcacheReplayExpiry.isUsedBy`) and treated the same way. Tests: 
`testEhcacheDetection`, `testExistingCacheWithoutEhcacheExpiry`.
   
   **2. Non-Ehcache TTL.** Without the Ehcache per-entry expiry, entries are 
bounded by the 1h default TTL again, and `getMaxTTL()` reports that value, so 
the 12h bound only applies where entries actually expire at their own expiry. 
Test: `testDefaultTtlWithoutEhcacheExpiry`.
   
   **3. OAuth constructors.** `JCacheOAuthDataProvider` releases the caches it 
already created, and the manager, when a later `createCache` fails, and 
`JCacheCodeDataProvider` closes its parent when `grantCache` can't be created. 
Test: `testFailedConstructionReleasesManager`.
   
   **4. STS/XKMS.** Both now go through `JCacheUtils` for acquire and release. 
`JCacheIdentityCache` releases its cache and manager once (`closed` flag). 
`JCacheXKMSClientCache` releases only its own cache and no longer closes the 
provider's default manager, so closing it leaves the replay caches alone. 
Tests: `JCacheIdentityCacheLifecycleTest`, `JCacheXKMSClientCacheLifecycleTest` 
(`testCloseLeavesDefaultManagerOpen`).
   
   **5. Static strong refs.** I'd rather keep strong references here, for two 
reasons:
   - Weak keys wouldn't help: the map value (the manager) holds the cache, 
which keeps the key reachable, so the entries would never be cleared.
   - Weak values would be risky: a manager that is still in use, but not 
strongly held by the caller at that moment, could be dropped and then created a 
second time over the same store.
   
   The leak you describe came from STS and XKMS never releasing their caches. 
With point 4 they release them, so nothing is left behind on undeploy. If you 
still see a path that leaves a manager registered, I'm happy to look at it.
   
   **6. NPE after close.** `JCacheCodeDataProvider` no longer nulls 
`grantCache`; a `grantCacheReleased` flag keeps the release idempotent, so an 
in-flight request gets the cache's own `IllegalStateException` as before. Test: 
`testUseAfterClose`.
   
   **7. Flaky test.** Ehcache evicts on its own time source, so a test clock 
can't drive the eviction itself. `testEhcacheEvictsAtEntryExpiry` now polls 
with a 30s deadline instead of a fixed sleep. The new 
`testEhcacheExpiryDurations` checks the expiry durations Ehcache is given on 
the test clock, deterministically.
   
   The new tests fail on the previous head. The cache tests of the affected 
modules, `SamlTokenTest` and the reproducer attached to the JIRA all pass.


-- 
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]

Reply via email to