Skip to content

FELIX-6893: Fix DispatchExecutor reentrancy race with a task counter - #613

Open
pderop wants to merge 1 commit into
apache:masterfrom
pderop:FELIX-6893
Open

pderop wants to merge 1 commit into
apache:masterfrom
pderop:FELIX-6893

Conversation

@pderop

@pderop pderop commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Motivation:

A thread executing a DispatchExecutor queue must run its reentrant tasks inline (e.g. component.add(dependency) from init()). The end of run() released the queue before clearing the executing thread:

m_scheduled.set(false);
m_executingThread = null;

Scenario: thread A, from the threadpool, finishes the queue and releases it. Thread B (a REMOVED event or stop(), which bypass the threadpool) then takes the queue over and sets itself as the executing thread. A then overwrites it with null. When a task running in B calls execute() again, B is no longer recognized as the queue owner: the task is queued instead of being run inline, and the component goes on (e.g. start()) before it has run.

Modification:

Rewrite DispatchExecutor around a counter of pending tasks: the thread which increments it from 0 owns the queue and runs tasks until it drops back to 0. The owner only clears the executing thread if no other thread has taken the queue over in the meantime. Task execution never throws, so the counter stays consistent. Unused methods are removed.

Result:

Reentrant tasks are always executed inline by the thread which owns the queue, and DispatchExecutor is much simpler.

Motivation:

A thread executing a DispatchExecutor queue must run its reentrant tasks
inline (e.g. component.add(dependency) from init()). The end of run()
released the queue before clearing the executing thread:

  m_scheduled.set(false);
  m_executingThread = null;

Scenario: thread A, from the threadpool, finishes the queue and
releases it. Thread B (a REMOVED event or stop(), which bypass the
threadpool) then takes the queue over and sets itself as the executing
thread. A then overwrites it with null. When a task running in B calls
execute() again, B is no longer recognized as the queue owner: the task
is queued instead of being run inline, and the component goes on (e.g.
start()) before it has run.

Modification:

Rewrite DispatchExecutor around a counter of pending tasks: the thread
which increments it from 0 owns the queue and runs tasks until it drops
back to 0. The owner only clears the executing thread if no other thread
has taken the queue over in the meantime. Task execution never throws,
so the counter stays consistent. Unused methods are removed.

Result:

Reentrant tasks are always executed inline by the thread which owns the
queue, and DispatchExecutor is much simpler.
@pderop pderop self-assigned this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant