finalizedBy and failures of dependent tasks

A similar question was raised in Finalizer tasks that depend on the base task do not execute if the base task fails, but I have a different use case.

task perfTest(dependsOn: ['jmClean', 'jmRun'],
    description:'Runs (cleanly) performance tests on a deployed application and collects the app log if available. ' +
    'Use --no-daemon to see progress. Use --info to see all JMeter command-line arguments.')
perfTest.finalizedBy 'collectLogs'

What I want to do here is to collect logs even if performance testing fails. So, I used finalizedBy and was puzzled about why in the world it was not working on failures. After some poking, I figured that it is not working because the failure actually occurs in the dependent jmRun task. So, the fix is this:

task perfTest(dependsOn: ['jmClean', 'jmRun'],
    description:'Runs (cleanly) performance tests on a deployed application and collects the app log if available. ' +
    'Use --no-daemon to see progress. Use --info to see all JMeter command-line arguments.')
jmRun.finalizedBy 'collectLogs' // Have to finalize jmRun rather than perfTest

This is OK, but a bit not what I expected from finalizedBy. I expected sort-of try/finally functionality for perfTest task. Is a defect in order?

This is the intended behavior. The finalizer task (the task passed to finalizedBy) won’t be run if the finalized task never executes. The typical use case is to cleanup resources potentially allocated by a specific task. If that task never runs, there is nothing for the finalizer task to do. So the answer is yes, the task that actually fails is the one that needs to have the finalizedBy dependency added to it.

OK, I can leave with that. It might make sense to mention this scenario in docs.