GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Don't strand tasks when trySuspend() races with startThread() by allthingssecurity · Pull Request #17608 · netty/netty

Conversation

@allthingssecurity

@allthingssecurity allthingssecurity commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Motivation:

Since #15766, trySuspend() moves an executor that was never started directly from ST_NOT_STARTED to ST_SUSPENDED. startThread() reads the state once and then does a single CAS from that value to ST_STARTED:

int currentState = state;
if (currentState == ST_NOT_STARTED || currentState == ST_SUSPENDED) {
    if (STATE_UPDATER.compareAndSet(this, currentState, ST_STARTED)) {

If trySuspend() runs between the read and the CAS, the CAS fails and startThread() returns without starting a thread. The task that execute() just added then sits in the queue of an executor that reports isSuspended() == true and has no thread. It only runs if something else is submitted later.

AutoScalingEventExecutorChooserFactory can hit this: its monitor calls trySuspend() on idle executors that were never started, while next() may still hand the same executor out. A Channel registration on such an event loop never completes, and because the loop is no longer in the active set, nothing else is likely to wake it.

Modification:

Re-read the state and retry the CAS in startThread() while the state is still ST_NOT_STARTED or ST_SUSPENDED (the if becomes a while, plus a break after a successful start). The loop only retries when another thread changed the state between the read and the CAS, so it cannot spin: each extra round needs a new state change by someone else. It can take more than two rounds, though. For example, the CAS loses to trySuspend() (ST_NOT_STARTED to ST_SUSPENDED), then another thread's startThread() wins, its doStartThread() fails and it resets the state to ST_NOT_STARTED, and then this thread retries again. Any state other than ST_NOT_STARTED or ST_SUSPENDED means another thread started the executor, the old thread re-engaged, or the executor is shutting down, and each of those already takes care of the queued task.

The retry may deliberately un-suspend an executor that trySuspend() has just suspended. That is the same thing execute() already does for an executor that is suspended, and the code comment at the retry says so.

Add SingleThreadEventExecutorTest.testExecuteRacingTrySuspendWhenExecutorIsNotStarted. The race cannot be forced deterministically because there is no hook between the state read and the CAS, so the test repeats execute() against a concurrent trySuspend() on fresh executors 10000 times. The executors use an Executor that only counts start requests, so no threads are created and the check (exactly one start request per execute()) is exact. It takes about 0.1s.

The test is regression-sensitive, not deterministic. Without the fix it needs the two threads to actually run in parallel and hit the window between the read and the CAS, so it may pass on machines with only 1 or 2 CPUs. With the fix it cannot fail falsely: every interleaving leads to exactly one start request.

Result:

A task added by execute() always gets a thread, even if it races with trySuspend().

This regressed in #15766.

Testing:

  • Without the fix the new test failed in every run on a 12-CPU machine, typically within the first 20 to 140 iterations, for example:
    AssertionFailedError: iteration 69 (isSuspended=true) ==> expected: <1> but was: <0>
    As said above, it may pass without the fix on machines with 1 or 2 CPUs.
  • With the fix: SingleThreadEventExecutorTest 16 tests, 0 failures; common module 1449 tests, 0 failures, 19 skipped; checkstyle passes.
  • Standalone harnesses against the real classes: execute() racing trySuspend() stranded the task in 5445 of 300000 iterations before the fix and 0 of 300000 after. SingleThreadIoEventLoop.register(channel) on NIO racing trySuspend() hung 39 of 4000 registrations before and 0 of 3000 after.

Interaction with open PRs:

  • Auto-port 4.2: SingleThreadEventExecutor: fix race between suspend and cancellation … #17601 (port of SingleThreadEventExecutor: fix race between suspend and cancellation … #17589) changes the post-run() decision in doStartThread(), a different code path (a running thread that is suspending). The two merge cleanly, and SingleThreadEventExecutorTest passes with both (17 tests). The model below was checked with both fixes applied.
  • SingleThreadEventExecutor: do not start threads when shutting down never-used executors #17183 and Update state in SingleThreadEventExecutor from SUSPENDING to STARTED on new work #15813 also edit startThread() and conflict textually, since this change turns the if into a while. Their extra else if branch moves after the loop as a plain if on the last observed state. With that resolution the tests pass for this change with SingleThreadEventExecutor: do not start threads when shutting down never-used executors #17183 (23 tests) and with Update state in SingleThreadEventExecutor from SUSPENDING to STARTED on new work #15813 (SingleThreadEventExecutorTest and AutoScalingEventExecutorChooserFactoryTest, 24 tests).

This was found with a TLA+ model of the executor state machine (liveness property "an accepted task eventually runs" violated) and reproduced with the real classes. With the retry, the model passes MutualExclusion, RunningHasThread and TaskRuns/TaskResolved. It was checked alone and together with the #17589 fix (where it also passes NoSpuriousShutdown), with 2 submitters, up to 2 trySuspend() calls, graceful shutdown and scheduled-task cancellation, starting from both ST_NOT_STARTED and ST_STARTED, up to 1.44M distinct states.

The change was prepared with Claude Code (Claude Opus 5.5), and the commit has a Co-Authored-By trailer.

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@allthingssecurity @claude
Don't strand tasks when trySuspend() races with startThread()
8693126
Motivation:

Since netty#15766, trySuspend() moves an executor that was never started
directly from ST_NOT_STARTED to ST_SUSPENDED. startThread() reads the
state once and then does a single CAS from that value to ST_STARTED.
If trySuspend() runs between the read and the CAS, the CAS fails and
startThread() returns without starting a thread. The task that
execute() just added then stays in the queue of a suspended executor
that has no thread, and only runs if something else is submitted
later.

AutoScalingEventExecutorChooserFactory can hit this, as its monitor
suspends idle executors that were never started while next() may
still hand them out. A Channel registration on such an event loop
never completes.

Modification:

Re-read the state and retry the CAS in startThread() while the state
is still ST_NOT_STARTED or ST_SUSPENDED. The loop only retries when
another thread changed the state between the read and the CAS, so it
cannot spin. It can take more than two rounds, for example when the
CAS loses to trySuspend(), then another thread's start fails and
resets the state to ST_NOT_STARTED, and then this thread retries
again. Any state other than ST_NOT_STARTED or ST_SUSPENDED means
another thread started the executor or is shutting it down.

The retry may un-suspend an executor that trySuspend() has just
suspended. That is intended, and is what execute() on a suspended
executor already does.

Add a stress test that races execute() with trySuspend() on
executors that were never started. The race can't be forced, so the
test is regression-sensitive rather than deterministic: without the
fix it may pass on machines with 1-2 CPUs, but with the fix it can't
fail.

Result:

A task added by execute() always gets a thread, even if it races with
trySuspend().

This regressed in netty#15766.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
normanmaurer
normanmaurer approved these changes Sep 24, 2026
View reviewed changes
@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Sep 28, 2026
@normanmaurer
Merge branch '4.2' into fix-stee-start-thread-suspend-race
59f6f7e
@normanmaurer normanmaurer added the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Sep 28, 2026
@normanmaurer
normanmaurer requested a review from franz1981 September 28, 2026 12:09
franz1981
franz1981 approved these changes Sep 28, 2026
View reviewed changes

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@testlens-app

testlens-app Bot commented Sep 28, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 59f6f7e
▶️ Tests: 424241 executed
⚪️ Checks: 27/27 completed


Learn more about TestLens at testlens.app/docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer
normanmaurer merged commit c4e7d1e into netty:4.2 Sep 28, 2026
28 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Sep 28, 2026
Auto-port 5.0: Don't strand tasks when trySuspend() races with startThread() #17654
Merged
@netty-project-bot

netty-project-bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17654

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Sep 28, 2026
normanmaurer added a commit that referenced this pull request Sep 29, 2026
@netty-project-bot @allthingssecurity @normanmaurer
Auto-port 5.0: Don't strand tasks when trySuspend() races with startT…
ff9e407
…hread() (#17654)

Auto-port of #17608 to 5.0
Cherry-picked commit: c4e7d1e

---
Motivation:

Since #15766, `trySuspend()` moves an executor that was never started
directly from `ST_NOT_STARTED` to `ST_SUSPENDED`. `startThread()` reads
the state once and then does a single CAS from that value to
`ST_STARTED`:

```java
int currentState = state;
if (currentState == ST_NOT_STARTED || currentState == ST_SUSPENDED) {
    if (STATE_UPDATER.compareAndSet(this, currentState, ST_STARTED)) {
```

If `trySuspend()` runs between the read and the CAS, the CAS fails and
`startThread()` returns without starting a thread. The task that
`execute()` just added then sits in the queue of an executor that
reports `isSuspended() == true` and has no thread. It only runs if
something else is submitted later.

`AutoScalingEventExecutorChooserFactory` can hit this: its monitor calls
`trySuspend()` on idle executors that were never started, while `next()`
may still hand the same executor out. A `Channel` registration on such
an event loop never completes, and because the loop is no longer in the
active set, nothing else is likely to wake it.

Modification:

Re-read the state and retry the CAS in `startThread()` while the state
is still `ST_NOT_STARTED` or `ST_SUSPENDED` (the `if` becomes a `while`,
plus a `break` after a successful start). The loop only retries when
another thread changed the state between the read and the CAS, so it
cannot spin: each extra round needs a new state change by someone else.
It can take more than two rounds, though. For example, the CAS loses to
`trySuspend()` (`ST_NOT_STARTED` to `ST_SUSPENDED`), then another
thread's `startThread()` wins, its `doStartThread()` fails and it resets
the state to `ST_NOT_STARTED`, and then this thread retries again. Any
state other than `ST_NOT_STARTED` or `ST_SUSPENDED` means another thread
started the executor, the old thread re-engaged, or the executor is
shutting down, and each of those already takes care of the queued task.

The retry may deliberately un-suspend an executor that `trySuspend()`
has just suspended. That is the same thing `execute()` already does for
an executor that is suspended, and the code comment at the retry says
so.

Add
`SingleThreadEventExecutorTest.testExecuteRacingTrySuspendWhenExecutorIsNotStarted`.
The race cannot be forced deterministically because there is no hook
between the state read and the CAS, so the test repeats `execute()`
against a concurrent `trySuspend()` on fresh executors 10000 times. The
executors use an `Executor` that only counts start requests, so no
threads are created and the check (exactly one start request per
`execute()`) is exact. It takes about 0.1s.

The test is regression-sensitive, not deterministic. Without the fix it
needs the two threads to actually run in parallel and hit the window
between the read and the CAS, so it may pass on machines with only 1 or
2 CPUs. With the fix it cannot fail falsely: every interleaving leads to
exactly one start request.

Result:

A task added by `execute()` always gets a thread, even if it races with
`trySuspend()`.

This regressed in #15766.

Testing:

- Without the fix the new test failed in every run on a 12-CPU machine,
typically within the first 20 to 140 iterations, for example:
`AssertionFailedError: iteration 69 (isSuspended=true) ==> expected: <1>
but was: <0>`
As said above, it may pass without the fix on machines with 1 or 2 CPUs.
- With the fix: `SingleThreadEventExecutorTest` 16 tests, 0 failures;
`common` module 1449 tests, 0 failures, 19 skipped; checkstyle passes.
- Standalone harnesses against the real classes: `execute()` racing
`trySuspend()` stranded the task in 5445 of 300000 iterations before the
fix and 0 of 300000 after. `SingleThreadIoEventLoop.register(channel)`
on NIO racing `trySuspend()` hung 39 of 4000 registrations before and 0
of 3000 after.

Interaction with open PRs:

- #17601 (port of #17589) changes the post-`run()` decision in
`doStartThread()`, a different code path (a running thread that is
suspending). The two merge cleanly, and `SingleThreadEventExecutorTest`
passes with both (17 tests). The model below was checked with both fixes
applied.
- #17183 and #15813 also edit `startThread()` and conflict textually,
since this change turns the `if` into a `while`. Their extra `else if`
branch moves after the loop as a plain `if` on the last observed state.
With that resolution the tests pass for this change with #17183 (23
tests) and with #15813 (`SingleThreadEventExecutorTest` and
`AutoScalingEventExecutorChooserFactoryTest`, 24 tests).

This was found with a TLA+ model of the executor state machine (liveness
property "an accepted task eventually runs" violated) and reproduced
with the real classes. With the retry, the model passes
`MutualExclusion`, `RunningHasThread` and `TaskRuns`/`TaskResolved`. It
was checked alone and together with the #17589 fix (where it also passes
`NoSpuriousShutdown`), with 2 submitters, up to 2 `trySuspend()` calls,
graceful shutdown and scheduled-task cancellation, starting from both
`ST_NOT_STARTED` and `ST_STARTED`, up to 1.44M distinct states.

Co-authored-by: allthingssecurity <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
@allthingssecurity allthingssecurity mentioned this pull request Sep 29, 2026
SingleThreadEventExecutor: do not start threads when shutting down never-used executors #17183
Closed
@allthingssecurity
allthingssecurity deleted the fix-stee-start-thread-suspend-race branch October 1, 2026 10:15
arimu1 added a commit to arimu1/netty that referenced this pull request Oct 3, 2026
@arimu1
Merge branch '4.2' into fix/17135-shutdown-unstarted-threads
68879cd
Keep the netty#17608 startThread retry loop, and run the never-started
SHUTTING_DOWN worker start only after that loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@normanmaurer normanmaurer normanmaurer approved these changes
@franz1981 franz1981 franz1981 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.19.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@allthingssecurity @netty-project-bot @normanmaurer @franz1981
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.