Skip to content

Defer SpotlessCache clearing from clean to SpotlessTaskService.close() - #3210

Open
lyn1dev wants to merge 1 commit into
diffplug:mainfrom
lyn1dev:fix/3067-clean-spotless-cache-race
Open

lyn1dev wants to merge 1 commit into
diffplug:mainfrom
lyn1dev:fix/3067-clean-spotless-cache-race

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 25, 2026

Copy link
Copy Markdown

Fixes #3067

Root Cause

SpotlessPlugin registered a doLast action on each subproject's clean task that called SpotlessCache.clearOnce(System.identityHashCode(project.getRootProject())) immediately during task execution.

This caused two issues:

  1. Concurrent classloader closure under --parallel: mustRunAfter("clean") only orders tasks within the same subproject. When :b:clean finishes while :a:spotless<Format> is running concurrently in the same Gradle daemon, SpotlessCache.clear() closes active FeatureClassLoader instances mid-format, resulting in ClassNotFoundException / NoClassDefFoundError (e.g., LINE_UNDEFINED lint errors).
  2. Broken deduplication key under configuration cache and Gradle 9: Serializing System.identityHashCode(project.getRootProject()) into the configuration cache causes subsequent clean builds reusing the configuration cache to pass the same cacheKey to SpotlessCache.clearOnce(cacheKey), skipping the clear entirely. In Gradle 9, project.getRootProject() can also return distinct wrapper instances per subproject.

Changes

  • SpotlessTaskService: Added a clearCacheOnClose flag set via clearCacheOnClose() and checked in close() (finally block after ratchet.close()), calling SpotlessCache.clearOnce(null) so the cache is cleared once at the end of the build after all tasks have completed without retaining a reference to the closed BuildService. Widened usesServiceTolerateTestFailure to accept Task.
  • SpotlessPlugin: Replaced the immediate SpotlessCache.clearOnce(cacheKey) doLast lambda on clean with a static ClearSpotlessCacheOnCloseAction registered alongside usesServiceTolerateTestFailure(clean, taskService), preserving configuration-cache and isolated-projects compatibility.
  • plugin-gradle/CHANGES.md: Added an entry under ## [Unreleased] -> ### Fixed.
  • CleanSpotlessCacheRaceTest: Added Gradle TestKit regression tests verifying:
    • A parallel :b:clean completing while :a:spotlessMisc is mid-execution does not close the in-use FeatureClassLoader.
    • clean still clears SpotlessCache at build end on both configuration-cache misses and configuration-cache hits.
    • clean + spotlessApply/spotlessCheck continue to work with org.gradle.unsafe.isolated-projects=true.

Verification

  • Confirmed the parallel-clean and configuration-cache-reuse regression tests fail without the production changes and pass with the fix applied.
  • Ran :plugin-gradle:spotlessCheck, :changelogCheck, and :plugin-gradle:test covering CleanSpotlessCacheRaceTest, IsolatedProjectTest, ConfigurationCacheTest, ConfigAvoidanceTest, MultiProjectTest, SpotlessTaskImplTest, and CompositeBuildRunToFixMessageTest (23 tests passed, 0 failures).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant