[
https://issues.apache.org/jira/browse/FELIX-6893?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Pierre De Rop resolved FELIX-6893.
----------------------------------
Resolution: Fixed
> DispatchExecutor: reentrant task deferred instead of run inline due to a race
> -----------------------------------------------------------------------------
>
> Key: FELIX-6893
> URL: https://issues.apache.org/jira/browse/FELIX-6893
> Project: Felix
> Issue Type: Bug
> Components: Dependency Manager
> Reporter: Pierre De Rop
> Assignee: Pierre De Rop
> Priority: Major
>
> {{DispatchExecutor.execute(Runnable, boolean)}} runs a task inline when the
> calling thread is the one currently draining the queue ({{m_executingThread
> == Thread.currentThread()}}). This matters for reentrant calls made from
> component callbacks, for example {{component.add(dependency)}} called from
> {{init()}}: the dependency must be added immediately, before the component
> state machine goes on.
> At the end of {{DispatchExecutor.run()}}, the {{finally}} block releases the
> {{m_scheduled}} flag *before* it clears {{m_executingThread}}:
> {code:java}
> } finally {
> m_scheduled.set(false);
> m_executingThread = null;
> if (m_tasks.peek() != null) {
> execute();
> }
> }
> {code}
> Between these two lines, another thread can acquire the queue and set
> {{m_executingThread}} to itself. The finishing thread then overwrites that
> value with {{null}}. From this point on, the new executing thread is no
> longer recognized as the owner of the queue, so its reentrant calls are
> queued and deferred instead of being run inline.
> h3. Example
> A component runs with a thread pool (parallel mode):
> # Thread A (thread pool) has drained the component queue and enters the
> {{finally}} block of {{run()}}. It sets {{m_scheduled}} to {{false}}.
> # Thread B calls {{execute(task, false)}}: this is the path used to bypass
> the thread pool for a {{REMOVED}} service event, or for the component
> {{stop()}}. B wins the CAS on {{m_scheduled}}, runs {{run()}} itself and sets
> {{m_executingThread = B}}.
> # Thread A runs {{m_executingThread = null}}, which overwrites B.
> # While B drains the queue, a task triggers a lifecycle callback (for example
> {{init()}}) that calls {{component.add(dependency)}}.
> # Because {{m_executingThread}} is {{null}}, the call is not run inline: the
> task is queued, and the CAS fails because B still holds the queue.
> # The state machine goes on (for example {{start()}} is called) *before* the
> dependency declared in {{init()}} is added. The deferred task only runs
> later, when B's loop picks it up.
> The race window is narrow, but when it is hit, callbacks run in a different
> order than the one the component expects.
> h3. Expected behavior
> While a thread is draining the queue of a component, any reentrant
> {{execute()}} call made from that thread must always run inline, whatever
> other threads are doing on the same queue at that moment.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)