e7andy
e7andy

Reputation: 58

What indicates if an Eclipse application workspace is locked/in use or not?

When I start my Eclipse application (with my custom plugins) for the first time a .lock file is created in the workspace folder .metadata. If I try to start a second application I get the correct error message "Workspace cannot be locked". The .lock file prevents me from starting multiple applications with the same workspace.

And if I manually delete the .lock file while an application is running and then try to start a second one it works. That is ok since I manually removed the file that prevents me from that.

Here is the strange part:

When I close the application the .lock file is not removed, but I can still start another Eclipse application so it doesn't seem to care about the .lock file at that point.

Why isn't the .lock file removed after the application is shutdown and how does it know that the workspace is not in use?

How can I manually check if a workspace is in use? The .lock file is apparently not a safe indicator.

Upvotes: 2

Views: 426

Answers (2)

greg-449
greg-449

Reputation: 111142

Eclipse does use the .lock file but it also acquires an actual 'lock' on the file. This is usually done using the Java FileChannel tryLock method which acquires an operating system level lock on the file:

RandomAccessFile raFile = new RandomAccessFile(lockFile, "rw");     

FileLock fileLock = raFile.getChannel().tryLock(0, 1, false);

The tryLock method throws an OverlappingFileLockException if the file is already locked by another Eclipse.

When Eclipse shuts down the lock is released and the file closed.

It is not necessary to delete the lock file so this is not done.

There is an environment variable osgi.locking which can be used to change this behavior.

Full code is in org.eclipse.osgi.internal.location.Locker_JavaNio

Upvotes: 1

E-Riz
E-Riz

Reputation: 32895

My understanding of the lock file mechanism is this:

  • When a workspace is opened in an instance of Eclipse, Eclipse first checks for the existence of a lock file. If one is present it tries to delete it.
    • Success deleting the lock file indicates that no currently-running instance of Eclipse is using that workspace, so this instance can continue on; it creates a new lock file, holding it open while that instance is running.
    • Failure to delete the lock file indicates that some instance of Eclipse is currently running using that workspace; since that other instance still has the file "open" the new instance can't delete it.
  • On normal shutdown, Eclipse releases the file, but does not neecssarily delete it. Thus, later Eclipse instances are free to delete it upon start-up.
  • If Eclipse crashes or does not shutdown normally, the lock file is also released (since the Eclipse process that held it is terminated), again allowing later instances to delete it during their startup.

Upvotes: 1

Related Questions