Add FindThreadStartInConstructor (Sonar S2693) - #1238
Merged
steve-aom-elliott merged 1 commit intoSep 15, 2026
Merged
Conversation
Search recipe for the shape flagged by sonar-java ThreadStartedInConstructorCheck (RSPEC-S2693): Thread.start() reached during construction of a non-final class. The new thread can observe a partially-initialised object, and any subclass' fields are guaranteed unset because the superclass constructor starts the thread before the subclass' own initialisation runs. Fits alongside FindVirtualThreadOpportunities in org.openrewrite.java.migrate.lang — both are "your thread management could be modernised" search recipes; S2693's remedy is to move the start() out of the construction path and use an ExecutorService. Detection walks getCursor().getPath() from a Thread.start() call outward to find the enclosing frame: - J.Lambda -> not construction (deferred) - J.Block with isStatic() -> static initializer, not construction - J.MethodDeclaration - constructor -> check enclosing class - regular method -> not construction - J.NewClass with body -> anonymous class, effectively final - J.ClassDeclaration (first hit) -> instance field init / init block -> check class MethodMatcher uses matchOverrides=true so Thread subclasses fire on myThread.start() too. Records and enums are treated as effectively final. Tagged RSPEC-S2693. No CWE — sonar-java doesn't map this rule to one either.
steve-aom-elliott
force-pushed
the
add-rspec-s2693-thread-start-in-constructor
branch
from
September 15, 2026 15:53
a0b9f89 to
d6f8415
Compare
steve-aom-elliott
deleted the
add-rspec-s2693-thread-start-in-constructor
branch
September 15, 2026 16:05
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.
Adds a search recipe covering S2693 — Threads should not be started in constructors.
Starting a thread before construction completes lets the new thread observe a partially-initialised receiver. The problem is worse in a non-
finalclass: the superclass constructor starts the thread before the subclass has run any of its own initialisation, guaranteeing the subclass' fields are unset.Fits alongside
FindVirtualThreadOpportunitiesinorg.openrewrite.java.migrate.lang— both are "your thread management could be modernised" search recipes; S2693's remedy is to move thestart()out of the construction path and delegate to anExecutorService.What it flags
Mirrors sonar-java's
ThreadStartedInConstructorCheck: aThread.start()invocation whose enclosing scope, in a non-finalclass, is one of:Thread t = ...; t.start();at field-init position){ ... t.start(); ... })Detection walks
getCursor().getPath()outward from the call site:J.LambdaJ.BlockwithisStatic()J.MethodDeclarationJ.NewClasswith bodyJ.ClassDeclaration(first, before any method)The enclosing-class check treats records and enums as effectively final — they can't be extended in the way S2693 cares about.
MethodMatcher("java.lang.Thread start()", true)usesmatchOverrides=truesomyThread.start()on aThreadsubclass instance fires too.Tagged
RSPEC-S2693only. Sonar-java's rule metadata has no CWE for S2693 — it's a design-quality issue rather than a specific security weakness.Known limitations (not flagged)
Runnable.run()/ExecutorService.submit()from a constructor — same "leaksthis" risk, different rule surface.start()called from a helper method invoked by the constructor — cross-method flow isn't tracked. Sonar-java has the same intra-method-only behaviour.new Thread(this).start()isn't specifically distinguished from plaint.start()— sonar-java doesn't inspect theRunnabletarget either.Tests
9 tests, all passing.