lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael McCandless (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LUCENE-5738) NativeLock is release if Lock is closed after obtain failed
Date Thu, 05 Jun 2014 17:22:01 GMT

    [ https://issues.apache.org/jira/browse/LUCENE-5738?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14018987#comment-14018987
] 

Michael McCandless commented on LUCENE-5738:
--------------------------------------------

Simple (non-Lucene) Java test repros the issue:

{noformat}

import java.io.File;
import java.io.IOException;

import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

import java.nio.channels.OverlappingFileLockException;
import java.nio.file.StandardOpenOption;

public class TestLock {
    public static void obtain() throws Exception {
        File path = new File("/tmp/LOCK");
        FileChannel channel = FileChannel.open(path.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        System.out.println("got channel " + channel);
        FileLock lock = null;
        try {
            lock = channel.tryLock();
        } catch (IOException | OverlappingFileLockException e) {
            // failed to get lock
        } finally {
            if (lock == null) {
                System.out.println("FAILED");
                channel.close();
             } else {
                System.out.println("SUCCESS");
            }
        }
    }

    public static void main(String[] foo) throws Exception {
        obtain();
        obtain();
        Thread.sleep(Integer.MAX_VALUE);
    }
}
{noformat}


> NativeLock is release if Lock is closed after obtain failed
> -----------------------------------------------------------
>
>                 Key: LUCENE-5738
>                 URL: https://issues.apache.org/jira/browse/LUCENE-5738
>             Project: Lucene - Core
>          Issue Type: Bug
>    Affects Versions: 4.8.1
>            Reporter: Simon Willnauer
>             Fix For: 4.9, 5.0
>
>
> if you obtain the NativeFSLock and try to obtain it again in the same JVM and close if
if it fails another process will be able to obtain it. This is pretty trappy though. If you
execute the main class twice the problem becomes pretty obvious.
> {noformat}
> import org.apache.lucene.store.Lock;
> import org.apache.lucene.store.NativeFSLockFactory;
> import java.io.File;
> import java.io.IOException;
> public class TestLock {
>  public static void main(String[] foo) throws IOException, InterruptedException {
>         NativeFSLockFactory lockFactory = new NativeFSLockFactory(new File("/tmp"));
>         Lock lock = lockFactory.makeLock("LOCK");
>         if (lock.obtain()) {
>             System.out.println("OBTAINED");
>         } else {
>             lock.close();
>             System.out.println("FAILED");
>         }
>         // try it again and close it if it fails
>         lock = lockFactory.makeLock("LOCK"); // <<<<==== this is a new lock
>         if (lock.obtain()) {
>             System.out.println("OBTAINED AGAIN");
>         } else {
>             lock.close(); // <<<<==== this releases the lock we obtained
>             System.out.println("FAILED on Second");
>         }
>         Thread.sleep(Integer.MAX_VALUE);
>     }
> }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.2#6252)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Mime
View raw message