You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "oscerd (via GitHub)" <gi...@apache.org> on 2026/08/24 20:36:03 UTC
[PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
oscerd opened a new pull request, #739:
URL: https://github.com/apache/camel-karaf/pull/739
Fixes #734
## 1. `getDelegate()` could build the registry twice
```java
if (delegate == null) {
ensureTrackerOpen();
delegate = createRegistry();
}
return delegate;
```
`delegate` is `volatile`, which makes the *read* safe but does not make the
check and the assignment atomic. Two threads arriving together on first access
each ran `createRegistry()`; one result was returned and the other silently
discarded, along with anything registered on it in the interim.
Replaced with double-checked locking against the volatile field, so the
conversion hot path stays lock-free and first access happens exactly once.
`removedService` and `addingService` now take the same lock, so the delegate
cannot be stopped and nulled while another thread is in the middle of handing
it out.
## 2. Discarding the delegate was silent
Any single `TypeConverterLoader` going away discards the whole delegate.
`createRegistry()` replays the core converters plus the loaders the tracker
currently holds — it *cannot* replay converters added via `addTypeConverter` or
by a Blueprint bean implementing `TypeConverters`. Those vanish from a running
context with nothing logged above `trace`.
`removedService` now logs at WARN naming the bundle whose loader went away and
saying what the rebuild cannot restore. This does not fix the loss, but it stops
it being invisible — which was the actual complaint.
## What is deliberately *not* changed
The asymmetry with `addingService` is intentional and I have left it alone.
`d54f9a806` (#625, PR #684) specifically *reverted* invalidate-on-add back to
loading into the existing delegate, precisely to preserve programmatically-added
converters. Making the two paths symmetric would undo that fix.
Genuinely replaying programmatic registrations on rebuild means recording them
as they are added — a larger design change than this issue warrants. Happy to
open a separate issue if it is worth doing.
## Tests
`concurrentFirstAccessShouldBuildTheRegistryOnce` releases 16 threads onto
`getDelegate()` from a single latch and asserts both that every caller sees the
same instance and that `createRegistry()` ran exactly once (counted via a
subclass override).
I checked it actually catches the bug rather than just passing: against the
previous `getDelegate()` it fails **3 out of 3 runs** —
```
AssertionFailedError: every caller must see the same registry instance
==> expected: <.....@44cffc25>
but was: <.....@2a369e14>
```
— and passes 3 out of 3 with the fix, so it pins the behaviour rather than
being a flaky probe.
```
Tests run: 13, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS
```
(all 4 pre-existing `OsgiTypeConverterTest` cases, including
`addingServiceShouldLoadIntoExistingDelegate` which guards the #625 behaviour,
still pass.)
---
_Claude Code on behalf of Andrea Cosentino_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3851027125
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,15 +67,15 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
@Override
- public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
+ public synchronized Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
LOG.trace("AddingService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
TypeConverterLoader loader = bundleContext.getService(serviceReference);
if (loader != null) {
Review Comment:
`bundleContext.getService()` increments the framework use-count for this service reference. If `loader.load(delegate)` subsequently throws, `addingService()` propagates `RuntimeCamelException`. The `ServiceTracker` treats a throwing `addingService()` as "not tracked", so `removedService()` is never called for this reference and `bundleContext.ungetService()` is never invoked.
The use-count is permanently stuck above zero; the originating bundle cannot be cleanly uninstalled for the lifetime of the OSGi framework. Fix: wrap `loader.load()` in a try-catch and call `bundleContext.ungetService(serviceReference)` in the catch before re-throwing.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608057
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -173,27 +242,33 @@ public <T> T tryConvertTo(Class<T> type, Object value) {
@Override
public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
- getDelegate().addTypeConverter(toType, fromType, typeConverter);
+ register(new TypeConvertible<>(fromType, toType),
+ registry -> registry.addTypeConverter(toType, fromType, typeConverter));
}
@Override
public void addTypeConverters(Object typeConverters) {
- getDelegate().addTypeConverters(typeConverters);
+ register(typeConverters, registry -> registry.addTypeConverters(typeConverters));
}
@Override
public void addBulkTypeConverters(BulkTypeConverters bulkTypeConverters) {
- getDelegate().addBulkTypeConverters(bulkTypeConverters);
+ register(bulkTypeConverters, registry -> registry.addBulkTypeConverters(bulkTypeConverters));
}
@Override
- public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
- return getDelegate().removeTypeConverter(toType, fromType);
+ public synchronized boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
+ boolean removed = getDelegate().removeTypeConverter(toType, fromType);
+ // delete the matching registration rather than recording an inverse. An inverse would reproduce the same
+ // end state on replay, but it would also keep the removed converter - and its bundle's classloader -
+ // strongly reachable for the life of the context, and cost a no-op call on every future rebuild
+ programmaticRegistrations.remove(new TypeConvertible<>(fromType, toType));
+ return removed;
}
@Override
public void addFallbackTypeConverter(TypeConverter typeConverter, boolean canPromote) {
- getDelegate().addFallbackTypeConverter(typeConverter, canPromote);
+ register(typeConverter, registry -> registry.addFallbackTypeConverter(typeConverter, canPromote));
Review Comment:
**Replaying after the loader loop inverts fallback converter precedence, because `addFallbackTypeConverter` prepends.**
`CoreTypeConverterRegistry.addFallbackTypeConverter` (verified in bytecode) is:
```java
fallbackConverters.add(0, new FallbackTypeConverter(typeConverter, canPromote));
```
i.e. **last added == index 0 == tried first**. That is what `createRegistry()`'s "Just make sure we install the high ranking fallback converter at last" comment relies on: the highest-ranking `ServiceReference` sorts last, so its fallback lands at index 0.
`replayProgrammaticRegistrations(answer)` then runs *after* the entire loop, so any programmatic fallback is prepended in front of the high-ranking loader fallback that the sort deliberately put there.
In the live registry the opposite ordering held: a fallback registered at time T sat behind every loader that arrived after T (`addingService` -> `loader.load(delegate)` -> prepend). So a single loader unregistration silently flips which fallback wins for every unmatched conversion in the context. The field Javadoc's claim that the rebuilt registry is brought "back to the same state" does not hold for fallbacks.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3860502103
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -121,6 +169,8 @@ protected void doStart() throws Exception {
protected void doStop() throws Exception {
this.tracker.close();
this.trackerOpened = false;
+ // close() calls removedService for everything still tracked, this only makes the end state explicit
+ this.trackedLoaders.clear();
ServiceHelper.stopService(this.delegate);
this.delegate = null;
}
Review Comment:
**Blocking**, and a one-liner.
`trackedLoaders` is cleared here but `programmaticRegistrations` is not, so a stop/start cycle replays registrations from the previous lifecycle — including converters owned by bundles that are gone by the time the context comes back up.
Reproduced against this head: `start()` -> `addTypeConverter(String, Marker, tc)` -> `stop()` -> `start()` -> `getDelegate().lookup(String.class, Marker.class)` returns the converter from the previous lifecycle.
`ServiceSupport` permits stop/start, and `OsgiTypeConverter` is a context-scoped service, so this is reachable on a context restart. Clearing the list alongside `trackedLoaders` is enough, and it would be worth a test next to `removedServiceReleasesTheService`.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -173,27 +223,30 @@ public <T> T tryConvertTo(Class<T> type, Object value) {
@Override
public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
- getDelegate().addTypeConverter(toType, fromType, typeConverter);
+ register(registry -> registry.addTypeConverter(toType, fromType, typeConverter));
}
@Override
public void addTypeConverters(Object typeConverters) {
- getDelegate().addTypeConverters(typeConverters);
+ register(registry -> registry.addTypeConverters(typeConverters));
}
@Override
public void addBulkTypeConverters(BulkTypeConverters bulkTypeConverters) {
- getDelegate().addBulkTypeConverters(bulkTypeConverters);
+ register(registry -> registry.addBulkTypeConverters(bulkTypeConverters));
}
@Override
public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
- return getDelegate().removeTypeConverter(toType, fromType);
+ boolean removed = getDelegate().removeTypeConverter(toType, fromType);
+ // replayed as well, so a rebuild reproduces the sequence rather than resurrecting the converter
+ programmaticRegistrations.add(registry -> registry.removeTypeConverter(toType, fromType));
+ return removed;
}
Review Comment:
Two things here, both feeding the retention problem in the `programmaticRegistrations` thread.
**Appends an inverse instead of pruning.** Recording a *removal* rather than deleting the matching *add* means the removed converter stays strongly reachable forever — the add lambda still holds it. Replay reproduces the right end state, so the tests pass, but the object is never released. Deleting the entry would give the same end state and actually let go.
**Appends even when nothing was removed.** `removed` is computed and returned but not consulted. A `removeTypeConverter` for a pair that was never registered still grows the list and still costs a no-op call on every future rebuild.
Also, as on the `register()` thread: the `getDelegate()` call and the `add` are not atomic with respect to each other, so this has the same lost-registration window.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608100
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +97,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
+ // deliberately not synchronized: the tracker calls this from the framework's service event dispatch, and
+ // taking this instance's monitor here would put our lock on the far side of the framework's, which is the
+ // ordering that makes a lock inversion possible
@Override
public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
LOG.trace("AddingService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
TypeConverterLoader loader = bundleContext.getService(serviceReference);
if (loader != null) {
+ trackedLoaders.put(serviceReference, loader);
try {
LOG.debug("loading type converter from bundle: {}", serviceReference.getBundle().getSymbolicName());
- if (delegate != null) {
+ DefaultTypeConverter current = delegate;
Review Comment:
**`delegate` is read here without the monitor, so a loader arriving during a rebuild can end up recorded in `trackedLoaders` but present in no registry, with nothing left to trigger another rebuild.**
1. T1 in synchronized `getDelegate()`: `delegate == null`, enters `createRegistry()`, executes `new ArrayList<>(trackedLoaders.keySet())` — empty.
2. T2 (framework dispatch, `addingService` for `sr_A`): `trackedLoaders.put(sr_A, loader)` succeeds, then this line reads `delegate` as `null` because T1 has not assigned yet, so the `if (current != null)` branch is skipped and `load()` is never called.
3. T1 completes and publishes a registry that does not contain `sr_A`'s converters.
`sr_A` is in `trackedLoaders`, so no further service event fires and the next rebuild only happens on an unrelated unregistration. Until then every conversion `sr_A` provided fails with `NoTypeConversionAvailableException`.
Recording the loader and deciding whether to load it are two steps raced against a build that is atomic with neither. Note the pre-PR code had the same hole via `tracker.getServiceReferences()` (the tracker's `tracked` map is only populated after `addingService` returns), so this is not a regression — but it is in the method being rewritten, and the `trackedLoaders` map is now the place where it could actually be closed.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3851027127
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -100,8 +100,17 @@ public void modifiedService(ServiceReference<TypeConverterLoader> serviceReferen
}
@Override
- public void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
+ public synchronized void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
LOG.trace("RemovedService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
+ if (this.delegate != null) {
+ // the rebuild in createRegistry replays the core converters and the loaders the tracker still
Review Comment:
When `removedService()` stops the entire delegate and `createRegistry()` rebuilds it, only converters discovered via the `ServiceTracker` are replayed. Converters registered programmatically via `addTypeConverter()`, `addBulkTypeConverters()`, or `addFallbackTypeConverter()`, including Blueprint bean-registered converters, are silently dropped with the old delegate.
Routes relying on those converters will fail with `NoTypeConversionAvailableException` after any bundle uninstalls a `TypeConverterLoader`, with no diagnostic pointing to the root cause. The log warning is insufficient. Either:
- Maintain a separate replay list of programmatically registered converters and re-apply them in `createRegistry()`.
- Abandon full-delegate teardown in favour of partial removal (unloading only the converters contributed by the departing loader).
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -242,15 +251,26 @@ public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
}
public DefaultTypeConverter getDelegate() {
- if (delegate == null) {
- // ensure the tracker is open so we can discover TypeConverterLoader services
Review Comment:
The fast path reads `answer = delegate` (volatile) and, if non-null, skips the `synchronized` block and returns `answer` directly to the caller. However, a concurrent `removedService()` (holding `synchronized(this)`) can call `ServiceHelper.stopService(this.delegate)` and set `this.delegate = null` *after* Thread A's null-check but *before* Thread A uses the returned value. Thread A then calls conversion methods on a stopped converter, resulting in `IllegalStateException` or silent data loss.
DCL is only safe when the published object is truly immutable after publication. A delegate that can be stopped and replaced does not meet that bar. Consider always going through the lock in `getDelegate()` — type conversion is not so hot that the lock overhead is measurable compared to actual conversion work.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3851027125
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,15 +67,15 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
@Override
- public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
+ public synchronized Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
LOG.trace("AddingService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
TypeConverterLoader loader = bundleContext.getService(serviceReference);
if (loader != null) {
Review Comment:
**Finding 5 — OSGi use-count leaked when `loader.load()` throws.**
`bundleContext.getService()` increments the framework use-count for this service reference. If `loader.load(delegate)` subsequently throws, `addingService()` propagates `RuntimeCamelException`. The `ServiceTracker` treats a throwing `addingService()` as "not tracked", so `removedService()` is never called for this reference and `bundleContext.ungetService()` is never invoked.
The use-count is permanently stuck above zero; the originating bundle cannot be cleanly uninstalled for the lifetime of the OSGi framework. Fix: wrap `loader.load()` in a try-catch and call `bundleContext.ungetService(serviceReference)` in the catch before re-throwing.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -242,15 +251,26 @@ public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
}
public DefaultTypeConverter getDelegate() {
- if (delegate == null) {
- // ensure the tracker is open so we can discover TypeConverterLoader services
Review Comment:
**Finding 2 — DCL fast path can return a stopped delegate.**
The fast path reads `answer = delegate` (volatile) and, if non-null, skips the `synchronized` block and returns `answer` directly to the caller. However, a concurrent `removedService()` (holding `synchronized(this)`) can call `ServiceHelper.stopService(this.delegate)` and set `this.delegate = null` *after* Thread A's null-check but *before* Thread A uses the returned value. Thread A then calls conversion methods on a stopped converter, resulting in `IllegalStateException` or silent data loss.
DCL is only safe when the published object is truly immutable after publication. A delegate that can be stopped and replaced does not meet that bar. Consider always going through the lock in `getDelegate()` — type conversion is not so hot that the lock overhead is measurable compared to actual conversion work.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -242,15 +251,26 @@ public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
}
public DefaultTypeConverter getDelegate() {
- if (delegate == null) {
- // ensure the tracker is open so we can discover TypeConverterLoader services
- // before creating the registry - this is important because getDelegate() may be
- // called during doInit() (e.g. when to() eagerly creates endpoints) which happens
- // before doStart() where the tracker is normally opened
- ensureTrackerOpen();
- delegate = createRegistry();
+ DefaultTypeConverter answer = delegate;
+ if (answer == null) {
+ // double checked locking against the volatile field: getDelegate is on the conversion hot path,
+ // so the common case must stay lock free, but the check and the assignment together are not
+ // atomic - without the lock two threads racing on first access each build a registry, and
+ // whatever was registered on the one that loses is silently dropped
+ synchronized (this) {
+ answer = delegate;
+ if (answer == null) {
+ // ensure the tracker is open so we can discover TypeConverterLoader services
+ // before creating the registry - this is important because getDelegate() may be
+ // called during doInit() (e.g. when to() eagerly creates endpoints) which happens
+ // before doStart() where the tracker is normally opened
+ ensureTrackerOpen();
+ answer = createRegistry();
Review Comment:
**Finding 1 — ABBA lock-order inversion; deadlock under concurrent service events.**
`createRegistry()` is called here while holding `synchronized(this)`. Inside `createRegistry()`, it calls `tracker.getServiceReferences()` and `tracker.getService()`, which the Felix/Equinox `ServiceTracker` guards with its own internal `Tracked` monitor.
Meanwhile, the OSGi framework holds that same `Tracked` monitor when dispatching service events — it calls `addingService()` / `removedService()`, which this PR has made `synchronized(this)`.
Thread A: holds `synchronized(this)` → calls `tracker.getService()` → waits for `Tracked` monitor
Thread B: holds `Tracked` monitor → calls `addingService()` → waits for `synchronized(this)`
Classic ABBA deadlock. This lock order did not exist before this PR. Fix: snapshot the tracked services *outside* any lock (e.g. call `tracker.getTracked()` which returns a defensive copy, or move the `tracker.*` calls before entering `synchronized(this)`), then use the snapshot inside the lock.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -100,8 +100,17 @@ public void modifiedService(ServiceReference<TypeConverterLoader> serviceReferen
}
@Override
- public void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
+ public synchronized void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
LOG.trace("RemovedService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
+ if (this.delegate != null) {
+ // the rebuild in createRegistry replays the core converters and the loaders the tracker still
Review Comment:
**Finding 6 — Warning acknowledges the loss of programmatic converters but does not fix it.**
When `removedService()` stops the entire delegate and `createRegistry()` rebuilds it, only converters discovered via the `ServiceTracker` are replayed. Converters registered programmatically via `addTypeConverter()`, `addBulkTypeConverters()`, or `addFallbackTypeConverter()` — including Blueprint bean-registered converters — are silently dropped with the old delegate.
Routes relying on those converters will fail with `NoTypeConversionAvailableException` after any bundle uninstalls a `TypeConverterLoader`, with no diagnostic pointing to the root cause. The log warning is insufficient. Either:
- Maintain a separate replay list of programmatically registered converters and re-apply them in `createRegistry()`.
- Abandon full-delegate teardown in favour of partial removal (unloading only the converters contributed by the departing loader).
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608106
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
Review Comment:
**This invariant does not hold. Anchoring here because the comment states it; the calls that break it are `ensureTrackerOpen()` inside synchronized `getDelegate()` (line 329) and `loader.load(answer)` / `answer.init()` in this method.**
The comment here, plus the ones on `addingService` and `removedService`, justify unsynchronizing the callbacks and abandoning `tracker.getServiceReferences()` on the grounds that no framework call may happen under this monitor. But inside synchronized `getDelegate()`:
- `ensureTrackerOpen()` -> `ServiceTracker.open()` acquires framework service-registry locks **and synchronously dispatches `addingService` for every initial service**;
- `answer.init()` / `answer.loadCoreAndFastTypeConverters()` do bundle-wiring resource scanning;
- `loader.load(answer)` twelve lines below runs arbitrary third-party bundle code that may call back into the framework or into this same `OsgiTypeConverter`.
So the `this` -> framework-lock edge the redesign was meant to remove is still present, just relocated. A concrete shape: the framework dispatch thread runs `addingService` -> `loader.load(current)` where a `CamelContextAware` loader performs a conversion -> `getDelegate()` -> blocks on `this`, while another thread holds `this` inside `tracker.open()`.
The residual risk may well be acceptable, but the comments assert the mitigation is complete when it is partial, which is the part that will mislead the next reader.
_AI-generated review on behalf of JB Onofré_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3851027145
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -242,15 +251,26 @@ public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
}
public DefaultTypeConverter getDelegate() {
- if (delegate == null) {
- // ensure the tracker is open so we can discover TypeConverterLoader services
- // before creating the registry - this is important because getDelegate() may be
- // called during doInit() (e.g. when to() eagerly creates endpoints) which happens
- // before doStart() where the tracker is normally opened
- ensureTrackerOpen();
- delegate = createRegistry();
+ DefaultTypeConverter answer = delegate;
+ if (answer == null) {
+ // double checked locking against the volatile field: getDelegate is on the conversion hot path,
+ // so the common case must stay lock free, but the check and the assignment together are not
+ // atomic - without the lock two threads racing on first access each build a registry, and
+ // whatever was registered on the one that loses is silently dropped
+ synchronized (this) {
+ answer = delegate;
+ if (answer == null) {
+ // ensure the tracker is open so we can discover TypeConverterLoader services
+ // before creating the registry - this is important because getDelegate() may be
+ // called during doInit() (e.g. when to() eagerly creates endpoints) which happens
+ // before doStart() where the tracker is normally opened
+ ensureTrackerOpen();
+ answer = createRegistry();
Review Comment:
As mentioned before:
`createRegistry()` is called here while holding `synchronized(this)`. Inside `createRegistry()`, it calls `tracker.getServiceReferences()` and `tracker.getService()`, which the Felix/Equinox `ServiceTracker` guards with its own internal `Tracked` monitor.
Meanwhile, the OSGi framework holds that same `Tracked` monitor when dispatching service events — it calls `addingService()` / `removedService()`, which this PR has made `synchronized(this)`.
Thread A: holds `synchronized(this)` → calls `tracker.getService()` → waits for `Tracked` monitor
Thread B: holds `Tracked` monitor → calls `addingService()` → waits for `synchronized(this)`
Classic ABBA deadlock. This lock order did not exist before this PR. Fix: snapshot the tracked services *outside* any lock (e.g. call `tracker.getTracked()` which returns a defensive copy, or move the `tracker.*` calls before entering `synchronized(this)`), then use the snapshot inside the lock.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3860502080
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -112,6 +150,16 @@ public void removedService(ServiceReference<TypeConverterLoader> serviceReferenc
this.delegate = null;
Review Comment:
**Blocking**, though I would rather see it split out than folded in here — see the bottom.
This write is not under the monitor that `getDelegate()` now takes, so making `getDelegate()` `synchronized` buys mutual exclusion between *readers* only, never against the writer. The invalidation can be swallowed entirely:
```
A: getDelegate() takes `this`, delegate == null, enters createRegistry()
(loader L already loaded into the new registry)
B: removedService(L) -> stopService(this.delegate) // still null, no-op
-> this.delegate = null // no monitor held
A: delegate = <registry built with L in it>
```
I reproduced this by stalling at the end of `createRegistry()`: after `removedService` returns, `getDelegate()` hands back *the same instance* that was built with the removed loader in it. Assertion `assertSame(built, afterRemoval)` passes. So converters belonging to an unregistered bundle keep serving indefinitely, and no subsequent event will invalidate them.
This predates the PR — the old unsynchronized `if (delegate == null)` had the same lost write. But it sits squarely inside #734's point 1, and `synchronized getDelegate()` makes the window *wider*, not narrower: the monitor is now held across the full rebuild (core converter scan plus every `loader.load()`), so there is much more time for a `removedService` to land inside it.
An epoch counter would close it — read a generation before `createRegistry()`, re-check it before the assign, discard and retry if `removedService` bumped it meanwhile.
That said: this is the same underlying problem as the eager-stop race you raised on the `getDelegate()` thread — invalidation and stop both happening outside the monitor, to an instance callers already hold. I would rather have one follow-up issue covering both than see either bolted on here. Your call, but if you open that issue, put this in it and I will not hold the PR on it.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3860502088
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -57,6 +61,19 @@ public class OsgiTypeConverter extends ServiceSupport implements TypeConverter,
private CamelContext camelContext;
private final Injector injector;
private final ServiceTracker<TypeConverterLoader, Object> tracker;
+ /**
+ * The loaders the tracker has handed us, kept here rather than read back from the tracker: resolving them
+ * through the tracker inside {@link #createRegistry()} would mean calling into the ServiceTracker and the
+ * framework while holding this instance's monitor.
+ */
+ private final Map<ServiceReference<TypeConverterLoader>, TypeConverterLoader> trackedLoaders
+ = new ConcurrentHashMap<>();
+ /**
+ * Registrations made through this facade rather than by a {@link TypeConverterLoader}, in the order they were
+ * made, so a rebuilt registry can be brought back to the same state. Discarding the delegate would otherwise
+ * drop them with no way to get them back.
+ */
+ private final List<Consumer<TypeConverterRegistry>> programmaticRegistrations = new CopyOnWriteArrayList<>();
Review Comment:
**Blocking.** This list is unbounded and never pruned, which in OSGi means classloader retention.
Every registration ever made through the facade is retained for the life of the `CamelContext`, and each retained lambda strongly references the `TypeConverter` instance it captured — and therefore the contributing bundle's classloader. After that bundle is uninstalled, nothing here lets go. That is the leak shape this repo cares most about, and it is new in this PR: before, discarding the delegate did at least drop the references.
Reproduced: 500 `addTypeConverter`/`removeTypeConverter` pairs leave the list at **1000** entries, with the converter from pair 1 still strongly reachable.
Two knock-ons worth naming:
- **Rebuild cost grows monotonically.** Every rebuild replays the whole list, and `addTypeConverters(Object)` re-runs reflective `@Converter` scanning each time. Since #739 also holds the instance monitor across the rebuild, the container's conversion stall grows with the number of registrations the context has ever seen.
- **The list has no relationship to bundle lifecycle.** A Blueprint container refresh re-registers, appending duplicates rather than replacing.
A keyed collection — `Map<TypeConvertible<?,?>, Consumer<TypeConverterRegistry>>`, insertion-ordered — would fix growth, retention, and the `removeTypeConverter` ordering question in one move: a removal deletes the entry instead of appending an inverse. It does not solve `addTypeConverters(Object)`, which has no natural key, but that is a much smaller surface to think about.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -112,6 +150,16 @@ public void removedService(ServiceReference<TypeConverterLoader> serviceReferenc
this.delegate = null;
Review Comment:
**Blocking**, though I would rather see it split out than folded in here — see the bottom.
This write is not under the monitor that `getDelegate()` now takes, so making `getDelegate()` `synchronized` buys mutual exclusion between *readers* only, never against the writer. The invalidation can be swallowed entirely:
```
A: getDelegate() takes `this`, delegate == null, enters createRegistry()
(loader L already loaded into the new registry)
B: removedService(L) -> stopService(this.delegate) // still null, no-op
-> this.delegate = null // no monitor held
A: delegate = <registry built with L in it>
```
I reproduced this by stalling at the end of `createRegistry()`: after `removedService` returns, `getDelegate()` hands back *the same instance* that was built with the removed loader in it. Assertion `assertSame(built, afterRemoval)` passes. So converters belonging to an unregistered bundle keep serving indefinitely, and no subsequent event will invalidate them.
This predates the PR — the old unsynchronized `if (delegate == null)` had the same lost write. But it sits squarely inside #734's point 1, and `synchronized getDelegate()` makes the window *wider*, not narrower: the monitor is now held across the full rebuild (core converter scan plus every `loader.load()`), so there is much more time for a `removedService` to land inside it.
An epoch counter would close it — read a generation before `createRegistry()`, re-check it before the assign, discard and retry if `removedService` bumped it meanwhile.
That said: this is the same underlying problem as the eager-stop race you raised on the `getDelegate()` thread — invalidation and stop both happening outside the monitor, to an instance callers already hold. I would rather have one follow-up issue covering both than see either bolted on here. Your call, but if you open that issue, put this in it and I will not hold the PR on it.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +335,59 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
+ loader.load(answer);
+ } catch (Throwable t) {
+ throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
}
}
+ replayProgrammaticRegistrations(answer);
+
LOG.trace("Created TypeConverter: {}", answer);
return answer;
}
+ /**
+ * Re-applies everything that was registered through this facade rather than by a
+ * {@link TypeConverterLoader}, in the order it was originally applied.
+ */
+ private void replayProgrammaticRegistrations(DefaultTypeConverter registry) {
+ if (programmaticRegistrations.isEmpty()) {
+ return;
+ }
+ LOG.debug("Replaying {} programmatic registration(s) onto the rebuilt type converter registry",
+ programmaticRegistrations.size());
+ for (Consumer<TypeConverterRegistry> registration : programmaticRegistrations) {
+ registration.accept(registry);
+ }
+ }
+
+ /**
+ * Applies a registration to the current delegate and remembers it, so that discarding the delegate does not
+ * discard the registration with it.
+ */
+ private void register(Consumer<TypeConverterRegistry> registration) {
+ // apply first: a registration the delegate rejects is not one worth replaying. Note getDelegate() may
+ // build the registry here, which replays the list as it stands - this registration is added after, so
+ // it cannot be applied twice
+ registration.accept(getDelegate());
+ programmaticRegistrations.add(registration);
+ }
Review Comment:
**Blocking.** This reintroduces the bug the PR fixes, one level down.
`register()` is apply-then-record, and no monitor spans both halves. A rebuild landing between them loses the registration from the live registry even though it is sitting in the replay list:
```
A: addTypeConverter -> getDelegate() returns D1 -> stalls inside the apply
B: removedService -> delegate = null
C: getDelegate() -> builds D2, replays the list (r is not in it yet)
A: programmaticRegistrations.add(r)
=> r is in the replay list, but was only ever applied to the discarded D1
```
I reproduced this against this head with a registry whose `addTypeConverter` I could stall inside: after the sequence above, `d2.lookup(String.class, Marker.class)` returns `null`, and no further rebuild happens to correct it. So the converter is silently absent from the live registry — exactly the symptom `programmaticConverterSurvivesARegistryRebuild` is meant to rule out.
The comment here is right that the registration cannot be applied *twice*; the gap is that it can be applied *zero* times.
The window is narrow for `addTypeConverter`, but `addTypeConverters(Object)` does reflective `@Converter` scanning, and Blueprint bean registration runs concurrently with bundle lifecycle events, so it is not theoretical.
Fix is cheap, since `getDelegate()` is reentrant on the same monitor:
```java
private synchronized void register(Consumer<TypeConverterRegistry> registration) {
```
`removeTypeConverter` has the same split and needs the same treatment.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -121,6 +169,8 @@ protected void doStart() throws Exception {
protected void doStop() throws Exception {
this.tracker.close();
this.trackerOpened = false;
+ // close() calls removedService for everything still tracked, this only makes the end state explicit
+ this.trackedLoaders.clear();
ServiceHelper.stopService(this.delegate);
this.delegate = null;
}
Review Comment:
**Blocking**, and a one-liner.
`trackedLoaders` is cleared here but `programmaticRegistrations` is not, so a stop/start cycle replays registrations from the previous lifecycle — including converters owned by bundles that are gone by the time the context comes back up.
Reproduced against this head: `start()` -> `addTypeConverter(String, Marker, tc)` -> `stop()` -> `start()` -> `getDelegate().lookup(String.class, Marker.class)` returns the converter from the previous lifecycle.
`ServiceSupport` permits stop/start, and `OsgiTypeConverter` is a context-scoped service, so this is reachable on a context restart. Clearing the list alongside `trackedLoaders` is enough, and it would be worth a test next to `removedServiceReleasesTheService`.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -238,10 +291,13 @@ public TypeConverterExists getTypeConverterExists() {
@Override
public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
- getDelegate().setTypeConverterExists(typeConverterExists);
+ register(registry -> registry.setTypeConverterExists(typeConverterExists));
}
- public DefaultTypeConverter getDelegate() {
+ // fully synchronized rather than double checked: the delegate is not immutable after publication -
+ // removedService stops and replaces it - so a lock free read of the field buys a race for no real gain,
+ // conversion work dwarfing an uncontended monitor either way
+ public synchronized DefaultTypeConverter getDelegate() {
Review Comment:
Not blocking, but the invariant the safety argument rests on does not hold, so I would like the comment and the PR description to stop asserting it.
The PR body says there is "no call into the tracker or the framework left underneath this instance's monitor at all". There are several. `getDelegate()` is now `synchronized` and calls `ensureTrackerOpen()`, and `tracker.open()` does `bundleContext.addServiceListener(...)` and `bundleContext.getServiceReferences(...)`, then synchronously runs `trackInitial()` -> your own `addingService` -> `bundleContext.getService(...)` — all under `this`. Separately, `createRegistry()` runs foreign `loader.load()` under `this`.
No ABBA results, because the callbacks no longer take this monitor, and (per your disassembly, which I verified) `Tracked` is not held across the customizer either. So the code is *not* deadlock-prone. But "we removed the `tracker.*` calls from `createRegistry()`" and "nothing framework-facing runs under our monitor" are different claims, and only the first one is true. Holding `this` across arbitrary bundle `load()` is the same shape I objected to originally; it is now reached by a different route.
Second, on this comment specifically: "conversion work dwarfing an uncontended monitor" is true in steady state and misleading during a rebuild, which is the case that matters. While `createRegistry()` runs — core converter package scan plus every tracked loader's `load()` — every conversion in the container is blocked on this monitor, and per the `programmaticRegistrations` thread that duration grows over the life of the context.
I am fine with the tradeoff. Please just say what it is: all conversions serialize behind a rebuild, and the monitor is held across foreign bundle code.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -173,27 +223,30 @@ public <T> T tryConvertTo(Class<T> type, Object value) {
@Override
public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
- getDelegate().addTypeConverter(toType, fromType, typeConverter);
+ register(registry -> registry.addTypeConverter(toType, fromType, typeConverter));
}
@Override
public void addTypeConverters(Object typeConverters) {
- getDelegate().addTypeConverters(typeConverters);
+ register(registry -> registry.addTypeConverters(typeConverters));
}
@Override
public void addBulkTypeConverters(BulkTypeConverters bulkTypeConverters) {
- getDelegate().addBulkTypeConverters(bulkTypeConverters);
+ register(registry -> registry.addBulkTypeConverters(bulkTypeConverters));
}
@Override
public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
- return getDelegate().removeTypeConverter(toType, fromType);
+ boolean removed = getDelegate().removeTypeConverter(toType, fromType);
+ // replayed as well, so a rebuild reproduces the sequence rather than resurrecting the converter
+ programmaticRegistrations.add(registry -> registry.removeTypeConverter(toType, fromType));
+ return removed;
}
Review Comment:
Two things here, both feeding the retention problem in the `programmaticRegistrations` thread.
**Appends an inverse instead of pruning.** Recording a *removal* rather than deleting the matching *add* means the removed converter stays strongly reachable forever — the add lambda still holds it. Replay reproduces the right end state, so the tests pass, but the object is never released. Deleting the entry would give the same end state and actually let go.
**Appends even when nothing was removed.** `removed` is computed and returned but not consulted. A `removeTypeConverter` for a pair that was never registered still grows the list and still costs a no-op call on every future rebuild.
Also, as on the `register()` thread: the `getDelegate()` call and the `add` are not atomic with respect to each other, so this has the same lost-registration window.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/test/java/org/apache/camel/karaf/core/OsgiTypeConverterTest.java:
##########
@@ -17,7 +17,19 @@
package org.apache.camel.karaf.core;
import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.TypeConverter;
import org.apache.camel.spi.Injector;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
Review Comment:
Style: the `java.util.*` imports landed after the `org.apache.camel.*` block, splitting it in two. Everywhere else in this file and in `OsgiTypeConverter.java` the order is `java.*`, blank line, `org.*`. Please move these above `org.apache.camel.CamelContext`.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -203,7 +256,7 @@ public TypeConverter lookup(Class<?> toType, Class<?> fromType) {
@Override
public void setInjector(Injector injector) {
- getDelegate().setInjector(injector);
+ register(registry -> registry.setInjector(injector));
}
Review Comment:
Minor: `setInjector` / `setTypeConverterExists` / `setTypeConverterExistsLoggingLevel` are *state*, not events, and replaying them as ordered lambdas gets the ordering subtly wrong.
`createRegistry()` constructs `OsgiDefaultTypeConverter` with the constructor-time `injector` field, runs `init()` and `loadCoreAndFastTypeConverters()`, and only then replays. So a rebuilt registry loads its core converters under the *old* injector and swaps afterwards — not equivalent to a registry built fresh with the current one. `this.injector` is never updated by this setter either.
Still an improvement on losing them entirely, so not blocking. But holding them as plain fields and applying them at construction would be both simpler and correct, and would keep three entries out of the replay list.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +84,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
Review Comment:
Agreed on keeping this, and your narrower justification is the right one.
One nit while you are in here, pre-existing but now more reachable: if `tracker.open()` throws, `trackerOpened` stays `false`, but `ServiceTracker.open()` assigns `tracked` *before* calling `trackInitial()`, so the retry returns early and never re-runs `trackInitial()`. The initial loaders that had not been processed yet are then never tracked at all.
`addingService` can throw `RuntimeCamelException` out of `trackInitial()` when a loader's `load()` fails, so one bad loader at startup can silently strand the others. Worth a separate issue rather than growing this PR — flagging it because the method is in the diff.
_Claude Code on behalf of JB Onofré_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3860502131
##########
core/camel-core-osgi/src/test/java/org/apache/camel/karaf/core/OsgiTypeConverterTest.java:
##########
@@ -17,7 +17,19 @@
package org.apache.camel.karaf.core;
import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.TypeConverter;
import org.apache.camel.spi.Injector;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
Review Comment:
Style: the `java.util.*` imports landed after the `org.apache.camel.*` block, splitting it in two. Everywhere else in this file and in `OsgiTypeConverter.java` the order is `java.*`, blank line, `org.*`. Please move these above `org.apache.camel.CamelContext`.
_Claude Code on behalf of JB Onofré_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608047
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -238,10 +314,13 @@ public TypeConverterExists getTypeConverterExists() {
@Override
public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
- getDelegate().setTypeConverterExists(typeConverterExists);
+ register(TYPE_CONVERTER_EXISTS_KEY, registry -> registry.setTypeConverterExists(typeConverterExists));
}
- public DefaultTypeConverter getDelegate() {
+ // fully synchronized rather than double checked: the delegate is not immutable after publication -
+ // removedService stops and replaces it - so a lock free read of the field buys a race for no real gain,
+ // conversion work dwarfing an uncontended monitor either way
+ public synchronized DefaultTypeConverter getDelegate() {
Review Comment:
**Making `getDelegate()` fully synchronized turns the conversion hot path into a global serialization point, and blocks every conversion in the container for the full duration of a rebuild.**
`convertTo`, `tryConvertTo`, `mandatoryConvertTo`, `lookup`, `allowNull`, `size`, `getStatistics` and `getInjector` all funnel through here. Previously `delegate` was `volatile` and read without a lock, so the hot path was contention-free; now every conversion on every route thread contends one monitor (and biased locking is gone as of JDK 15, so it inflates).
The rebuild case is the sharper one: when a single `TypeConverterLoader` unregisters, the next `getDelegate()` holds this monitor across `init()` + `loadCoreAndFastTypeConverters()` (bundle resource scanning) + `loader.load()` for every tracked loader + the replay. Every conversion in the container blocks for that whole span. `registrationCannotInterleaveWithARebuild` demonstrates precisely this with its 2 s stall.
The comment's "conversion work dwarfing an uncontended monitor" only holds while uncontended. Keeping a volatile fast-path read and synchronizing only the build gives the same build-once guarantee at no hot-path cost:
```java
DefaultTypeConverter d = delegate;
if (d != null) {
return d;
}
synchronized (this) { ... }
```
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -238,10 +314,13 @@ public TypeConverterExists getTypeConverterExists() {
@Override
public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
- getDelegate().setTypeConverterExists(typeConverterExists);
+ register(TYPE_CONVERTER_EXISTS_KEY, registry -> registry.setTypeConverterExists(typeConverterExists));
}
- public DefaultTypeConverter getDelegate() {
+ // fully synchronized rather than double checked: the delegate is not immutable after publication -
+ // removedService stops and replaces it - so a lock free read of the field buys a race for no real gain,
+ // conversion work dwarfing an uncontended monitor either way
+ public synchronized DefaultTypeConverter getDelegate() {
Review Comment:
**Making `getDelegate()` fully synchronized turns the conversion hot path into a global serialization point, and blocks every conversion in the container for the full duration of a rebuild.**
`convertTo`, `tryConvertTo`, `mandatoryConvertTo`, `lookup`, `allowNull`, `size`, `getStatistics` and `getInjector` all funnel through here. Previously `delegate` was `volatile` and read without a lock, so the hot path was contention-free; now every conversion on every route thread contends one monitor (and biased locking is gone as of JDK 15, so it inflates).
The rebuild case is the sharper one: when a single `TypeConverterLoader` unregisters, the next `getDelegate()` holds this monitor across `init()` + `loadCoreAndFastTypeConverters()` (bundle resource scanning) + `loader.load()` for every tracked loader + the replay. Every conversion in the container blocks for that whole span. `registrationCannotInterleaveWithARebuild` demonstrates precisely this with its 2 s stall.
The comment's "conversion work dwarfing an uncontended monitor" only holds while uncontended. Keeping a volatile fast-path read and synchronizing only the build gives the same build-once guarantee at no hot-path cost:
```java
DefaultTypeConverter d = delegate;
if (d != null) {
return d;
}
synchronized (this) { ... }
```
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608040
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
Review Comment:
**`sr.getBundle().getSymbolicName()` is dereferenced eagerly inside the `try`, so a concurrently-stopping bundle turns a debug log into an aborted registry rebuild.**
`ServiceReference.getBundle()` returns `null` once the service is unregistered. This loop already anticipates that window — the `loader == null` guard four lines up is commented "unregistered between the snapshot and here" — and then dereferences `getBundle()` unconditionally for the same window. The argument is evaluated regardless of log level, so DEBUG being off does not help.
Because the call sits inside the `try { ... } catch (Throwable t)` that rethrows as `RuntimeCamelException`, the NPE does not just skip one loader: it escapes `createRegistry()` and `getDelegate()`, so a routine bundle stop makes the whole type converter registry unbuildable.
Worth noting the inconsistency: `removedService` guards this exact call (`serviceReference.getBundle() != null ? ... : serviceReference`), but this line and the one in `addingService` do not. The tests mask it because `setUp()` stubs `serviceReference.getBundle()` to a non-null mock.
_AI-generated review on behalf of JB Onofré_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "oscerd (via GitHub)" <gi...@apache.org>.
oscerd commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3870770117
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +335,59 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
+ loader.load(answer);
+ } catch (Throwable t) {
+ throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
}
}
+ replayProgrammaticRegistrations(answer);
+
LOG.trace("Created TypeConverter: {}", answer);
return answer;
}
+ /**
+ * Re-applies everything that was registered through this facade rather than by a
+ * {@link TypeConverterLoader}, in the order it was originally applied.
+ */
+ private void replayProgrammaticRegistrations(DefaultTypeConverter registry) {
+ if (programmaticRegistrations.isEmpty()) {
+ return;
+ }
+ LOG.debug("Replaying {} programmatic registration(s) onto the rebuilt type converter registry",
+ programmaticRegistrations.size());
+ for (Consumer<TypeConverterRegistry> registration : programmaticRegistrations) {
+ registration.accept(registry);
+ }
+ }
+
+ /**
+ * Applies a registration to the current delegate and remembers it, so that discarding the delegate does not
+ * discard the registration with it.
+ */
+ private void register(Consumer<TypeConverterRegistry> registration) {
+ // apply first: a registration the delegate rejects is not one worth replaying. Note getDelegate() may
+ // build the registry here, which replays the list as it stands - this registration is added after, so
+ // it cannot be applied twice
+ registration.accept(getDelegate());
+ programmaticRegistrations.add(registration);
+ }
Review Comment:
Fixed exactly as you suggested — `register()` is `private synchronized`, and `removeTypeConverter()` has the same treatment.
You are right that the comment defended the wrong half. It argued the registration cannot be applied *twice*; the hole was that it can be applied *zero* times. I have rewritten it to say what the monitor is actually for.
On testing it: `registrationCannotInterleaveWithARebuild` stalls inside an overridden `createRegistry()` and asserts a concurrent `addTypeConverter` cannot proceed, then that it lands on the live registry. Being straight about its limits — it passes against the previous head too, because the old `register()` still went through a `synchronized getDelegate()` and blocked there anyway. So it is a forward guard, not a regression test. Reproducing the real interleave needs a registry that can be stalled inside its own `addTypeConverter`, the way your throwaway did; I did not build one, and said so in the commit message rather than let the test imply more coverage than it has.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -112,6 +150,16 @@ public void removedService(ServiceReference<TypeConverterLoader> serviceReferenc
this.delegate = null;
Review Comment:
Split out as you asked: **#743**, which covers both this lost invalidation and the eager-stop race from the `getDelegate()` thread, since they are the same shape — mutation of an instance callers already hold, performed outside the monitor that publishes it — and a fix for one constrains the fix for the other.
Your epoch-counter sketch is in there, along with your reproduction (stalling at the end of `createRegistry()`, `assertSame(built, afterRemoval)` passing) and the point that #739 widens the window rather than narrowing it, since the monitor is now held across the whole rebuild.
Nothing about it is fixed in this PR.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -57,6 +61,19 @@ public class OsgiTypeConverter extends ServiceSupport implements TypeConverter,
private CamelContext camelContext;
private final Injector injector;
private final ServiceTracker<TypeConverterLoader, Object> tracker;
+ /**
+ * The loaders the tracker has handed us, kept here rather than read back from the tracker: resolving them
+ * through the tracker inside {@link #createRegistry()} would mean calling into the ServiceTracker and the
+ * framework while holding this instance's monitor.
+ */
+ private final Map<ServiceReference<TypeConverterLoader>, TypeConverterLoader> trackedLoaders
+ = new ConcurrentHashMap<>();
+ /**
+ * Registrations made through this facade rather than by a {@link TypeConverterLoader}, in the order they were
+ * made, so a rebuilt registry can be brought back to the same state. Discarding the delegate would otherwise
+ * drop them with no way to get them back.
+ */
+ private final List<Consumer<TypeConverterRegistry>> programmaticRegistrations = new CopyOnWriteArrayList<>();
Review Comment:
Agreed, and it is my regression — the replay list introduced it. Taken your suggestion.
`programmaticRegistrations` is now a keyed, insertion-ordered `LinkedHashMap<Object, Consumer<TypeConverterRegistry>>`. The key is what the registration is *about*:
- `TypeConvertible` for `addTypeConverter` and `addConverter`
- the contributed instance for `addBulkTypeConverters`, `addFallbackTypeConverter` and `addTypeConverters(Object)`
- a sentinel per setter, so repeated `setTypeConverterExists` calls replace rather than stack
That fixes all three things you named in one move: growth, retention, and the `removeTypeConverter` ordering question. It also answers the Blueprint refresh point — re-registering the same conversion now replaces the entry instead of appending a duplicate (`reRegisteringTheSameConversionReplaces`).
You are right that `addTypeConverters(Object)` has no natural key. Keying on the contributed object itself is the best available: a refresh that hands over an equal instance replaces, and one that hands over a genuinely different object is a genuinely different registration. It does not bound the reflective `@Converter` re-scan on rebuild, which stays proportional to the number of distinct contributed objects.
Tests: `addRemovePairsDoNotAccumulate` reproduces your figure — 1000 entries against the previous head, 0 now.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -173,27 +223,30 @@ public <T> T tryConvertTo(Class<T> type, Object value) {
@Override
public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
- getDelegate().addTypeConverter(toType, fromType, typeConverter);
+ register(registry -> registry.addTypeConverter(toType, fromType, typeConverter));
}
@Override
public void addTypeConverters(Object typeConverters) {
- getDelegate().addTypeConverters(typeConverters);
+ register(registry -> registry.addTypeConverters(typeConverters));
}
@Override
public void addBulkTypeConverters(BulkTypeConverters bulkTypeConverters) {
- getDelegate().addBulkTypeConverters(bulkTypeConverters);
+ register(registry -> registry.addBulkTypeConverters(bulkTypeConverters));
}
@Override
public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
- return getDelegate().removeTypeConverter(toType, fromType);
+ boolean removed = getDelegate().removeTypeConverter(toType, fromType);
+ // replayed as well, so a rebuild reproduces the sequence rather than resurrecting the converter
+ programmaticRegistrations.add(registry -> registry.removeTypeConverter(toType, fromType));
+ return removed;
}
Review Comment:
Both fixed, by the keyed collection in the `programmaticRegistrations` thread.
- **Appends an inverse instead of pruning** — `removeTypeConverter` now deletes the matching `TypeConvertible` entry. Same end state on replay, and the converter is actually released rather than staying reachable through the add lambda.
- **Appends even when nothing was removed** — a removal for a pair that was never registered now leaves the collection untouched. `removed` is still returned to the caller, it just no longer drives a retained entry. Test: `removingAConversionThatWasNeverRegisteredDoesNotAccumulate`.
The lost-registration window you noted here is closed by the same `synchronized` as on `register()` — `removeTypeConverter` is synchronized too, so its `getDelegate()` and its prune are one step.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -121,6 +169,8 @@ protected void doStart() throws Exception {
protected void doStop() throws Exception {
this.tracker.close();
this.trackerOpened = false;
+ // close() calls removedService for everything still tracked, this only makes the end state explicit
+ this.trackedLoaders.clear();
ServiceHelper.stopService(this.delegate);
this.delegate = null;
}
Review Comment:
Fixed, and thank you for reproducing it — I had not thought about the restart path at all.
`doStop()` now clears `programmaticRegistrations` alongside `trackedLoaders`, inside a `synchronized (this)` block so it cannot race a rebuild. The `tracker.close()` call stays outside the monitor, since that is a framework call and putting it under the lock is the thing we spent the first round removing.
Test added next to `removedServiceReleasesTheService` as you suggested: `restartDoesNotReplayThePreviousLifecycle` runs `start()` -> `addTypeConverter` -> `stop()` -> `start()` and asserts both that nothing is retained and that the lookup comes back `null`. It fails against the previous head.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +84,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
Review Comment:
Filed as **#744** rather than grown into this PR, as you suggested.
Good catch on the retry path — `ServiceTracker.open()` assigning `tracked` before calling `trackInitial()` means the flag-based retry is not just useless but actively misleading, since the second call returns as though it had succeeded. Worth noting in the issue that the fix probably is not the flag: a single loader throwing out of `trackInitial()` aborting the rest of the initial set is the more interesting half.
The `synchronized` stays here for the reason in the thread above.
_Claude Code on behalf of Andrea Cosentino_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "oscerd (via GitHub)" <gi...@apache.org>.
oscerd commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3853471973
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,15 +67,15 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
Review Comment:
Kept, but for a narrower reason than the original commit implied — you are right that the activator's own serialisation covers the common path.
`ensureTrackerOpen()` is reachable from both `doStart()` and `getDelegate()`, and `trackerOpened` is a check-then-act, so two threads arriving together could both call `tracker.open()`. It is cheap insurance rather than something the activator guarantees.
What changed is that it is now safe to hold: `addingService` / `removedService` no longer take this monitor, so `tracker.open()` firing `trackInitial()` synchronously from inside the lock cannot come back round and block on it.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,15 +67,15 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
@Override
- public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
+ public synchronized Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
Review Comment:
Removed.
`addingService` and `removedService` are both unsynchronized again, and the fix goes a step further than dropping the keyword: the loaders the tracker hands us are now kept in a `ConcurrentHashMap<ServiceReference, TypeConverterLoader>` field, maintained by the two callbacks. `createRegistry()` iterates a sorted snapshot of that map instead of calling `tracker.getServiceReferences()` / `tracker.getService()`, so there is no call into the tracker or the framework left underneath this instance's monitor at all.
Detail on the lock ordering itself in my reply on the `getDelegate()` thread.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -100,8 +100,17 @@ public void modifiedService(ServiceReference<TypeConverterLoader> serviceReferen
}
@Override
- public void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
+ public synchronized void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
Review Comment:
Removed, same change.
`removedService` now drops the reference from the tracked-loader map, ungets the service and invalidates the delegate — no monitor held, and no framework call under a lock.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,15 +67,15 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
@Override
- public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
+ public synchronized Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
LOG.trace("AddingService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
TypeConverterLoader loader = bundleContext.getService(serviceReference);
if (loader != null) {
Review Comment:
Fixed. `addingService` now wraps the load in a try/catch, drops the reference from the tracked-loader map and calls `bundleContext.ungetService(serviceReference)` before re-throwing.
While in there: the normal path leaked too. `removedService` never ungot the service either, so *every* tracked loader's use count stayed above zero for the lifetime of the framework, not only the ones whose `load()` threw. Since `addingService` is what takes the service — we are the customizer, so the tracker does not take it for us — releasing it is ours to do on both paths, and `removedService` now ungets as well. Flagging that explicitly rather than folding it in silently.
Both leaks predate this PR: `git diff main...` shows `addingService`'s body was byte-identical to `main` apart from the `synchronized` keyword.
Tests: `addingServiceReleasesTheServiceWhenLoadingFails` and `removedServiceReleasesTheService`, both of which fail against the previous commit.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -100,8 +100,17 @@ public void modifiedService(ServiceReference<TypeConverterLoader> serviceReferen
}
@Override
- public void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
+ public synchronized void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
LOG.trace("RemovedService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
+ if (this.delegate != null) {
+ // the rebuild in createRegistry replays the core converters and the loaders the tracker still
Review Comment:
Agreed — fixed properly rather than logged. I went with your first option, the replay list.
`OsgiTypeConverter` now records every registration made through the facade, in order, as a `CopyOnWriteArrayList<Consumer<TypeConverterRegistry>>`, and `createRegistry()` replays it onto the rebuilt registry after the loaders have been loaded. That covers `addTypeConverter`, `addTypeConverters`, `addBulkTypeConverters`, `addFallbackTypeConverter` and `addConverter` — and also `setInjector`, `setTypeConverterExists` and `setTypeConverterExistsLoggingLevel`, which were being lost in exactly the same way. `removeTypeConverter` is recorded too, so a rebuild reproduces the sequence rather than resurrecting a converter that was deliberately taken out.
I did not take the partial-removal route: unloading only the departing loader's converters needs a way to attribute a converter to its origin, which `TypeConverterRegistry` does not expose, and it would undo the invalidate-and-rebuild strategy that d54f9a806 (#625) introduced on purpose. Happy to revisit it as a separate change if you would rather have that shape.
The warning stays, but now says what actually happens — the registry is discarded and rebuilt on next use, with programmatic registrations replayed — and it is suppressed while the context is stopping, so `tracker.close()` no longer emits one per loader on shutdown.
Tests: `programmaticConverterSurvivesARegistryRebuild` and `removedTypeConverterIsNotResurrectedByARebuild`.
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -242,15 +251,26 @@ public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
}
public DefaultTypeConverter getDelegate() {
- if (delegate == null) {
- // ensure the tracker is open so we can discover TypeConverterLoader services
Review Comment:
Changed as asked — `getDelegate()` is now plainly `synchronized`, with no double-checked read.
One thing I would rather be straight about than let the thread read as closed: it does not actually fix the race you describe. The caller still receives the delegate and uses it after the monitor is released, so `removedService` can stop it a moment later just the same — always locking narrows the window, it does not close it. The race is also older than this PR; before it, `getDelegate()` read the field with no lock at all.
Closing it properly means not stopping the outgoing delegate eagerly: let the old instance keep serving in-flight conversions and let it go when nothing references it. That is a different change and I would rather not smuggle it into this one. Shall I open an issue for it?
_Claude Code on behalf of Andrea Cosentino_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -242,15 +251,26 @@ public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
}
public DefaultTypeConverter getDelegate() {
- if (delegate == null) {
- // ensure the tracker is open so we can discover TypeConverterLoader services
- // before creating the registry - this is important because getDelegate() may be
- // called during doInit() (e.g. when to() eagerly creates endpoints) which happens
- // before doStart() where the tracker is normally opened
- ensureTrackerOpen();
- delegate = createRegistry();
+ DefaultTypeConverter answer = delegate;
+ if (answer == null) {
+ // double checked locking against the volatile field: getDelegate is on the conversion hot path,
+ // so the common case must stay lock free, but the check and the assignment together are not
+ // atomic - without the lock two threads racing on first access each build a registry, and
+ // whatever was registered on the one that loses is silently dropped
+ synchronized (this) {
+ answer = delegate;
+ if (answer == null) {
+ // ensure the tracker is open so we can discover TypeConverterLoader services
+ // before creating the registry - this is important because getDelegate() may be
+ // called during doInit() (e.g. when to() eagerly creates endpoints) which happens
+ // before doStart() where the tracker is normally opened
+ ensureTrackerOpen();
+ answer = createRegistry();
Review Comment:
Fixed, and by removing the call rather than reordering it: `createRegistry()` no longer touches the `ServiceTracker`. `addingService` / `removedService` maintain a `ConcurrentHashMap<ServiceReference, TypeConverterLoader>`, and the rebuild iterates a sorted snapshot of that, so there is no `tracker.*` call inside `synchronized (this)` and no ordering to get wrong.
One correction for the record, because I went and checked rather than taking it on trust. I disassembled `org.osgi.util.tracker.AbstractTracked` 1.5.4:
- in `trackAdding`, `customizerAdding` is invoked at bytecode offset 8 — **before** the first `monitorenter` at offset 16;
- in `untrack`, `customizerRemoved` is invoked at offset 78 — **after** every `monitorexit`.
So the reference `ServiceTracker` deliberately does not hold the `Tracked` monitor across customizer callbacks, and that specific A/B cycle could not have formed.
That does not rescue the code as it stood, and the change stands: holding our own monitor across `tracker.getService()` and `bundleContext.getService()` is the wrong shape regardless, it made the class hostage to a `ServiceTracker` implementation detail, and `synchronized addingService` also meant every conversion in the container queued behind an arbitrary bundle's `load()` for as long as it took. I just did not want to leave a deadlock in the commit history that the implementation does not actually admit. Happy to be corrected if you know of a framework that does hold `Tracked` there.
_Claude Code on behalf of Andrea Cosentino_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608128
##########
core/camel-core-osgi/src/test/java/org/apache/camel/karaf/core/OsgiTypeConverterTest.java:
##########
@@ -110,4 +125,243 @@ void removedServiceShouldInvalidateDelegate() throws Exception {
var delegateAfter = osgiTypeConverter.getDelegate();
assertNotNull(delegateAfter);
}
+
+ @Test
+ void concurrentFirstAccessShouldBuildTheRegistryOnce() throws Exception {
+ int threads = 16;
+ AtomicInteger created = new AtomicInteger();
+ CountDownLatch startLine = new CountDownLatch(1);
+
+ OsgiTypeConverter counting = new OsgiTypeConverter(bundleContext, camelContext, injector) {
+ @Override
+ protected DefaultTypeConverter createRegistry() {
+ created.incrementAndGet();
+ return super.createRegistry();
+ }
+ };
+
+ ExecutorService pool = Executors.newFixedThreadPool(threads);
+ try {
+ List<Future<DefaultTypeConverter>> futures = new ArrayList<>();
+ for (int i = 0; i < threads; i++) {
+ futures.add(pool.submit(() -> {
+ startLine.await();
+ return counting.getDelegate();
+ }));
+ }
+ // release them all at once so they race on the null check
+ startLine.countDown();
+
+ DefaultTypeConverter first = futures.get(0).get(30, TimeUnit.SECONDS);
+ assertNotNull(first);
+ for (Future<DefaultTypeConverter> f : futures) {
+ assertSame(first, f.get(30, TimeUnit.SECONDS),
+ "every caller must see the same registry instance");
+ }
+ } finally {
+ pool.shutdownNow();
+ }
+
+ assertEquals(1, created.get(),
+ "the registry must be built exactly once, otherwise converters registered on a discarded"
+ + " instance are silently lost");
+ }
+
+ /** Marker source type, so the registered converter cannot collide with a core one. */
+ interface Marker {
+ }
+
+ @Test
+ void rebuiltRegistryReloadsTheTrackedLoadersWithoutAskingTheTracker() throws Exception {
+ // arrives before the registry exists, so addingService only records it
+ osgiTypeConverter.addingService(serviceReference);
+ verify(loader, never()).load(any());
+
+ DefaultTypeConverter first = osgiTypeConverter.getDelegate();
+
+ // createRegistry replayed it from the recorded loaders; it never called back into the ServiceTracker,
+ // which is what would put a framework call underneath this instance's monitor
+ verify(loader).load(first);
+ }
+
+ @Test
+ void programmaticConverterSurvivesARegistryRebuild() throws Exception {
+ DefaultTypeConverter before = osgiTypeConverter.getDelegate();
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ assertNotNull(before.lookup(String.class, Marker.class), "precondition: the converter is registered");
+
+ // one loader going away discards the whole registry
+ osgiTypeConverter.removedService(serviceReference, loader);
+ DefaultTypeConverter after = osgiTypeConverter.getDelegate();
+
+ assertNotSame(before, after, "the registry should have been rebuilt");
+ assertNotNull(after.lookup(String.class, Marker.class),
+ "a converter registered programmatically must be replayed onto the rebuilt registry, otherwise it"
+ + " disappears from a running context when any bundle unregisters a loader");
+ }
+
+ @Test
+ void removedTypeConverterIsNotResurrectedByARebuild() throws Exception {
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ osgiTypeConverter.removeTypeConverter(String.class, Marker.class);
+
+ osgiTypeConverter.removedService(serviceReference, loader);
+
+ assertNull(osgiTypeConverter.getDelegate().lookup(String.class, Marker.class),
+ "the replay must reproduce the sequence, not just the additions");
+ }
+
+ @Test
+ void addingServiceReleasesTheServiceWhenLoadingFails() throws Exception {
+ osgiTypeConverter.getDelegate();
+ doThrow(new RuntimeException("boom")).when(loader).load(any());
+
+ assertThrows(RuntimeCamelException.class, () -> osgiTypeConverter.addingService(serviceReference));
+
+ // a customizer that throws is treated as never tracked, so removedService will not run for this
+ // reference and nothing else would release the use count taken by addingService
+ verify(bundleContext).ungetService(serviceReference);
+ }
+
+ @Test
+ void removedServiceReleasesTheService() {
+ osgiTypeConverter.addingService(serviceReference);
+
+ osgiTypeConverter.removedService(serviceReference, loader);
+
+ verify(bundleContext).ungetService(serviceReference);
+ }
+
+ @Test
+ void addingServiceMustNotHoldTheInstanceMonitor() throws Exception {
+ // build first, so addingService takes the branch that calls into the loader
+ osgiTypeConverter.getDelegate();
+
+ CountDownLatch insideLoad = new CountDownLatch(1);
+ CountDownLatch releaseLoad = new CountDownLatch(1);
+ doAnswer(invocation -> {
+ insideLoad.countDown();
+ releaseLoad.await(30, TimeUnit.SECONDS);
+ return null;
+ }).when(loader).load(any());
+
+ ExecutorService pool = Executors.newFixedThreadPool(2);
+ try {
+ Future<?> adding = pool.submit(() -> osgiTypeConverter.addingService(serviceReference));
+ assertTrue(insideLoad.await(30, TimeUnit.SECONDS), "addingService should have reached loader.load");
+
+ // the framework calls addingService while it is dispatching a service event; if it took this
+ // instance's monitor, every conversion in the container would block behind an arbitrary bundle's
+ // loader for as long as that loader takes
+ Future<DefaultTypeConverter> reader = pool.submit(osgiTypeConverter::getDelegate);
+ assertNotNull(reader.get(10, TimeUnit.SECONDS),
+ "getDelegate must not be blocked by an in-flight addingService");
+
+ releaseLoad.countDown();
+ adding.get(30, TimeUnit.SECONDS);
+ } finally {
+ releaseLoad.countDown();
+ pool.shutdownNow();
+ }
+ }
+
+ @Test
+ void addRemovePairsDoNotAccumulate() {
+ osgiTypeConverter.getDelegate();
+
+ for (int i = 0; i < 500; i++) {
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ osgiTypeConverter.removeTypeConverter(String.class, Marker.class);
+ }
+
+ // an inverse-appending list would sit at 1000 here, and every converter it captured - and the classloader
+ // of the bundle that contributed it - would stay strongly reachable for the life of the context
+ assertEquals(0, osgiTypeConverter.programmaticRegistrationCount(),
+ "add/remove pairs must prune, not accumulate");
+ }
+
+ @Test
+ void reRegisteringTheSameConversionReplaces() {
+ osgiTypeConverter.getDelegate();
+
+ for (int i = 0; i < 10; i++) {
+ // what a Blueprint container refresh looks like: the same conversion registered again
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ }
+
+ assertEquals(1, osgiTypeConverter.programmaticRegistrationCount(),
+ "re-registering the same conversion must replace rather than append");
+ }
+
+ @Test
+ void removingAConversionThatWasNeverRegisteredDoesNotAccumulate() {
+ osgiTypeConverter.getDelegate();
+
+ osgiTypeConverter.removeTypeConverter(String.class, Marker.class);
+
+ assertEquals(0, osgiTypeConverter.programmaticRegistrationCount(),
+ "a removal for a pair that was never registered must not be retained");
+ }
+
+ @Test
+ void restartDoesNotReplayThePreviousLifecycle() throws Exception {
+ osgiTypeConverter.start();
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ assertNotNull(osgiTypeConverter.getDelegate().lookup(String.class, Marker.class));
+
+ osgiTypeConverter.stop();
+ osgiTypeConverter.start();
+
+ // the converters captured before the stop belong to bundles that may be gone by now
+ assertEquals(0, osgiTypeConverter.programmaticRegistrationCount());
+ assertNull(osgiTypeConverter.getDelegate().lookup(String.class, Marker.class),
+ "a stop/start cycle must not resurrect the previous lifecycle's registrations");
+ }
+
+ @Test
+ void registrationCannotInterleaveWithARebuild() throws Exception {
+ CountDownLatch insideBuild = new CountDownLatch(1);
+ CountDownLatch releaseBuild = new CountDownLatch(1);
+
+ OsgiTypeConverter stalling = new OsgiTypeConverter(bundleContext, camelContext, injector) {
+ @Override
+ protected DefaultTypeConverter createRegistry() {
+ DefaultTypeConverter built = super.createRegistry();
+ insideBuild.countDown();
+ try {
+ releaseBuild.await(30, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ return built;
+ }
+ };
+
+ ExecutorService pool = Executors.newFixedThreadPool(2);
+ try {
+ Future<DefaultTypeConverter> builder = pool.submit(stalling::getDelegate);
+ assertTrue(insideBuild.await(30, TimeUnit.SECONDS), "the rebuild should have started");
+
+ Future<?> registrar = pool.submit(() -> {
+ stalling.addTypeConverter(String.class, Marker.class, typeConverter);
+ return null;
+ });
+
+ // apply-and-record has to be one step against the rebuild. If it were not, this registration would be
+ // applied to the registry being discarded and only recorded afterwards, so the rebuilt one would
+ // neither have it applied nor replay it
+ assertThrows(TimeoutException.class, () -> registrar.get(2, TimeUnit.SECONDS),
Review Comment:
**Proving this by waiting 2 s for a `TimeoutException` is a `Thread.sleep` in disguise, and it passes vacuously if the second pool thread has not yet entered `addTypeConverter`.**
Two problems:
1. It adds a hard 2 s to every build and asserts a negative by elapsed time — the pattern the project's Awaitility guidance exists to eliminate ("flaky, slow, and non-deterministic").
2. It is not actually a guard. On a loaded CI box or with a cold thread pool, the `registrar` thread may not have reached the synchronized `register()` at all; the `get()` times out anyway and the test reports success even against an implementation where `register()` is **not** synchronized. The PR description already notes this test passes against its predecessor.
A deterministic positive assertion is available and the test almost makes it already: release the build, join both futures, then assert the registration is present in the published registry — which lines 358-359 do. Dropping the timing assertion loses nothing and removes the 2 s.
_AI-generated review on behalf of JB Onofré_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608136
##########
core/camel-core-osgi/src/test/java/org/apache/camel/karaf/core/OsgiTypeConverterTest.java:
##########
@@ -17,7 +17,20 @@
package org.apache.camel.karaf.core;
import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.TypeConverter;
import org.apache.camel.spi.Injector;
+import java.util.ArrayList;
Review Comment:
**The new `java.util` imports were inserted into the middle of the existing `org.apache.camel` group, splitting it in two.**
Lines 19-22 are `org.apache.camel.*`, lines 23-31 are `java.util.*`, then line 33 returns to `org.apache.camel.impl.converter.DefaultTypeConverter` and `org.apache.camel.spi.*`.
`OsgiTypeConverter.java` itself (lines 19-28) and the rest of the module put `java.*` first, then a blank line, then the third-party groups. There is no enforced formatter plugin here so nothing fails the build, but this guarantees a churn diff the next time anyone runs an IDE import organiser over the file.
##########
core/camel-core-osgi/src/test/java/org/apache/camel/karaf/core/OsgiTypeConverterTest.java:
##########
@@ -17,7 +17,20 @@
package org.apache.camel.karaf.core;
import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.TypeConverter;
import org.apache.camel.spi.Injector;
+import java.util.ArrayList;
Review Comment:
**The new `java.util` imports were inserted into the middle of the existing `org.apache.camel` group, splitting it in two.**
Lines 19-22 are `org.apache.camel.*`, lines 23-31 are `java.util.*`, then line 33 returns to `org.apache.camel.impl.converter.DefaultTypeConverter` and `org.apache.camel.spi.*`.
`OsgiTypeConverter.java` itself (lines 19-28) and the rest of the module put `java.*` first, then a blank line, then the third-party groups. There is no enforced formatter plugin here so nothing fails the build, but this guarantees a churn diff the next time anyone runs an IDE import organiser over the file.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608121
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -203,7 +278,7 @@ public TypeConverter lookup(Class<?> toType, Class<?> fromType) {
@Override
public void setInjector(Injector injector) {
Review Comment:
**`setInjector` only records a replay lambda and never updates the `injector` field, so every rebuilt registry is constructed with the constructor-time injector and loads its core converters before the replay corrects it.**
`createRegistry()` passes the `final` field `this.injector` to `new OsgiDefaultTypeConverter(...)`, then calls `answer.init()` and `answer.loadCoreAndFastTypeConverters()`, and only afterwards calls `replayProgrammaticRegistrations(answer)`. So after a rebuild, converters instantiated during core loading used the **old** injector; the configured one is swapped in only after the fact, and anything already created keeps the stale reference.
`TYPE_CONVERTER_EXISTS_KEY` and `TYPE_CONVERTER_EXISTS_LOGGING_LEVEL_KEY` have the same ordering problem, and it compounds the replay issue on `replayProgrammaticRegistrations`: a context configured `Override` or `Fail` has all its core and loader converters registered under the constructor defaults (`Ignore` / `DEBUG`), and the real policy only takes effect partway through the replay, in insertion order.
This is strictly better than pre-PR behaviour (where the setting was lost entirely), so not a blocker — but "brought back to the same state" is not what happens.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
+ loader.load(answer);
+ } catch (Throwable t) {
+ throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
}
}
+ replayProgrammaticRegistrations(answer);
+
LOG.trace("Created TypeConverter: {}", answer);
return answer;
}
+ /**
+ * Re-applies everything that was registered through this facade rather than by a
+ * {@link TypeConverterLoader}, in the order it was originally applied.
+ */
+ private void replayProgrammaticRegistrations(DefaultTypeConverter registry) {
+ if (programmaticRegistrations.isEmpty()) {
+ return;
+ }
+ LOG.debug("Replaying {} programmatic registration(s) onto the rebuilt type converter registry",
+ programmaticRegistrations.size());
+ for (Consumer<TypeConverterRegistry> registration : programmaticRegistrations.values()) {
+ registration.accept(registry);
+ }
+ }
+
+ /**
Review Comment:
**Two consecutive Javadoc blocks: this one is orphaned by the `/** */` for `programmaticRegistrationCount()` that follows, so `register()` ends up with no Javadoc and the surviving comment documents the wrong method.**
Java attaches only the last block before a declaration. This block — the load-bearing explanation of why apply-and-record has to be atomic, which both the commit message and the PR description lean on — becomes a dangling comment attached to nothing, and `register()` at line 421 is undocumented. The `{@link #getDelegate()}` reference in it is invisible to doclint too.
Looks like a rebase/squash artifact; moving this block down to immediately precede `register()` fixes it.
_AI-generated review on behalf of JB Onofré_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608021
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
+ loader.load(answer);
+ } catch (Throwable t) {
+ throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
}
}
+ replayProgrammaticRegistrations(answer);
Review Comment:
**The replay is outside any try/catch, so one throwing registration permanently bricks the registry — and with `TypeConverterExists.Fail` it is guaranteed to throw.**
I disassembled `CoreTypeConverterRegistry` from `camel-base-4.18.1.jar`:
- `addConverter(TypeConvertible, TypeConverter)` is a bare `converters.put(...)` — no duplicate check.
- `addTypeConverter(Class, Class, TypeConverter)` routes through `addOrReplaceTypeConverter()` -> `onTypeConverterExists()`, which returns `true` for `Override`, `false` for `Ignore`, and otherwise **throws `TypeConverterExistsException`**.
So:
1. Context is configured `typeConverterExists=Fail`.
2. A Blueprint bean calls `addTypeConverter(Foo, Bar, tc)` while only core converters are loaded — no conflict, applied and recorded.
3. Bundle X's `TypeConverterLoader` later registers `Bar -> Foo` via `addConverter()` (plain `put`, no check).
4. Any loader unregisters; the delegate is discarded.
5. Next `getDelegate()`: loaders load **first**, then this replay calls `addTypeConverter(Foo, Bar, tc)` -> existing converter found -> `TypeConverterExistsException` escapes `createRegistry()` and `getDelegate()`.
`delegate` stays `null`, so every `convertTo` / `tryConvertTo` / `lookup` in the container throws forever, and each retry re-throws because the offending entry is still in `programmaticRegistrations`. The loader loop just above is wrapped in `try/catch (Throwable)`; the replay is not.
Even on the default path this is lossy rather than fatal: the constructor sets `typeConverterExists = Ignore` and `typeConverterExistsLoggingLevel = LoggingLevel.DEBUG`, so the same collision silently drops the programmatic converter and logs only at DEBUG. `programmaticConverterSurvivesARegistryRebuild` does not catch it because the `Marker` interface is chosen specifically so the pair cannot collide with a core converter.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3860502075
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +335,59 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
+ loader.load(answer);
+ } catch (Throwable t) {
+ throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
}
}
+ replayProgrammaticRegistrations(answer);
+
LOG.trace("Created TypeConverter: {}", answer);
return answer;
}
+ /**
+ * Re-applies everything that was registered through this facade rather than by a
+ * {@link TypeConverterLoader}, in the order it was originally applied.
+ */
+ private void replayProgrammaticRegistrations(DefaultTypeConverter registry) {
+ if (programmaticRegistrations.isEmpty()) {
+ return;
+ }
+ LOG.debug("Replaying {} programmatic registration(s) onto the rebuilt type converter registry",
+ programmaticRegistrations.size());
+ for (Consumer<TypeConverterRegistry> registration : programmaticRegistrations) {
+ registration.accept(registry);
+ }
+ }
+
+ /**
+ * Applies a registration to the current delegate and remembers it, so that discarding the delegate does not
+ * discard the registration with it.
+ */
+ private void register(Consumer<TypeConverterRegistry> registration) {
+ // apply first: a registration the delegate rejects is not one worth replaying. Note getDelegate() may
+ // build the registry here, which replays the list as it stands - this registration is added after, so
+ // it cannot be applied twice
+ registration.accept(getDelegate());
+ programmaticRegistrations.add(registration);
+ }
Review Comment:
**Blocking.** This reintroduces the bug the PR fixes, one level down.
`register()` is apply-then-record, and no monitor spans both halves. A rebuild landing between them loses the registration from the live registry even though it is sitting in the replay list:
```
A: addTypeConverter -> getDelegate() returns D1 -> stalls inside the apply
B: removedService -> delegate = null
C: getDelegate() -> builds D2, replays the list (r is not in it yet)
A: programmaticRegistrations.add(r)
=> r is in the replay list, but was only ever applied to the discarded D1
```
I reproduced this against this head with a registry whose `addTypeConverter` I could stall inside: after the sequence above, `d2.lookup(String.class, Marker.class)` returns `null`, and no further rebuild happens to correct it. So the converter is silently absent from the live registry — exactly the symptom `programmaticConverterSurvivesARegistryRebuild` is meant to rule out.
The comment here is right that the registration cannot be applied *twice*; the gap is that it can be applied *zero* times.
The window is narrow for `addTypeConverter`, but `addTypeConverters(Object)` does reflective `@Converter` scanning, and Blueprint bean registration runs concurrently with bundle lifecycle events, so it is not theoretical.
Fix is cheap, since `getDelegate()` is reentrant on the same monitor:
```java
private synchronized void register(Consumer<TypeConverterRegistry> registration) {
```
`removeTypeConverter` has the same split and needs the same treatment.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3871545734
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +84,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
Review Comment:
👍
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre merged PR #739:
URL: https://github.com/apache/camel-karaf/pull/739
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608067
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -173,27 +242,33 @@ public <T> T tryConvertTo(Class<T> type, Object value) {
@Override
public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
- getDelegate().addTypeConverter(toType, fromType, typeConverter);
+ register(new TypeConvertible<>(fromType, toType),
+ registry -> registry.addTypeConverter(toType, fromType, typeConverter));
}
@Override
public void addTypeConverters(Object typeConverters) {
- getDelegate().addTypeConverters(typeConverters);
+ register(typeConverters, registry -> registry.addTypeConverters(typeConverters));
Review Comment:
**The keyed map only prunes the `addTypeConverter` / `addConverter` pair. `addTypeConverters`, `addBulkTypeConverters` and `addFallbackTypeConverter` are keyed by contributed-instance identity and have no removal API, so the map still grows without bound and still pins bundle classloaders.**
`removeTypeConverter` is the only pruning path and it only deletes a `TypeConvertible` key. `TypeConverterRegistry` exposes no `removeTypeConverters` / `removeFallbackTypeConverter` / `removeBulkTypeConverters`, so entries recorded here (and at the `addBulkTypeConverters` / `addFallbackTypeConverter` calls below) live until `doStop()`.
That breaks the stated rationale for keying on the affected path. "Re-registering replaces rather than appends, which is what a Blueprint container refresh does" is true for `addTypeConverter`, but a container refresh instantiates a **new bean**, so `addTypeConverters(newBeanInstance)` hashes to a fresh identity key and appends. 50 refreshes of a bundle contributing a `TypeConverters` bean leave 50 entries, each strongly holding the bean and through it the classloader of a bundle that may already be uninstalled — the leak shape the review flagged, on the path the PR description names as its motivating case. `addRemovePairsDoNotAccumulate` only exercises `addTypeConverter` / `removeTypeConverter`.
Secondary point: all three share one untyped key space, and `BulkTypeConverters extends Ordered, TypeConverter`, so the same instance passed to both `addBulkTypeConverters` and `addFallbackTypeConverter` collides and one registration is dropped from the replay. (`CoreTypeConverterRegistry.addBulkTypeConverters` is also a bare `return` in 4.18, so recording that one is dead weight either way.)
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608029
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -99,9 +138,21 @@ public Object addingService(ServiceReference<TypeConverterLoader> serviceReferen
public void modifiedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
}
+ // not synchronized, for the same reason as addingService
@Override
public void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
LOG.trace("RemovedService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
+ trackedLoaders.remove(serviceReference);
+ // we took the service in addingService, so releasing it is ours to do
+ ungetQuietly(serviceReference);
+ if (this.delegate != null && !isStopping() && !isStopped()) {
Review Comment:
**`!isStopped()` also suppresses this warning in every pre-`STARTED` state, so the diagnostic #734 asked for is missing exactly in the lazy-init window this class documents.**
From `BaseService` (camel-api 4.18):
```java
public boolean isStopped() {
return status < STARTING || status >= STOPPED;
}
```
with `NEW = 0, BUILT = 1, INITIALIZING = 2, INITIALIZED = 3, STARTING = 4 ... STOPPED = 9 ... FAILED = 12`.
So `isStopped()` is `true` for `NEW`, `BUILT`, `INITIALIZING`, `INITIALIZED` and `FAILED` — not just after a stop. `getDelegate()`'s own comment below says it "may be called during `doInit()` ... which happens before `doStart()`". An `OsgiTypeConverter` that is already serving conversions but has not reached `STARTED` will therefore discard its registry on a loader unregistration with **no log line at all**, which is the invisibility this PR exists to remove.
`isStopping()` alone already achieves the stated goal (no line-per-loader on `tracker.close()`, since `stop()` sets `status = STOPPING` before calling `doStop()`). The `&& !isStopped()` half is what breaks it; if a post-stop guard is also wanted, `status != STOPPED` is the narrow form.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3850720591
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,15 +67,15 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
Review Comment:
I'm not sure it's actually required as the bundle activator is itself sync.
It's not a big deal to add this, but not sure it will actually do much.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,15 +67,15 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
@Override
- public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
+ public synchronized Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
Review Comment:
We should not use `synchronized` here: `createRegistry()` calls `tracker.getServiceReference()` and `tracker.getService()` while holding `this`, but the `ServiceTracker` holds its own `Tracked` monitor when it calls `addingService()`/`removedService()`.
We should either not use `synchronized` here or snapshot the tracked services before entering the lock.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -100,8 +100,17 @@ public void modifiedService(ServiceReference<TypeConverterLoader> serviceReferen
}
@Override
- public void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
+ public synchronized void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
Review Comment:
Same here, `synchronized` should not be used on `removedService()`, else we can introduce a deadlock.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "oscerd (via GitHub)" <gi...@apache.org>.
oscerd commented on PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#issuecomment-5421112699
@jbonofre thanks — all seven threads are answered in `2c0676e26`, and the PR description has been rewritten to match where the change now stands rather than where it started.
Summary of what moved:
- **Callback locking** — `synchronized` is off `addingService` / `removedService`, and `createRegistry()` no longer calls the `ServiceTracker` at all: the callbacks maintain a `ConcurrentHashMap<ServiceReference, TypeConverterLoader>` and the rebuild iterates a sorted snapshot of it, so nothing calls into the tracker or the framework under this instance's monitor.
- **`getDelegate()`** — plainly `synchronized`, no double-checked read.
- **Use count** — ungets on the failure path, and on the normal `removedService` path, which leaked for every tracked loader rather than only the throwing ones.
- **Converter loss** — fixed rather than logged, via the replay list you suggested.
Two of the replies push back a little rather than just saying "done", so they are worth a look before you re-review: I could not reproduce the `Tracked` lock inversion against the reference `ServiceTracker` bytecode (the fix still stands, for other reasons), and always-locking `getDelegate()` does not actually close the stop-after-return race — I would rather say that than let the thread read as resolved. Details in the two threads.
I have left all seven unresolved for you to close.
**CI:** green on `2c0676e26`. The first run had one failure, `CamelQuartzITest.testResultMock` — `Expected: <1> but was: <2>` — which is a pre-existing flake rather than anything in this change: the route's cron is `0/1+*+*+*+*+?`, one trigger per second, against a mock asserting exactly one message, and Java 21 passed on the same commit while Java 17 failed. A re-run of the failed job went green with no code change. Happy to open a separate issue for that test.
_Claude Code on behalf of Andrea Cosentino_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3860502088
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -57,6 +61,19 @@ public class OsgiTypeConverter extends ServiceSupport implements TypeConverter,
private CamelContext camelContext;
private final Injector injector;
private final ServiceTracker<TypeConverterLoader, Object> tracker;
+ /**
+ * The loaders the tracker has handed us, kept here rather than read back from the tracker: resolving them
+ * through the tracker inside {@link #createRegistry()} would mean calling into the ServiceTracker and the
+ * framework while holding this instance's monitor.
+ */
+ private final Map<ServiceReference<TypeConverterLoader>, TypeConverterLoader> trackedLoaders
+ = new ConcurrentHashMap<>();
+ /**
+ * Registrations made through this facade rather than by a {@link TypeConverterLoader}, in the order they were
+ * made, so a rebuilt registry can be brought back to the same state. Discarding the delegate would otherwise
+ * drop them with no way to get them back.
+ */
+ private final List<Consumer<TypeConverterRegistry>> programmaticRegistrations = new CopyOnWriteArrayList<>();
Review Comment:
**Blocking.** This list is unbounded and never pruned, which in OSGi means classloader retention.
Every registration ever made through the facade is retained for the life of the `CamelContext`, and each retained lambda strongly references the `TypeConverter` instance it captured — and therefore the contributing bundle's classloader. After that bundle is uninstalled, nothing here lets go. That is the leak shape this repo cares most about, and it is new in this PR: before, discarding the delegate did at least drop the references.
Reproduced: 500 `addTypeConverter`/`removeTypeConverter` pairs leave the list at **1000** entries, with the converter from pair 1 still strongly reachable.
Two knock-ons worth naming:
- **Rebuild cost grows monotonically.** Every rebuild replays the whole list, and `addTypeConverters(Object)` re-runs reflective `@Converter` scanning each time. Since #739 also holds the instance monitor across the rebuild, the container's conversion stall grows with the number of registrations the context has ever seen.
- **The list has no relationship to bundle lifecycle.** A Blueprint container refresh re-registers, appending duplicates rather than replacing.
A keyed collection — `Map<TypeConvertible<?,?>, Consumer<TypeConverterRegistry>>`, insertion-ordered — would fix growth, retention, and the `removeTypeConverter` ordering question in one move: a removal deletes the entry instead of appending an inverse. It does not solve `addTypeConverters(Object)`, which has no natural key, but that is a much smaller surface to think about.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -238,10 +291,13 @@ public TypeConverterExists getTypeConverterExists() {
@Override
public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
- getDelegate().setTypeConverterExists(typeConverterExists);
+ register(registry -> registry.setTypeConverterExists(typeConverterExists));
}
- public DefaultTypeConverter getDelegate() {
+ // fully synchronized rather than double checked: the delegate is not immutable after publication -
+ // removedService stops and replaces it - so a lock free read of the field buys a race for no real gain,
+ // conversion work dwarfing an uncontended monitor either way
+ public synchronized DefaultTypeConverter getDelegate() {
Review Comment:
Not blocking, but the invariant the safety argument rests on does not hold, so I would like the comment and the PR description to stop asserting it.
The PR body says there is "no call into the tracker or the framework left underneath this instance's monitor at all". There are several. `getDelegate()` is now `synchronized` and calls `ensureTrackerOpen()`, and `tracker.open()` does `bundleContext.addServiceListener(...)` and `bundleContext.getServiceReferences(...)`, then synchronously runs `trackInitial()` -> your own `addingService` -> `bundleContext.getService(...)` — all under `this`. Separately, `createRegistry()` runs foreign `loader.load()` under `this`.
No ABBA results, because the callbacks no longer take this monitor, and (per your disassembly, which I verified) `Tracked` is not held across the customizer either. So the code is *not* deadlock-prone. But "we removed the `tracker.*` calls from `createRegistry()`" and "nothing framework-facing runs under our monitor" are different claims, and only the first one is true. Holding `this` across arbitrary bundle `load()` is the same shape I objected to originally; it is now reached by a different route.
Second, on this comment specifically: "conversion work dwarfing an uncontended monitor" is true in steady state and misleading during a rebuild, which is the case that matters. While `createRegistry()` runs — core converter package scan plus every tracked loader's `load()` — every conversion in the container is blocked on this monitor, and per the `programmaticRegistrations` thread that duration grows over the life of the context.
I am fine with the tradeoff. Please just say what it is: all conversions serialize behind a rebuild, and the monitor is held across foreign bundle 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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3860502109
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -238,10 +291,13 @@ public TypeConverterExists getTypeConverterExists() {
@Override
public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
- getDelegate().setTypeConverterExists(typeConverterExists);
+ register(registry -> registry.setTypeConverterExists(typeConverterExists));
}
- public DefaultTypeConverter getDelegate() {
+ // fully synchronized rather than double checked: the delegate is not immutable after publication -
+ // removedService stops and replaces it - so a lock free read of the field buys a race for no real gain,
+ // conversion work dwarfing an uncontended monitor either way
+ public synchronized DefaultTypeConverter getDelegate() {
Review Comment:
Not blocking, but the invariant the safety argument rests on does not hold, so I would like the comment and the PR description to stop asserting it.
The PR body says there is "no call into the tracker or the framework left underneath this instance's monitor at all". There are several. `getDelegate()` is now `synchronized` and calls `ensureTrackerOpen()`, and `tracker.open()` does `bundleContext.addServiceListener(...)` and `bundleContext.getServiceReferences(...)`, then synchronously runs `trackInitial()` -> your own `addingService` -> `bundleContext.getService(...)` — all under `this`. Separately, `createRegistry()` runs foreign `loader.load()` under `this`.
No ABBA results, because the callbacks no longer take this monitor, and (per your disassembly, which I verified) `Tracked` is not held across the customizer either. So the code is *not* deadlock-prone. But "we removed the `tracker.*` calls from `createRegistry()`" and "nothing framework-facing runs under our monitor" are different claims, and only the first one is true. Holding `this` across arbitrary bundle `load()` is the same shape I objected to originally; it is now reached by a different route.
Second, on this comment specifically: "conversion work dwarfing an uncontended monitor" is true in steady state and misleading during a rebuild, which is the case that matters. While `createRegistry()` runs — core converter package scan plus every tracked loader's `load()` — every conversion in the container is blocked on this monitor, and per the `programmaticRegistrations` thread that duration grows over the life of the context.
I am fine with the tradeoff. Please just say what it is: all conversions serialize behind a rebuild, and the monitor is held across foreign bundle code.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -203,7 +256,7 @@ public TypeConverter lookup(Class<?> toType, Class<?> fromType) {
@Override
public void setInjector(Injector injector) {
- getDelegate().setInjector(injector);
+ register(registry -> registry.setInjector(injector));
}
Review Comment:
Minor: `setInjector` / `setTypeConverterExists` / `setTypeConverterExistsLoggingLevel` are *state*, not events, and replaying them as ordered lambdas gets the ordering subtly wrong.
`createRegistry()` constructs `OsgiDefaultTypeConverter` with the constructor-time `injector` field, runs `init()` and `loadCoreAndFastTypeConverters()`, and only then replays. So a rebuilt registry loads its core converters under the *old* injector and swaps afterwards — not equivalent to a registry built fresh with the current one. `this.injector` is never updated by this setter either.
Still an improvement on losing them entirely, so not blocking. But holding them as plain fields and applying them at construction would be both simpler and correct, and would keep three entries out of the replay list.
_Claude Code on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +84,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
Review Comment:
Agreed on keeping this, and your narrower justification is the right one.
One nit while you are in here, pre-existing but now more reachable: if `tracker.open()` throws, `trackerOpened` stays `false`, but `ServiceTracker.open()` assigns `tracked` *before* calling `trackInitial()`, so the retry returns early and never re-runs `trackInitial()`. The initial loaders that had not been processed yet are then never tracked at all.
`addingService` can throw `RuntimeCamelException` out of `trackInitial()` when a loader's `load()` fails, so one bad loader at startup can silently strand the others. Worth a separate issue rather than growing this PR — flagging it because the method is in the diff.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608021
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
+ loader.load(answer);
+ } catch (Throwable t) {
+ throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
}
}
+ replayProgrammaticRegistrations(answer);
Review Comment:
**The replay is outside any try/catch, so one throwing registration permanently bricks the registry — and with `TypeConverterExists.Fail` it is guaranteed to throw.**
I disassembled `CoreTypeConverterRegistry` from `camel-base-4.18.1.jar`:
- `addConverter(TypeConvertible, TypeConverter)` is a bare `converters.put(...)` — no duplicate check.
- `addTypeConverter(Class, Class, TypeConverter)` routes through `addOrReplaceTypeConverter()` -> `onTypeConverterExists()`, which returns `true` for `Override`, `false` for `Ignore`, and otherwise **throws `TypeConverterExistsException`**.
So:
1. Context is configured `typeConverterExists=Fail`.
2. A Blueprint bean calls `addTypeConverter(Foo, Bar, tc)` while only core converters are loaded — no conflict, applied and recorded.
3. Bundle X's `TypeConverterLoader` later registers `Bar -> Foo` via `addConverter()` (plain `put`, no check).
4. Any loader unregisters; the delegate is discarded.
5. Next `getDelegate()`: loaders load **first**, then this replay calls `addTypeConverter(Foo, Bar, tc)` -> existing converter found -> `TypeConverterExistsException` escapes `createRegistry()` and `getDelegate()`.
`delegate` stays `null`, so every `convertTo` / `tryConvertTo` / `lookup` in the container throws forever, and each retry re-throws because the offending entry is still in `programmaticRegistrations`. The loader loop just above is wrapped in `try/catch (Throwable)`; the replay is not.
Even on the default path this is lossy rather than fatal: the constructor sets `typeConverterExists = Ignore` and `typeConverterExistsLoggingLevel = LoggingLevel.DEBUG`, so the same collision silently drops the programmatic converter and logs only at DEBUG. `programmaticConverterSurvivesARegistryRebuild` does not catch it because the `Marker` interface is chosen specifically so the pair cannot collide with a core converter.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
Review Comment:
**`sr.getBundle().getSymbolicName()` is dereferenced eagerly inside the `try`, so a concurrently-stopping bundle turns a debug log into an aborted registry rebuild.**
`ServiceReference.getBundle()` returns `null` once the service is unregistered. This loop already anticipates that window — the `loader == null` guard four lines up is commented "unregistered between the snapshot and here" — and then dereferences `getBundle()` unconditionally for the same window. The argument is evaluated regardless of log level, so DEBUG being off does not help.
Because the call sits inside the `try { ... } catch (Throwable t)` that rethrows as `RuntimeCamelException`, the NPE does not just skip one loader: it escapes `createRegistry()` and `getDelegate()`, so a routine bundle stop makes the whole type converter registry unbuildable.
Worth noting the inconsistency: `removedService` guards this exact call (`serviceReference.getBundle() != null ? ... : serviceReference`), but this line and the one in `addingService` do not. The tests mask it because `setUp()` stubs `serviceReference.getBundle()` to a non-null mock.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -99,9 +138,21 @@ public Object addingService(ServiceReference<TypeConverterLoader> serviceReferen
public void modifiedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
}
+ // not synchronized, for the same reason as addingService
@Override
public void removedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
LOG.trace("RemovedService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
+ trackedLoaders.remove(serviceReference);
+ // we took the service in addingService, so releasing it is ours to do
+ ungetQuietly(serviceReference);
+ if (this.delegate != null && !isStopping() && !isStopped()) {
Review Comment:
**`!isStopped()` also suppresses this warning in every pre-`STARTED` state, so the diagnostic #734 asked for is missing exactly in the lazy-init window this class documents.**
From `BaseService` (camel-api 4.18):
```java
public boolean isStopped() {
return status < STARTING || status >= STOPPED;
}
```
with `NEW = 0, BUILT = 1, INITIALIZING = 2, INITIALIZED = 3, STARTING = 4 ... STOPPED = 9 ... FAILED = 12`.
So `isStopped()` is `true` for `NEW`, `BUILT`, `INITIALIZING`, `INITIALIZED` and `FAILED` — not just after a stop. `getDelegate()`'s own comment below says it "may be called during `doInit()` ... which happens before `doStart()`". An `OsgiTypeConverter` that is already serving conversions but has not reached `STARTED` will therefore discard its registry on a loader unregistration with **no log line at all**, which is the invisibility this PR exists to remove.
`isStopping()` alone already achieves the stated goal (no line-per-loader on `tracker.close()`, since `stop()` sets `status = STOPPING` before calling `doStop()`). The `&& !isStopped()` half is what breaks it; if a post-stop guard is also wanted, `status != STOPPED` is the narrow form.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -238,10 +314,13 @@ public TypeConverterExists getTypeConverterExists() {
@Override
public void setTypeConverterExists(TypeConverterExists typeConverterExists) {
- getDelegate().setTypeConverterExists(typeConverterExists);
+ register(TYPE_CONVERTER_EXISTS_KEY, registry -> registry.setTypeConverterExists(typeConverterExists));
}
- public DefaultTypeConverter getDelegate() {
+ // fully synchronized rather than double checked: the delegate is not immutable after publication -
+ // removedService stops and replaces it - so a lock free read of the field buys a race for no real gain,
+ // conversion work dwarfing an uncontended monitor either way
+ public synchronized DefaultTypeConverter getDelegate() {
Review Comment:
**Making `getDelegate()` fully synchronized turns the conversion hot path into a global serialization point, and blocks every conversion in the container for the full duration of a rebuild.**
`convertTo`, `tryConvertTo`, `mandatoryConvertTo`, `lookup`, `allowNull`, `size`, `getStatistics` and `getInjector` all funnel through here. Previously `delegate` was `volatile` and read without a lock, so the hot path was contention-free; now every conversion on every route thread contends one monitor (and biased locking is gone as of JDK 15, so it inflates).
The rebuild case is the sharper one: when a single `TypeConverterLoader` unregisters, the next `getDelegate()` holds this monitor across `init()` + `loadCoreAndFastTypeConverters()` (bundle resource scanning) + `loader.load()` for every tracked loader + the replay. Every conversion in the container blocks for that whole span. `registrationCannotInterleaveWithARebuild` demonstrates precisely this with its 2 s stall.
The comment's "conversion work dwarfing an uncontended monitor" only holds while uncontended. Keeping a volatile fast-path read and synchronizing only the build gives the same build-once guarantee at no hot-path cost:
```java
DefaultTypeConverter d = delegate;
if (d != null) {
return d;
}
synchronized (this) { ... }
```
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -173,27 +242,33 @@ public <T> T tryConvertTo(Class<T> type, Object value) {
@Override
public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
- getDelegate().addTypeConverter(toType, fromType, typeConverter);
+ register(new TypeConvertible<>(fromType, toType),
+ registry -> registry.addTypeConverter(toType, fromType, typeConverter));
}
@Override
public void addTypeConverters(Object typeConverters) {
- getDelegate().addTypeConverters(typeConverters);
+ register(typeConverters, registry -> registry.addTypeConverters(typeConverters));
Review Comment:
**The keyed map only prunes the `addTypeConverter` / `addConverter` pair. `addTypeConverters`, `addBulkTypeConverters` and `addFallbackTypeConverter` are keyed by contributed-instance identity and have no removal API, so the map still grows without bound and still pins bundle classloaders.**
`removeTypeConverter` is the only pruning path and it only deletes a `TypeConvertible` key. `TypeConverterRegistry` exposes no `removeTypeConverters` / `removeFallbackTypeConverter` / `removeBulkTypeConverters`, so entries recorded here (and at the `addBulkTypeConverters` / `addFallbackTypeConverter` calls below) live until `doStop()`.
That breaks the stated rationale for keying on the affected path. "Re-registering replaces rather than appends, which is what a Blueprint container refresh does" is true for `addTypeConverter`, but a container refresh instantiates a **new bean**, so `addTypeConverters(newBeanInstance)` hashes to a fresh identity key and appends. 50 refreshes of a bundle contributing a `TypeConverters` bean leave 50 entries, each strongly holding the bean and through it the classloader of a bundle that may already be uninstalled — the leak shape the review flagged, on the path the PR description names as its motivating case. `addRemovePairsDoNotAccumulate` only exercises `addTypeConverter` / `removeTypeConverter`.
Secondary point: all three share one untyped key space, and `BulkTypeConverters extends Ordered, TypeConverter`, so the same instance passed to both `addBulkTypeConverters` and `addFallbackTypeConverter` collides and one registration is dropped from the replay. (`CoreTypeConverterRegistry.addBulkTypeConverters` is also a bare `return` in 4.18, so recording that one is dead weight either way.)
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -99,9 +138,21 @@ public Object addingService(ServiceReference<TypeConverterLoader> serviceReferen
public void modifiedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
}
+ // not synchronized, for the same reason as addingService
Review Comment:
**This comment is anchored here, but the defect is `this.delegate = null` at line 163 just below (and the `ServiceHelper.stopService(this.delegate)` above it) — an unsynchronized write to a field `getDelegate()` now guards with a monitor, so an invalidation can be swallowed by an in-flight rebuild.**
Interleaving:
1. T1 in synchronized `getDelegate()`: `delegate == null`, enters `createRegistry()`, snapshots `trackedLoaders` (contains `sr_A`), begins loading.
2. T2 (framework dispatch, unsynchronized `removedService` for `sr_A`): removes `sr_A` from `trackedLoaders`, calls `ServiceHelper.stopService(this.delegate)` on the still-null field, sets `delegate = null`.
3. T1 finishes and assigns `delegate` = the registry it built **with** `sr_A`'s converters.
The invalidation is lost and nothing will trigger another rebuild, so the context permanently serves converters from a bundle that is gone. The mirror-image interleaving hands a stopped delegate to a live caller.
I see this is split out to #743, which is reasonable as a scoping call — but widening `getDelegate()` to a full monitor in *this* PR makes the window larger, not smaller, so it is worth confirming the deferral still holds rather than inheriting it from the pre-monitor shape.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -173,27 +242,33 @@ public <T> T tryConvertTo(Class<T> type, Object value) {
@Override
public void addTypeConverter(Class<?> toType, Class<?> fromType, TypeConverter typeConverter) {
- getDelegate().addTypeConverter(toType, fromType, typeConverter);
+ register(new TypeConvertible<>(fromType, toType),
+ registry -> registry.addTypeConverter(toType, fromType, typeConverter));
}
@Override
public void addTypeConverters(Object typeConverters) {
- getDelegate().addTypeConverters(typeConverters);
+ register(typeConverters, registry -> registry.addTypeConverters(typeConverters));
}
@Override
public void addBulkTypeConverters(BulkTypeConverters bulkTypeConverters) {
- getDelegate().addBulkTypeConverters(bulkTypeConverters);
+ register(bulkTypeConverters, registry -> registry.addBulkTypeConverters(bulkTypeConverters));
}
@Override
- public boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
- return getDelegate().removeTypeConverter(toType, fromType);
+ public synchronized boolean removeTypeConverter(Class<?> toType, Class<?> fromType) {
+ boolean removed = getDelegate().removeTypeConverter(toType, fromType);
+ // delete the matching registration rather than recording an inverse. An inverse would reproduce the same
+ // end state on replay, but it would also keep the removed converter - and its bundle's classloader -
+ // strongly reachable for the life of the context, and cost a no-op call on every future rebuild
+ programmaticRegistrations.remove(new TypeConvertible<>(fromType, toType));
+ return removed;
}
@Override
public void addFallbackTypeConverter(TypeConverter typeConverter, boolean canPromote) {
- getDelegate().addFallbackTypeConverter(typeConverter, canPromote);
+ register(typeConverter, registry -> registry.addFallbackTypeConverter(typeConverter, canPromote));
Review Comment:
**Replaying after the loader loop inverts fallback converter precedence, because `addFallbackTypeConverter` prepends.**
`CoreTypeConverterRegistry.addFallbackTypeConverter` (verified in bytecode) is:
```java
fallbackConverters.add(0, new FallbackTypeConverter(typeConverter, canPromote));
```
i.e. **last added == index 0 == tried first**. That is what `createRegistry()`'s "Just make sure we install the high ranking fallback converter at last" comment relies on: the highest-ranking `ServiceReference` sorts last, so its fallback lands at index 0.
`replayProgrammaticRegistrations(answer)` then runs *after* the entire loop, so any programmatic fallback is prepended in front of the high-ranking loader fallback that the sort deliberately put there.
In the live registry the opposite ordering held: a fallback registered at time T sat behind every loader that arrived after T (`addingService` -> `loader.load(delegate)` -> prepend). So a single loader unregistration silently flips which fallback wins for every unmatched conversion in the context. The field Javadoc's claim that the rebuilt registry is brought "back to the same state" does not hold for fallbacks.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +97,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
Review Comment:
**`ensureTrackerOpen()` is now synchronized on `this`, but `doStop()` closes the tracker and clears `trackerOpened` without that monitor — and `getDelegate()` has no lifecycle guard, so the tracker can be left open after stop, permanently.**
`BaseService.stop()` runs `doStop()` under its own `ReentrantLock`, not under `this`, so the two are not mutually excluded. Interleaving: T1 in `doStop()` executes `tracker.close()`; T2 inside synchronized `getDelegate()` -> `ensureTrackerOpen()` calls `tracker.open()`; T1 then sets `trackerOpened = false`. Result: a live, listening `ServiceTracker` on a `STOPPED` service, holding a `getService()` use count for every `TypeConverterLoader`.
Nothing can clean that up — `BaseService.stop()` returns early on `if (status == STOPPED || status == SHUTTING_DOWN || status == SHUTDOWN)`, so a second `stop()` never reaches `doStop()`.
The same hole exists with no race at all: `getDelegate()` performs no `isStopped()` / `isStopping()` check, so one stray conversion after stop calls `ensureTrackerOpen()` -> `tracker.open()` and resurrects the tracker for the life of the framework, pinning every contributing bundle's classloader. Synchronizing one half of a two-field invariant is worse than neither, because it reads as protected. `restartDoesNotReplayThePreviousLifecycle` shows the lifecycle was considered, but only the explicit stop/start path.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +97,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
if (!trackerOpened) {
tracker.open();
trackerOpened = true;
}
}
+ // deliberately not synchronized: the tracker calls this from the framework's service event dispatch, and
+ // taking this instance's monitor here would put our lock on the far side of the framework's, which is the
+ // ordering that makes a lock inversion possible
@Override
public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
LOG.trace("AddingService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
TypeConverterLoader loader = bundleContext.getService(serviceReference);
if (loader != null) {
+ trackedLoaders.put(serviceReference, loader);
try {
LOG.debug("loading type converter from bundle: {}", serviceReference.getBundle().getSymbolicName());
- if (delegate != null) {
+ DefaultTypeConverter current = delegate;
Review Comment:
**`delegate` is read here without the monitor, so a loader arriving during a rebuild can end up recorded in `trackedLoaders` but present in no registry, with nothing left to trigger another rebuild.**
1. T1 in synchronized `getDelegate()`: `delegate == null`, enters `createRegistry()`, executes `new ArrayList<>(trackedLoaders.keySet())` — empty.
2. T2 (framework dispatch, `addingService` for `sr_A`): `trackedLoaders.put(sr_A, loader)` succeeds, then this line reads `delegate` as `null` because T1 has not assigned yet, so the `if (current != null)` branch is skipped and `load()` is never called.
3. T1 completes and publishes a registry that does not contain `sr_A`'s converters.
`sr_A` is in `trackedLoaders`, so no further service event fires and the next rebuild only happens on an unrelated unregistration. Until then every conversion `sr_A` provided fails with `NoTypeConversionAvailableException`.
Recording the loader and deciding whether to load it are two steps raced against a build that is atomic with neither. Note the pre-PR code had the same hole via `tracker.getServiceReferences()` (the tracker's `tracked` map is only populated after `addingService` returns), so this is not a regression — but it is in the method being rewritten, and the `trackedLoaders` map is now the place where it could actually be closed.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
Review Comment:
**This invariant does not hold. Anchoring here because the comment states it; the calls that break it are `ensureTrackerOpen()` inside synchronized `getDelegate()` (line 329) and `loader.load(answer)` / `answer.init()` in this method.**
The comment here, plus the ones on `addingService` and `removedService`, justify unsynchronizing the callbacks and abandoning `tracker.getServiceReferences()` on the grounds that no framework call may happen under this monitor. But inside synchronized `getDelegate()`:
- `ensureTrackerOpen()` -> `ServiceTracker.open()` acquires framework service-registry locks **and synchronously dispatches `addingService` for every initial service**;
- `answer.init()` / `answer.loadCoreAndFastTypeConverters()` do bundle-wiring resource scanning;
- `loader.load(answer)` twelve lines below runs arbitrary third-party bundle code that may call back into the framework or into this same `OsgiTypeConverter`.
So the `this` -> framework-lock edge the redesign was meant to remove is still present, just relocated. A concrete shape: the framework dispatch thread runs `addingService` -> `loader.load(current)` where a `CamelContextAware` loader performs a conversion -> `getDelegate()` -> blocks on `this`, while another thread holds `this` inside `tracker.open()`.
The residual risk may well be acceptable, but the comments assert the mitigation is complete when it is partial, which is the part that will mislead the next reader.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -279,28 +358,73 @@ public Set<ClassLoader> getClassLoaders() {
throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
}
- // Load the type converters the tracker has been tracking
- // Here we need to use the ServiceReference to check the ranking
- ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
- if (serviceReferences != null) {
- ArrayList<ServiceReference<TypeConverterLoader>> servicesList =
- new ArrayList<>(Arrays.asList(serviceReferences));
- // Just make sure we install the high ranking fallback converter at last
- Collections.sort(servicesList);
- for (ServiceReference<TypeConverterLoader> sr : servicesList) {
- try {
- LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
- ((TypeConverterLoader)this.tracker.getService(sr)).load(answer);
- } catch (Throwable t) {
- throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
- }
+ // Load the type converters the tracker has been tracking. These come from our own map rather than from
+ // tracker.getServiceReferences()/getService(): this runs while holding this instance's monitor, and
+ // calling back into the tracker from here is what would establish a lock ordering against the framework.
+ List<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<>(trackedLoaders.keySet());
+ // Just make sure we install the high ranking fallback converter at last
+ Collections.sort(servicesList);
+ for (ServiceReference<TypeConverterLoader> sr : servicesList) {
+ TypeConverterLoader loader = trackedLoaders.get(sr);
+ if (loader == null) {
+ // unregistered between the snapshot and here
+ continue;
+ }
+ try {
+ LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
+ loader.load(answer);
+ } catch (Throwable t) {
+ throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
}
}
+ replayProgrammaticRegistrations(answer);
+
LOG.trace("Created TypeConverter: {}", answer);
return answer;
}
+ /**
+ * Re-applies everything that was registered through this facade rather than by a
+ * {@link TypeConverterLoader}, in the order it was originally applied.
+ */
+ private void replayProgrammaticRegistrations(DefaultTypeConverter registry) {
+ if (programmaticRegistrations.isEmpty()) {
+ return;
+ }
+ LOG.debug("Replaying {} programmatic registration(s) onto the rebuilt type converter registry",
+ programmaticRegistrations.size());
+ for (Consumer<TypeConverterRegistry> registration : programmaticRegistrations.values()) {
+ registration.accept(registry);
+ }
+ }
+
+ /**
Review Comment:
**Two consecutive Javadoc blocks: this one is orphaned by the `/** */` for `programmaticRegistrationCount()` that follows, so `register()` ends up with no Javadoc and the surviving comment documents the wrong method.**
Java attaches only the last block before a declaration. This block — the load-bearing explanation of why apply-and-record has to be atomic, which both the commit message and the PR description lean on — becomes a dangling comment attached to nothing, and `register()` at line 421 is undocumented. The `{@link #getDelegate()}` reference in it is invisible to doclint too.
Looks like a rebase/squash artifact; moving this block down to immediately precede `register()` fixes it.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/test/java/org/apache/camel/karaf/core/OsgiTypeConverterTest.java:
##########
@@ -17,7 +17,20 @@
package org.apache.camel.karaf.core;
import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.TypeConverter;
import org.apache.camel.spi.Injector;
+import java.util.ArrayList;
Review Comment:
**The new `java.util` imports were inserted into the middle of the existing `org.apache.camel` group, splitting it in two.**
Lines 19-22 are `org.apache.camel.*`, lines 23-31 are `java.util.*`, then line 33 returns to `org.apache.camel.impl.converter.DefaultTypeConverter` and `org.apache.camel.spi.*`.
`OsgiTypeConverter.java` itself (lines 19-28) and the rest of the module put `java.*` first, then a blank line, then the third-party groups. There is no enforced formatter plugin here so nothing fails the build, but this guarantees a churn diff the next time anyone runs an IDE import organiser over the file.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -203,7 +278,7 @@ public TypeConverter lookup(Class<?> toType, Class<?> fromType) {
@Override
public void setInjector(Injector injector) {
Review Comment:
**`setInjector` only records a replay lambda and never updates the `injector` field, so every rebuilt registry is constructed with the constructor-time injector and loads its core converters before the replay corrects it.**
`createRegistry()` passes the `final` field `this.injector` to `new OsgiDefaultTypeConverter(...)`, then calls `answer.init()` and `answer.loadCoreAndFastTypeConverters()`, and only afterwards calls `replayProgrammaticRegistrations(answer)`. So after a rebuild, converters instantiated during core loading used the **old** injector; the configured one is swapped in only after the fact, and anything already created keeps the stale reference.
`TYPE_CONVERTER_EXISTS_KEY` and `TYPE_CONVERTER_EXISTS_LOGGING_LEVEL_KEY` have the same ordering problem, and it compounds the replay issue on `replayProgrammaticRegistrations`: a context configured `Override` or `Fail` has all its core and loader converters registered under the constructor defaults (`Ignore` / `DEBUG`), and the real policy only takes effect partway through the replay, in insertion order.
This is strictly better than pre-PR behaviour (where the setting was lost entirely), so not a blocker — but "brought back to the same state" is not what happens.
_AI-generated review on behalf of JB Onofré_
##########
core/camel-core-osgi/src/test/java/org/apache/camel/karaf/core/OsgiTypeConverterTest.java:
##########
@@ -110,4 +125,243 @@ void removedServiceShouldInvalidateDelegate() throws Exception {
var delegateAfter = osgiTypeConverter.getDelegate();
assertNotNull(delegateAfter);
}
+
+ @Test
+ void concurrentFirstAccessShouldBuildTheRegistryOnce() throws Exception {
+ int threads = 16;
+ AtomicInteger created = new AtomicInteger();
+ CountDownLatch startLine = new CountDownLatch(1);
+
+ OsgiTypeConverter counting = new OsgiTypeConverter(bundleContext, camelContext, injector) {
+ @Override
+ protected DefaultTypeConverter createRegistry() {
+ created.incrementAndGet();
+ return super.createRegistry();
+ }
+ };
+
+ ExecutorService pool = Executors.newFixedThreadPool(threads);
+ try {
+ List<Future<DefaultTypeConverter>> futures = new ArrayList<>();
+ for (int i = 0; i < threads; i++) {
+ futures.add(pool.submit(() -> {
+ startLine.await();
+ return counting.getDelegate();
+ }));
+ }
+ // release them all at once so they race on the null check
+ startLine.countDown();
+
+ DefaultTypeConverter first = futures.get(0).get(30, TimeUnit.SECONDS);
+ assertNotNull(first);
+ for (Future<DefaultTypeConverter> f : futures) {
+ assertSame(first, f.get(30, TimeUnit.SECONDS),
+ "every caller must see the same registry instance");
+ }
+ } finally {
+ pool.shutdownNow();
+ }
+
+ assertEquals(1, created.get(),
+ "the registry must be built exactly once, otherwise converters registered on a discarded"
+ + " instance are silently lost");
+ }
+
+ /** Marker source type, so the registered converter cannot collide with a core one. */
+ interface Marker {
+ }
+
+ @Test
+ void rebuiltRegistryReloadsTheTrackedLoadersWithoutAskingTheTracker() throws Exception {
+ // arrives before the registry exists, so addingService only records it
+ osgiTypeConverter.addingService(serviceReference);
+ verify(loader, never()).load(any());
+
+ DefaultTypeConverter first = osgiTypeConverter.getDelegate();
+
+ // createRegistry replayed it from the recorded loaders; it never called back into the ServiceTracker,
+ // which is what would put a framework call underneath this instance's monitor
+ verify(loader).load(first);
+ }
+
+ @Test
+ void programmaticConverterSurvivesARegistryRebuild() throws Exception {
+ DefaultTypeConverter before = osgiTypeConverter.getDelegate();
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ assertNotNull(before.lookup(String.class, Marker.class), "precondition: the converter is registered");
+
+ // one loader going away discards the whole registry
+ osgiTypeConverter.removedService(serviceReference, loader);
+ DefaultTypeConverter after = osgiTypeConverter.getDelegate();
+
+ assertNotSame(before, after, "the registry should have been rebuilt");
+ assertNotNull(after.lookup(String.class, Marker.class),
+ "a converter registered programmatically must be replayed onto the rebuilt registry, otherwise it"
+ + " disappears from a running context when any bundle unregisters a loader");
+ }
+
+ @Test
+ void removedTypeConverterIsNotResurrectedByARebuild() throws Exception {
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ osgiTypeConverter.removeTypeConverter(String.class, Marker.class);
+
+ osgiTypeConverter.removedService(serviceReference, loader);
+
+ assertNull(osgiTypeConverter.getDelegate().lookup(String.class, Marker.class),
+ "the replay must reproduce the sequence, not just the additions");
+ }
+
+ @Test
+ void addingServiceReleasesTheServiceWhenLoadingFails() throws Exception {
+ osgiTypeConverter.getDelegate();
+ doThrow(new RuntimeException("boom")).when(loader).load(any());
+
+ assertThrows(RuntimeCamelException.class, () -> osgiTypeConverter.addingService(serviceReference));
+
+ // a customizer that throws is treated as never tracked, so removedService will not run for this
+ // reference and nothing else would release the use count taken by addingService
+ verify(bundleContext).ungetService(serviceReference);
+ }
+
+ @Test
+ void removedServiceReleasesTheService() {
+ osgiTypeConverter.addingService(serviceReference);
+
+ osgiTypeConverter.removedService(serviceReference, loader);
+
+ verify(bundleContext).ungetService(serviceReference);
+ }
+
+ @Test
+ void addingServiceMustNotHoldTheInstanceMonitor() throws Exception {
+ // build first, so addingService takes the branch that calls into the loader
+ osgiTypeConverter.getDelegate();
+
+ CountDownLatch insideLoad = new CountDownLatch(1);
+ CountDownLatch releaseLoad = new CountDownLatch(1);
+ doAnswer(invocation -> {
+ insideLoad.countDown();
+ releaseLoad.await(30, TimeUnit.SECONDS);
+ return null;
+ }).when(loader).load(any());
+
+ ExecutorService pool = Executors.newFixedThreadPool(2);
+ try {
+ Future<?> adding = pool.submit(() -> osgiTypeConverter.addingService(serviceReference));
+ assertTrue(insideLoad.await(30, TimeUnit.SECONDS), "addingService should have reached loader.load");
+
+ // the framework calls addingService while it is dispatching a service event; if it took this
+ // instance's monitor, every conversion in the container would block behind an arbitrary bundle's
+ // loader for as long as that loader takes
+ Future<DefaultTypeConverter> reader = pool.submit(osgiTypeConverter::getDelegate);
+ assertNotNull(reader.get(10, TimeUnit.SECONDS),
+ "getDelegate must not be blocked by an in-flight addingService");
+
+ releaseLoad.countDown();
+ adding.get(30, TimeUnit.SECONDS);
+ } finally {
+ releaseLoad.countDown();
+ pool.shutdownNow();
+ }
+ }
+
+ @Test
+ void addRemovePairsDoNotAccumulate() {
+ osgiTypeConverter.getDelegate();
+
+ for (int i = 0; i < 500; i++) {
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ osgiTypeConverter.removeTypeConverter(String.class, Marker.class);
+ }
+
+ // an inverse-appending list would sit at 1000 here, and every converter it captured - and the classloader
+ // of the bundle that contributed it - would stay strongly reachable for the life of the context
+ assertEquals(0, osgiTypeConverter.programmaticRegistrationCount(),
+ "add/remove pairs must prune, not accumulate");
+ }
+
+ @Test
+ void reRegisteringTheSameConversionReplaces() {
+ osgiTypeConverter.getDelegate();
+
+ for (int i = 0; i < 10; i++) {
+ // what a Blueprint container refresh looks like: the same conversion registered again
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ }
+
+ assertEquals(1, osgiTypeConverter.programmaticRegistrationCount(),
+ "re-registering the same conversion must replace rather than append");
+ }
+
+ @Test
+ void removingAConversionThatWasNeverRegisteredDoesNotAccumulate() {
+ osgiTypeConverter.getDelegate();
+
+ osgiTypeConverter.removeTypeConverter(String.class, Marker.class);
+
+ assertEquals(0, osgiTypeConverter.programmaticRegistrationCount(),
+ "a removal for a pair that was never registered must not be retained");
+ }
+
+ @Test
+ void restartDoesNotReplayThePreviousLifecycle() throws Exception {
+ osgiTypeConverter.start();
+ osgiTypeConverter.addTypeConverter(String.class, Marker.class, typeConverter);
+ assertNotNull(osgiTypeConverter.getDelegate().lookup(String.class, Marker.class));
+
+ osgiTypeConverter.stop();
+ osgiTypeConverter.start();
+
+ // the converters captured before the stop belong to bundles that may be gone by now
+ assertEquals(0, osgiTypeConverter.programmaticRegistrationCount());
+ assertNull(osgiTypeConverter.getDelegate().lookup(String.class, Marker.class),
+ "a stop/start cycle must not resurrect the previous lifecycle's registrations");
+ }
+
+ @Test
+ void registrationCannotInterleaveWithARebuild() throws Exception {
+ CountDownLatch insideBuild = new CountDownLatch(1);
+ CountDownLatch releaseBuild = new CountDownLatch(1);
+
+ OsgiTypeConverter stalling = new OsgiTypeConverter(bundleContext, camelContext, injector) {
+ @Override
+ protected DefaultTypeConverter createRegistry() {
+ DefaultTypeConverter built = super.createRegistry();
+ insideBuild.countDown();
+ try {
+ releaseBuild.await(30, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ return built;
+ }
+ };
+
+ ExecutorService pool = Executors.newFixedThreadPool(2);
+ try {
+ Future<DefaultTypeConverter> builder = pool.submit(stalling::getDelegate);
+ assertTrue(insideBuild.await(30, TimeUnit.SECONDS), "the rebuild should have started");
+
+ Future<?> registrar = pool.submit(() -> {
+ stalling.addTypeConverter(String.class, Marker.class, typeConverter);
+ return null;
+ });
+
+ // apply-and-record has to be one step against the rebuild. If it were not, this registration would be
+ // applied to the registry being discarded and only recorded afterwards, so the rebuilt one would
+ // neither have it applied nor replay it
+ assertThrows(TimeoutException.class, () -> registrar.get(2, TimeUnit.SECONDS),
Review Comment:
**Proving this by waiting 2 s for a `TimeoutException` is a `Thread.sleep` in disguise, and it passes vacuously if the second pool thread has not yet entered `addTypeConverter`.**
Two problems:
1. It adds a hard 2 s to every build and asserts a negative by elapsed time — the pattern the project's Awaitility guidance exists to eliminate ("flaky, slow, and non-deterministic").
2. It is not actually a guard. On a loaded CI box or with a cold thread pool, the `registrar` thread may not have reached the synchronized `register()` at all; the `get()` times out anyway and the test reports success even against an implementation where `register()` is **not** synchronized. The PR description already notes this test passes against its predecessor.
A deterministic positive assertion is available and the test almost makes it already: release the build, join both futures, then assert the registration is present in the published registry — which lines 358-359 do. Dropping the timing assertion loses nothing and removes the 2 s.
_AI-generated review on behalf of JB Onofré_
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(#734): build the type converter registry once, and say when it is discarded [camel-karaf]
Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #739:
URL: https://github.com/apache/camel-karaf/pull/739#discussion_r3872608088
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -99,9 +138,21 @@ public Object addingService(ServiceReference<TypeConverterLoader> serviceReferen
public void modifiedService(ServiceReference<TypeConverterLoader> serviceReference, Object o) {
}
+ // not synchronized, for the same reason as addingService
Review Comment:
**This comment is anchored here, but the defect is `this.delegate = null` at line 163 just below (and the `ServiceHelper.stopService(this.delegate)` above it) — an unsynchronized write to a field `getDelegate()` now guards with a monitor, so an invalidation can be swallowed by an in-flight rebuild.**
Interleaving:
1. T1 in synchronized `getDelegate()`: `delegate == null`, enters `createRegistry()`, snapshots `trackedLoaders` (contains `sr_A`), begins loading.
2. T2 (framework dispatch, unsynchronized `removedService` for `sr_A`): removes `sr_A` from `trackedLoaders`, calls `ServiceHelper.stopService(this.delegate)` on the still-null field, sets `delegate = null`.
3. T1 finishes and assigns `delegate` = the registry it built **with** `sr_A`'s converters.
The invalidation is lost and nothing will trigger another rebuild, so the context permanently serves converters from a bundle that is gone. The mirror-image interleaving hands a stopped delegate to a live caller.
I see this is split out to #743, which is reasonable as a scoping call — but widening `getDelegate()` to a full monitor in *this* PR makes the window larger, not smaller, so it is worth confirming the deferral still holds rather than inheriting it from the pre-monitor shape.
##########
core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiTypeConverter.java:
##########
@@ -67,27 +97,36 @@ public OsgiTypeConverter(BundleContext bundleContext, CamelContext camelContext,
this.tracker = new ServiceTracker<>(bundleContext, TypeConverterLoader.class.getName(), this);
}
- private void ensureTrackerOpen() {
+ private synchronized void ensureTrackerOpen() {
Review Comment:
**`ensureTrackerOpen()` is now synchronized on `this`, but `doStop()` closes the tracker and clears `trackerOpened` without that monitor — and `getDelegate()` has no lifecycle guard, so the tracker can be left open after stop, permanently.**
`BaseService.stop()` runs `doStop()` under its own `ReentrantLock`, not under `this`, so the two are not mutually excluded. Interleaving: T1 in `doStop()` executes `tracker.close()`; T2 inside synchronized `getDelegate()` -> `ensureTrackerOpen()` calls `tracker.open()`; T1 then sets `trackerOpened = false`. Result: a live, listening `ServiceTracker` on a `STOPPED` service, holding a `getService()` use count for every `TypeConverterLoader`.
Nothing can clean that up — `BaseService.stop()` returns early on `if (status == STOPPED || status == SHUTTING_DOWN || status == SHUTDOWN)`, so a second `stop()` never reaches `doStop()`.
The same hole exists with no race at all: `getDelegate()` performs no `isStopped()` / `isStopping()` check, so one stray conversion after stop calls `ensureTrackerOpen()` -> `tracker.open()` and resurrects the tracker for the life of the framework, pinning every contributing bundle's classloader. Synchronizing one half of a two-field invariant is worse than neither, because it reads as protected. `restartDoesNotReplayThePreviousLifecycle` shows the lifecycle was considered, but only the explicit stop/start path.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org