Skip to content

Fix the suspension of QueuedTaskExecutor after the queued tasks - #48

Merged
Roberto-Gentili merged 1 commit into
masterfrom
claude/suspension-after-queued-tasks
Nov 29, 2026
Merged

Roberto-Gentili merged 1 commit into
masterfrom
claude/suspension-after-queued-tasks

Conversation

@Roberto-Gentili

Copy link
Copy Markdown
Member

suspend(false, ...) always failed on master, and so did shutDown(true) and close(), which use it. suspend0 added the suspending task to the queue with addToQueue() without submitting it and then waited for it, so checkSubmitted() threw TaskStateException "is not submitted". It has been broken since 05885a9 (September 2020), where submit(true) became addToQueue().

The exception itself failed: its constructors call Strings.from(task.getCreatorInfos(), 1), which throws a NullPointerException when the tracking of the creation of the tasks is disabled, the default for the executors created with QueuedTaskExecutor.create. Every TaskStateException of a dedicated executor became a NullPointerException that hid the real error.

Fixing the suspension brought out a third defect: between the moment the launcher takes a task from the queue and the moment the thread of the task puts it in tasksInExecution, the task is in neither collection, so the waits do not see it. A task submitted right before shutDown(true) or close() could start after closeResources() and fail with a NullPointerException on tasksInExecution.put without being executed.

  • The suspension that is not immediate waits for the tasks in execution and then, if the launcher is not suspended yet, for the tasks already in the queue one by one, while the launching goes on, so the tasks they submit and wait for are executed too. Then it suspends the launching as the immediate suspension does. With a suspending task in the queue, as createSuspendingTask did, a child task would end up behind it and its parent would wait forever. If the launcher is already suspended, only the tasks in execution are waited for and the queued ones are not started, as runOnlyOnce(..., () -> supended) meant. createSuspendingTask and its override in Group are removed, and the Javadoc of the two suspend methods is updated.
  • The launcher puts a task in tasksInExecution inside synchronized (task), right after removing it from the queue.
  • TasksMonitorer skips the tasks whose startTime is still null, which the launcher has just taken.
  • TaskStateException builds its message once: without the information about where the task was created, the message is <task> <message>.
  • New tests in BackgroundExecutorTest, on dedicated executors: suspendAfterExecutingQueuedTasksTestOne, shutDownAfterExecutingQueuedTasksTestOne, shutDownAfterExecutingQueuedTasksTestTwo, where a task in execution submits a task and waits for it during shutDown(true), closeTestOne and waitForFinishOfNotSubmittedTaskTestOne.

Relation with #47. Both pull requests change QueuedTaskExecutor and BackgroundExecutorTest. A three-way merge combines them without conflicts in either order, and with both changes the tests of BackgroundExecutorTest pass locally (9 passed, 2 skipped).

How this is tested. The workflow Build and test (pull requests) runs on this pull request. Locally, on Windows with JDK 21:

  • none of the 5 new tests passes on master, where all of them fail with the NullPointerException of Strings.from; with only the change to TaskStateException 1 passes and the 4 about suspension and shutdown fail with "is not submitted"; with the whole change all 5 pass;
  • submit() of 5 tasks followed by shutDown(true), 1000 rounds: without the registration in the launcher, tasks were lost in 2 rounds, and in 97 rounds (111 tasks) under the load of 24 threads; with the whole change no task was lost and nothing stalled, also with close() and with nested tasks under load;
  • AllExceptHeavyTestsSuite: 289 tests, no failures, 2 skipped.

Not fixed here.

  • suspend(true) on an executor that is already suspended hangs, and so does shutDown(false) after a suspension. This already happened on master, but it can now be reached with suspend(false) followed by shutDown(false).
  • With Abort the queued tasks before waiting for the running ones in shutDown(false) #47 too, shutDown(true) hangs if a task started while it waits submits a task and waits for it after the suspension. Enabling the discardingTasks of Abort the queued tasks before waiting for the running ones in shutDown(false) #47 right before the immediate suspension in shutDown(true) would close it.
  • changePriorityToAllTaskBeforeAndWaitThem, used by the public waitFor(task, priority, ignoreDeadLocked), changes the priority of the task waited for instead of the tasks before it.
  • checkAndNotifySuspension does not check the suspension again under resumeCallerMutex, so a resumeFromSuspension() that falls between the check and the wait() is lost; this needs a suspension and a resumption at the same time.

🤖 Generated with Claude Code

suspend(false, ...) always failed, and so did shutDown(true) and
close(): the suspending task was added to the queue without being
submitted, so waiting for it threw TaskStateException "is not
submitted". The exception itself failed with a NullPointerException
when the tracking of the creation of the tasks was disabled, the
default for the executors created with QueuedTaskExecutor.create, and
hid the real error.

The suspension that is not immediate now waits for the tasks in
execution and for the tasks already in the queue while the launching
goes on, so that the tasks they submit and wait for are executed too,
and then suspends the launching as the immediate one does. If the
launching is already suspended it only waits for the tasks in
execution.

The launcher now registers a task among the tasks in execution as soon
as it takes it from the queue: before, the task was in neither
collection until its thread started it, so the waits could miss it and
a task submitted right before shutDown(true) could start after the
resources had been released. The monitoring of the tasks skips the
tasks that have not started yet.

TaskStateException leaves out where the task was created when it has
not been recorded.
@Roberto-Gentili
Roberto-Gentili merged commit 5251bba into master Nov 29, 2026
58 of 59 checks passed
@Roberto-Gentili
Roberto-Gentili deleted the claude/suspension-after-queued-tasks branch November 29, 2026 15:58
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