Repository navigation
Do not wait for the launcher if the launching is already suspended - #50
Merged
Merged
Conversation
suspend(true, ...) on an executor whose launching was already suspended waited for a notification that the launcher, already waiting to be resumed, never sent, so it hung forever, and so did shutDown(false) after a suspension, which suspend(false) now makes easier to reach. The immediate suspension now waits for the launcher only when the launching is not suspended yet, as the suspension that is not immediate already did. The launcher checked the suspension flag before taking resumeCallerMutex, so a resumeFromSuspension() that came in between was lost and the launcher stayed suspended: now it checks the flag again before waiting. The flag is volatile, since it is read without a lock and under different monitors.
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.
Two problems in the suspension of
QueuedTaskExecutorthat #48 left open.Suspending an executor that is already suspended hung. The immediate suspension sets
supendedand waits onsuspensionCallerMutexuntil the launcher notifies that it has stopped. When the launching was already suspended, the launcher was already waiting onresumeCallerMutexand never sent that notification, sosuspend(true, ...)hung forever, and so didshutDown(false)after any suspension, since it callssuspend(true, true). This happened onmasterbefore #48 too, but since #48 it can also be reached withsuspend(false)followed byshutDown(false).A resumption could be lost.
checkAndNotifySuspensionreadssupendedwithout a lock and only then takesresumeCallerMutexand waits on it. AresumeFromSuspension()falling between the two set the flag tofalseand notified nobody, and the launcher then waited until the next resumption, with the executor no longer suspended.suspendmethods says so.checkAndNotifySuspensionstill notifies the callers of the suspension, then checkssupendedagain underresumeCallerMutexand waits only if the launching is still suspended.supendedisvolatile, since it is read without a lock and under different monitors.BackgroundExecutorTest, on dedicated executors:suspendTestTwo(a secondsuspend(true)returns, the executor stays suspended and then resumes),shutDownTestThree(shutDown(false)aftersuspend(false)ends and aborts the queued task) andresumeFromSuspensionTestOne, which holdsresumeCallerMutexto stop the launcher right after it has read the flag, resumes the executor at that point and checks that the launching goes on. The helperwaitUntilWaitingInbecomeswaitUntilIn, with the state of the thread as a parameter.How this is tested. The workflow Build and test (pull requests) runs on this pull request. Locally, on Windows with JDK 21:
master, the two hangs detected by a 30 s timeout and the lost resumption by the task that is never executed, and pass with this change; all the tests ofBackgroundExecutorTestpass 3 times out of 3 (12 passed, 2 skipped);immediately: onmasterthe two ending withsuspend(true)hang, with this change all of them return;submit()of 5 tasks followed byshutDown(true)orclose(), 1000 rounds each, and nested tasks, 300 rounds: no task lost, nothing stalled, no error or warning in the log;AllExceptHeavyTestsSuite(mvn -B -o test -DskipTests=false): 296 tests, no failures, 2 skipped; the only ERROR in the log comes frominterruptTestOne, which interrupts a task on purpose.Not fixed here, still open from #48:
shutDown(true)can still hang if a task started while it waits submits a task and waits for it after the suspension; enablingdiscardingTasksright 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.suspend(true)can still wait forever if the resumption comes before the launcher has read the flag.🤖 Generated with Claude Code