Skip to content

Finalizer tasks are not run when the build is cancelled #28399

@CLOVIS-AI

Description

@CLOVIS-AI

Current Behavior

Let's imagine we have two tasks:

  • a
  • b

and we have declared that task("a") { finalizedBy("b") }.

If we cancel the build (CTRL-C in the CLI, stop button in IDEA) while a is running, both a and b are marked as cancelled.

Expected Behavior

a should be cancelled, but b should run anyway.

Context (optional)

Finalizer tasks are useful in situations where the build creates a resource that has to be cleaned up regardless of the build failing or succeeding. An example of such a resource is a web container that is started before an integration test task and which should be always shut down, even if some of the tests fail.
https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:finalizer_tasks

I would expect that long-running tasks that start servers would fall under the same use-case, and thus would also need to be finalized even if they are cancelled before finishing.

Steps to Reproduce

// build.gradle.kts

val a by tasks.registering {
   doLast {
        Thread.sleep(10000)
    }

    finalizedBy(b)
} 

val b by tasks.registering {
    doLast {
        println("Ran")
    }
}

Gradle version

8.3

Build scan URL (optional)

No response

Your Environment (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed:duplicateDuplicated or superseeded by another issuein:execution-engineincremental, up-to-date, overlapping outputs

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions