[ 
https://issues.apache.org/jira/browse/ARIES-2255?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Timothy James Ward updated ARIES-2255:
--------------------------------------
    Description: 
Closing an OSGi program built with coalesce(configuration(pid), just(...)) can 
throw a NullPointerException if a configuration update event is being applied 
on the Config Admin event thread at the same time.

Stack trace (from the Typed Events bus bundle stopping):

{{java.lang.NullPointerException: Cannot invoke 
"org.apache.aries.component.dsl.OSGiResult.close()" because the return value of 
"java.util.concurrent.atomic.AtomicReference.get()" is null}}
{{at 
org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$1(CoalesceOSGiImpl.java:75)}}
{{at 
org.apache.aries.component.dsl.internal.UpdateSupport.deferTermination(UpdateSupport.java:49)}}
{{at 
org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$2(CoalesceOSGiImpl.java:73)}}
{{at org.apache.aries.component.dsl.OSGiResult.run(OSGiResult.java:29)}}
{{at 
org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl.signalLeave(ConfigurationOSGiImpl.java:186)}}
{{at 
org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl.lambda$null$3(ConfigurationOSGiImpl.java:138)}}
{{...}}
{{at 
org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$4(CoalesceOSGiImpl.java:123)}}
h3. Cause:

in ConfigurationOSGiImpl, the configuration listener runs 
UpdateSupport.runUpdate(...). Inside it op.apply(...) stores the new terminator 
in terminatorAtomicReference, but the publication into CoalesceOSGiImpl is 
deferred (UpdateSupport.deferPublication) and only runs in the finally of 
runUpdate. CoalesceOSGiImpl sets its result reference from that deferred 
publication, and the terminator it returns calls result.get().close() 
unconditionally. Between the two steps another thread calling close() takes the 
new terminator and runs it while result is still null. The window is the time 
spent in the downstream publication, which is long for real programs (the bus 
constructs services there).
h3. Consequences:

close() throws, the downstream program is left partly open, and the late 
publication then completes against a closed program.
h3. Reproduction:

a unit test with a mocked BundleContext and Config Admin registers 
coalesce(configuration(pid), just(Hashtable::new)).map(...) .flatMap(...), 
fires a CM_UPDATED event from a second thread and calls close() on the first. 
With about 1 ms spent in the downstream map step, close() threw this NPE in 766 
of 1000 iterations. Without any delay the window was too small to hit in 20000 
iterations.
h3. Proposed fix:

make the terminator tolerate a publication that has not completed. Keep a 
per-publication terminated flag. The terminator sets it and closes the result 
only if it is already set. The deferred publication skips publishing when 
already terminated, and closes its own result if termination arrives while it 
is publishing. With this change the same test produced no failures in 3 x 1000 
iterations.

A lock in ConfigurationOSGiImpl was tried first and deadlocks against the 
synchronized (initialized) monitor held by the Coalesce close path, so the fix 
has to be in CoalesceOSGiImpl.

Note: on master the update propagation (result.get().update() in 
CoalesceOSGiImpl) reads the same reference and should get the same null check.

  was:
Closing an OSGi program built with coalesce(configuration(pid), just(...)) can 
throw a NullPointerException if a configuration update event is being applied 
on the Config Admin event thread at the same time.

Stack trace (from the Typed Events bus bundle stopping):

{{java.lang.NullPointerException: Cannot invoke 
"org.apache.aries.component.dsl.OSGiResult.close()" because the return value of 
"java.util.concurrent.atomic.AtomicReference.get()" is null
at 
org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$1(CoalesceOSGiImpl.java:75)
at 
org.apache.aries.component.dsl.internal.UpdateSupport.deferTermination(UpdateSupport.java:49)
at 
org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$2(CoalesceOSGiImpl.java:73)
at org.apache.aries.component.dsl.OSGiResult.run(OSGiResult.java:29)
at 
org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl.signalLeave(ConfigurationOSGiImpl.java:186)
at 
org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl.lambda$null$3(ConfigurationOSGiImpl.java:138)
...
at 
org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$4(CoalesceOSGiImpl.java:123)}}
h3. Cause:

in ConfigurationOSGiImpl, the configuration listener runs 
UpdateSupport.runUpdate(...). Inside it op.apply(...) stores the new terminator 
in terminatorAtomicReference, but the publication into CoalesceOSGiImpl is 
deferred (UpdateSupport.deferPublication) and only runs in the finally of 
runUpdate. CoalesceOSGiImpl sets its result reference from that deferred 
publication, and the terminator it returns calls result.get().close() 
unconditionally. Between the two steps another thread calling close() takes the 
new terminator and runs it while result is still null. The window is the time 
spent in the downstream publication, which is long for real programs (the bus 
constructs services there).
h3. Consequences:

close() throws, the downstream program is left partly open, and the late 
publication then completes against a closed program.
h3. Reproduction:

a unit test with a mocked BundleContext and Config Admin registers 
coalesce(configuration(pid), just(Hashtable::new)).map(...) .flatMap(...), 
fires a CM_UPDATED event from a second thread and calls close() on the first. 
With about 1 ms spent in the downstream map step, close() threw this NPE in 766 
of 1000 iterations. Without any delay the window was too small to hit in 20000 
iterations.
h3. Proposed fix:

make the terminator tolerate a publication that has not completed. Keep a 
per-publication terminated flag. The terminator sets it and closes the result 
only if it is already set. The deferred publication skips publishing when 
already terminated, and closes its own result if termination arrives while it 
is publishing. With this change the same test produced no failures in 3 x 1000 
iterations.

A lock in ConfigurationOSGiImpl was tried first and deadlocks against the 
synchronized (initialized) monitor held by the Coalesce close path, so the fix 
has to be in CoalesceOSGiImpl.

Note: on master the update propagation (result.get().update() in 
CoalesceOSGiImpl) reads the same reference and should get the same null check.


> CoalesceOSGiImpl: NullPointerException when closed while a configuration 
> update is being published
> --------------------------------------------------------------------------------------------------
>
>                 Key: ARIES-2255
>                 URL: https://issues.apache.org/jira/browse/ARIES-2255
>             Project: Aries
>          Issue Type: Bug
>          Components: Component DSL
>    Affects Versions: 1.2.2
>         Environment: Component DSL 1.2.2 (also present on master, 
> 2.0.0-SNAPSHOT), Apache Felix 7.0.5, Temurin JDK 17. Found through Apache 
> Aries Typed Events, where it makes an integration test fail intermittently on 
> CI.
>            Reporter: Timothy James Ward
>            Priority: Major
>             Fix For: 1.2.3
>
>
> Closing an OSGi program built with coalesce(configuration(pid), just(...)) 
> can throw a NullPointerException if a configuration update event is being 
> applied on the Config Admin event thread at the same time.
> Stack trace (from the Typed Events bus bundle stopping):
> {{java.lang.NullPointerException: Cannot invoke 
> "org.apache.aries.component.dsl.OSGiResult.close()" because the return value 
> of "java.util.concurrent.atomic.AtomicReference.get()" is null}}
> {{at 
> org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$1(CoalesceOSGiImpl.java:75)}}
> {{at 
> org.apache.aries.component.dsl.internal.UpdateSupport.deferTermination(UpdateSupport.java:49)}}
> {{at 
> org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$2(CoalesceOSGiImpl.java:73)}}
> {{at org.apache.aries.component.dsl.OSGiResult.run(OSGiResult.java:29)}}
> {{at 
> org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl.signalLeave(ConfigurationOSGiImpl.java:186)}}
> {{at 
> org.apache.aries.component.dsl.internal.ConfigurationOSGiImpl.lambda$null$3(ConfigurationOSGiImpl.java:138)}}
> {{...}}
> {{at 
> org.apache.aries.component.dsl.internal.CoalesceOSGiImpl.lambda$null$4(CoalesceOSGiImpl.java:123)}}
> h3. Cause:
> in ConfigurationOSGiImpl, the configuration listener runs 
> UpdateSupport.runUpdate(...). Inside it op.apply(...) stores the new 
> terminator in terminatorAtomicReference, but the publication into 
> CoalesceOSGiImpl is deferred (UpdateSupport.deferPublication) and only runs 
> in the finally of runUpdate. CoalesceOSGiImpl sets its result reference from 
> that deferred publication, and the terminator it returns calls 
> result.get().close() unconditionally. Between the two steps another thread 
> calling close() takes the new terminator and runs it while result is still 
> null. The window is the time spent in the downstream publication, which is 
> long for real programs (the bus constructs services there).
> h3. Consequences:
> close() throws, the downstream program is left partly open, and the late 
> publication then completes against a closed program.
> h3. Reproduction:
> a unit test with a mocked BundleContext and Config Admin registers 
> coalesce(configuration(pid), just(Hashtable::new)).map(...) .flatMap(...), 
> fires a CM_UPDATED event from a second thread and calls close() on the first. 
> With about 1 ms spent in the downstream map step, close() threw this NPE in 
> 766 of 1000 iterations. Without any delay the window was too small to hit in 
> 20000 iterations.
> h3. Proposed fix:
> make the terminator tolerate a publication that has not completed. Keep a 
> per-publication terminated flag. The terminator sets it and closes the result 
> only if it is already set. The deferred publication skips publishing when 
> already terminated, and closes its own result if termination arrives while it 
> is publishing. With this change the same test produced no failures in 3 x 
> 1000 iterations.
> A lock in ConfigurationOSGiImpl was tried first and deadlocks against the 
> synchronized (initialized) monitor held by the Coalesce close path, so the 
> fix has to be in CoalesceOSGiImpl.
> Note: on master the update propagation (result.get().update() in 
> CoalesceOSGiImpl) reads the same reference and should get the same null check.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to