This issue comes when there are more than one process which is doing something with a file in your disk (whether it's your Jenkins job's workspace or Gradle's cache path or Gradle build folder that it creates) where these processes are trying to do something with a file and holds a lock.
If you have anti-virus, you may see this. Un-installing may resolve the issue but then you don't have anti-virus (it's better if you can set something in anti-virus to scan files periodically).
In my case, this issue only comes when I have a Jenkinsfile
pipeline, where I'm running Gradle task for 3-5 projects in parallel
(let's say Gradle is running an rpm
task or build
task) and when I'm running concurrent runs of this pipeline.
As you can see my pipeline code (Jenkinsfile Pipeline DSL: How to Show Multi-Columns in Jobs dashboard GUI - For all Dynamically created stages - When within PIPELINE section) is creating dynamic stage(s) and when I call it within pipeline
section, I use parallel
to run N no. of projects (that I can define in a hash/array) gradle tasks in parallel.
What I noticed is, all these runs (running in parallel) is running using my user-id (or some service account) and they are all using same Gradle version (4.3.1 in my case) and thus, they are using the same cache.
Running them in parallel alone --OR-- more realistic case would be running multiple pipeline runs of this pipeline job (running 5 project's gradle task in parallel) would probably land into this LOCK file condition.
You can probably solve this by setting a unique GRADLE_USER_HOME="~${WORKSPACE}/.cache"
(i.e. use Jenkins job's workspace which is dynamically created and is unique) or even better by setting Gradle user home as:
dt_stamp=`date +%s`; ## set and pass this dt_stamp variable for Gradle user home before calling Gradle.
GRADLE_USER_HOME="~${WORKSPACE}/${dt}/.cache"`
This way, at least in Gradle's cache, you won't see any issues due to lock files (NOTE: You'll not be able to efficiently use Gradle's cache concept of being lazy
(or not fetching artifacts/library dependencies from a binary repository like Artifactory each time) while using Gradle among many projects if you set GRADLE_USER_HOME to a dynamic folder value as it'd be like running gradle after clearing it's cache each time (for some it's OK if your build doesn't take much time and it's more cleaner way to build as well)).
When I tried to run just one pipeline run (which runs all 5 project's gradle task in paralle), the pipeline succeeds all the time.
Running multiple instances of the pipeline job (running 5 parallel builds in each run) lands into this file LOCK issue.
Error mesg when I ran many concurrent pipeline runs (each pipeline run - runs Gradle tasks in parallel on 5 different projects).
FAILURE: Build failed with an exception.
* What went wrong:
Failed to capture snapshot of output files for task ':rpm' property 'archivePath' during up-to-date check.
> Timeout waiting to lock file hash cache (/view/user123456_Team_Tools/vobs/space/test/folder1/Project1of5/.gradle/4.3.1/fileHashes). It is currently in use by another Gradle instance.
Owner PID: 29003
Our PID: 903
Owner Operation:
Our operation:
Lock file: /view/user123456_Team_Tools/vobs/space/test/folder1/Project1of5/.gradle/4.3.1/fileHashes/fileHashes.lock
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
-- OR --
You may also get this kind of error where a process is not able to delete a directory (while Gradle is in progress as the target folder/file may be in use by some other process)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':clean'.
> Unable to delete directory: /view/user123456_Team_Tools/vobs/space/test/folder1/project2of5/build/classes/java/main/org/syntax/jedit
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
If you are really lucky (like me), then you'll probably hit this error as well:
Jenkins - java.lang.IllegalArgumentException: Last unit does not have enough valid bits &
Gradle error: Task 'null' not found in root project
See here for more details: Jenkins - java.lang.IllegalArgumentException: Last unit does not have enough valid bits & Gradle error: Task 'null' not found in root project