Skip to content

java.nio.channels.OverlappingFileLockException for Jacoco offline #331

@david-wei-wei

Description

@david-wei-wei

Hi, recently I used Jacoco 0.7.2 to test our AppServer, Encountered such a problem:

java.nio.channels.OverlappingFileLockException 
at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255) 
at sun.nio.ch.SharedFileLockTable.add(FileLockTable.java:152) 
at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:1030) 
at java.nio.channels.FileChannel.lock(FileChannel.java:1053) 
at org.jacoco.agent.rt.internal_e6e56f0.output.FileOutput.openFile(FileOutput.java:69) 
at org.jacoco.agent.rt.internal_e6e56f0.output.FileOutput.writeExecutionData(FileOutput.java:53) 
at org.jacoco.agent.rt.internal_e6e56f0.Agent.shutdown(Agent.java:137) 
at org.jacoco.agent.rt.internal_e6e56f0.Agent$1.run(Agent.java:54) 

In Jacoco Offline, are there different threads to synchronisation/write locking for jacoco.exec in one JVM? In order to avoid concurrent writes from different threads running in parallel in one JVM, I changed some code, it can solve our problem temporarily, as below:

private OutputStream openFile() throws IOException {
  final FileOutputStream file = new FileOutputStream(destFile, append);
  int retries = 5;//retries 5 times
  // Avoid concurrent writes from different agents running in parallel:
  for (int t = 0; t < retries; t++)
  try{
      file.getChannel().lock();
      break;
  }catch(OverlappingFileLockException ofe){
      System.out.println("[JACOCO][RT] Got OverlappingFileLockException, re-trying.");
      try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                //ignore
            }
  }
  return file;
 }

Please help to check the issue.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions