No worker lease associated with the current thread

I’m upgrading Gradle from version 6 to 7.4 and for one of the tasks I’m getting the following exception:

No worker lease associated with the current thread
org.gradle.internal.work.NoAvailableWorkerLeaseException: No worker lease associated with the current thread
        at org.gradle.internal.work.DefaultWorkerLeaseService.getCurrentWorkerLease(DefaultWorkerLeaseService.java:91)
        at org.gradle.internal.operations.DefaultBuildOperationQueueFactory.create(DefaultBuildOperationQueueFactory.java:33)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.executeInParallel(DefaultBuildOperationExecutor.java:132)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.runAll(DefaultBuildOperationExecutor.java:102)
...

the code responsible for this:

test {
...
    // #gradle7 - this test fail
     afterSuite {
         project.javaexec {
             main = "TestSuiteCatalogCategoriesExtractor"
             classpath = sourceSets.test.runtimeClasspath
             systemProperties = ["testsuitecatalog.extractor.reportfile":reportDestinationFile]
             args = testMethodsClasses
         }
        print("Scanned ${testMethodsClasses.size()} test class details, final report saved to ${reportDestinationFile}")
     }
}

The task works well with Gradle 6.x and below. It fails with current gradle (7.4.2).

Appreciate any help.
Radek