Error "Could not read path" on a broken symlink when copying using Copy task

We have a directory “bin” which, because of processes outside of our direct control, might contain broken symlinks. We have a task ‘deleteBrokenSymlinksFromBin’ to attempt to clear these out:

task deleteBrokenSymlinksFromBin {
doLast {
    File binDir = new File("${rootDir}/install/server/x64/bin")
    binDir.listFiles()
            .collect { it.toPath() }
            .findAll { Files.isSymbolicLink(it) && !Files.exists(it) }
            .each {
                try {
                    logger.info("Deleting broken symlink on path ${it}")
                    Files.delete(it)
                } catch (Exception e) {
                    logger.error("There was an error deleting a symlink from bin ${it}")
                }
            }
   }
}

)
We have a Copy task, “deployBin” which depends on deleteBrokenSymlinksFromBin. On Linux this task reports “Could not read path” errors mentioning paths of symlinks the previous task should have deleted.
Should the above code work? We’re struggling to reason about exactly when the inputs/outputs on the copy task are populated & evaluated.

// Log
:workgroup:deployBin FAILED [17:09:39][BuildServer] [17:09:39][BuildServer] FAILURE: Build failed with an exception. [17:09:39][BuildServer] [17:09:39][BuildServer] * What went wrong: [17:09:39][BuildServer] Could not read path '/data/tc/near/shared/install/server/x64/bin/.libtabmixins.so.debug.caUR6r'. [17:09:39][BuildServer] > /data/tc/near/shared/install/server/x64/bin/.libtabmixins.so.debug.caUR6r [17:09:39][BuildServer]

I’m unable to reproduce the issue using a copy task. Could you describe how the inputs to the copy task are set?

We configure input by calling ‘configure’ on the task and passing it a closure such as

{ 
    from configurations.default {
        include ‘tab-*.jar’
    }
}

I got this resolved. There was a parallel task running which copied symlinks into the bin folder. I made that task wait for this (deployBin) task to complete and it all works fine.

To conclude, the task ‘deleteBrokenSymlinksFromBin’ worked as expected and there was no issue with it to begin with.