Skip to content

Running multiple Detekt tasks concurrently may cause deadlock #3047

@damianw

Description

@damianw

Context

In my Android app, I have configured separate Detekt tasks for each variant, including test variants, with the full dependency classpath for each (e.g. detektDebug, detektDebugAndroidTest, etc).

Expected Behavior

I am trying to run multiple at the same time, e.g. ./gradlew detektDebug detektDebugAndroidTest, which Gradle (correctly) infers can happen concurrently.

Observed Behavior

Running multiple Detekt tasks concurrently leads to a deadlock in certain situations. The problem appears to be with ClassLoaderCache: FileCollection.files resolves the included files, but also (unexpectedly to me) blocks until the dependencies are satisfied if they haven't been already. By calling files while being synchronized on ClassLoaderCache, other Detekt invocations become blocked waiting on the monitor, which results in the first Detekt never having its dependencies satisfied, leading to a deadlock.

Steps to Reproduce

I don't have a code sample at the moment (but can provide if necessary) since the configuration and setup would be non-trivial to extract, but the gist is:

  • Register a Detekt task with a classpath containing the classpath of the project's main source set / variant
  • Register a Detekt task with a classpath containing the classpath the project's test source set / variant (I think this is important, based on the fact that the test Detekt task will depend on the classpath of the main source set / variant)
  • Invoke Gradle with both tasks as goals (and sufficient cores/workers to run multiple concurrently)

A thread dump for when I have observed this occurring can be found here:

If I were to provide a suggestion, I think that moving the call to FileCollection.files outside of the synchronized block would be the simplest fix.

Your Environment

  • Version of detekt used: 1.12.0
  • Version of Gradle used (if applicable): 6.5
  • Operating System and version: Mac OS 10.15.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions