Skip to content

Do not wait for the launcher if the launching is already suspended - #50

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

Roberto-Gentili merged 1 commit into
masterfrom
claude/repeated-suspension

Conversation

@Roberto-Gentili

@Roberto-Gentili Roberto-Gentili commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Two problems in the suspension of QueuedTaskExecutor that #48 left open.

Suspending an executor that is already suspended hung. The immediate suspension sets supended and waits on suspensionCallerMutex until the launcher notifies that it has stopped. When the launching was already suspended, the launcher was already waiting on resumeCallerMutex and never sent that notification, so suspend(true, ...) hung forever, and so did shutDown(false) after any suspension, since it calls suspend(true, true). This happened on master before #48 too, but since #48 it can also be reached with suspend(false) followed by shutDown(false).

A resumption could be lost. checkAndNotifySuspension reads supended without a lock and only then takes resumeCallerMutex and waits on it. A resumeFromSuspension() falling between the two set the flag to false and notified nobody, and the launcher then waited until the next resumption, with the executor no longer suspended.

  • The immediate suspension waits for the launcher only if the launching is not suspended yet, as the suspension that is not immediate already does; otherwise it only waits for the tasks in execution. The Javadoc of the two suspend methods says so.
  • checkAndNotifySuspension still notifies the callers of the suspension, then checks supended again under resumeCallerMutex and waits only if the launching is still suspended.
  • supended is volatile, since it is read without a lock and under different monitors.
  • New tests in BackgroundExecutorTest, on dedicated executors: suspendTestTwo (a second suspend(true) returns, the executor stays suspended and then resumes), shutDownTestThree (shutDown(false) after suspend(false) ends and aborts the queued task) and resumeFromSuspensionTestOne, which holds resumeCallerMutex to stop the launcher right after it has read the flag, resumes the executor at that point and checks that the launching goes on. The helper waitUntilWaitingIn becomes waitUntilIn, 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:

  • the 3 new tests fail on 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 of BackgroundExecutorTest pass 3 times out of 3 (12 passed, 2 skipped);
  • two suspensions in a row, in the four combinations of immediately: on master the two ending with suspend(true) hang, with this change all of them return;
  • submit() of 5 tasks followed by shutDown(true) or close(), 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 from interruptTestOne, 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; enabling discardingTasks 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.
  • When a suspension and a resumption run at the same time in different threads, the caller of suspend(true) can still wait forever if the resumption comes before the launcher has read the flag.

🤖 Generated with Claude Code

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.
@Roberto-Gentili
Roberto-Gentili merged commit f3a4b1e into master Nov 29, 2026
117 of 118 checks passed
@Roberto-Gentili
Roberto-Gentili deleted the claude/repeated-suspension branch November 29, 2026 17:30
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