Repository navigation
Fix the suspension of QueuedTaskExecutor after the queued tasks - #48
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
suspend(false, ...)always failed onmaster, and so didshutDown(true)andclose(), which use it.suspend0added the suspending task to the queue withaddToQueue()without submitting it and then waited for it, socheckSubmitted()threwTaskStateException"is not submitted". It has been broken since 05885a9 (September 2020), wheresubmit(true)becameaddToQueue().The exception itself failed: its constructors call
Strings.from(task.getCreatorInfos(), 1), which throws aNullPointerExceptionwhen the tracking of the creation of the tasks is disabled, the default for the executors created withQueuedTaskExecutor.create. EveryTaskStateExceptionof a dedicated executor became aNullPointerExceptionthat 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 beforeshutDown(true)orclose()could start aftercloseResources()and fail with aNullPointerExceptionontasksInExecution.putwithout being executed.createSuspendingTaskdid, 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, asrunOnlyOnce(..., () -> supended)meant.createSuspendingTaskand its override inGroupare removed, and the Javadoc of the twosuspendmethods is updated.tasksInExecutioninsidesynchronized (task), right after removing it from the queue.TasksMonitorerskips the tasks whosestartTimeis stillnull, which the launcher has just taken.TaskStateExceptionbuilds its message once: without the information about where the task was created, the message is<task> <message>.BackgroundExecutorTest, on dedicated executors:suspendAfterExecutingQueuedTasksTestOne,shutDownAfterExecutingQueuedTasksTestOne,shutDownAfterExecutingQueuedTasksTestTwo, where a task in execution submits a task and waits for it duringshutDown(true),closeTestOneandwaitForFinishOfNotSubmittedTaskTestOne.Relation with #47. Both pull requests change
QueuedTaskExecutorandBackgroundExecutorTest. A three-way merge combines them without conflicts in either order, and with both changes the tests ofBackgroundExecutorTestpass 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:
master, where all of them fail with theNullPointerExceptionofStrings.from; with only the change toTaskStateException1 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 byshutDown(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 withclose()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 doesshutDown(false)after a suspension. This already happened onmaster, but it can now be reached withsuspend(false)followed byshutDown(false).shutDown(true)hangs if a task started while it waits submits a task and waits for it after the suspension. Enabling thediscardingTasksof Abort the queued tasks before waiting for the running ones in shutDown(false) #47 right before the immediate suspension inshutDown(true)would close it.changePriorityToAllTaskBeforeAndWaitThem, used by the publicwaitFor(task, priority, ignoreDeadLocked), changes the priority of the task waited for instead of the tasks before it.checkAndNotifySuspensiondoes not check the suspension again underresumeCallerMutex, so aresumeFromSuspension()that falls between the check and thewait()is lost; this needs a suspension and a resumption at the same time.🤖 Generated with Claude Code