FULL PRODUCT VERSION :
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
"This is a confusing bug report. I think the submitter is missing the main point of file locks, that is file locks are for coordinating between programs to the same file. In the same test case there is only one program and the spec is clear that there is no guarantee that a file lock prevents access to the locked region. It is also clear that file locks cannot be used to coordinate access to a file in the same or multiple threads in the same virtual machine."
NO!
Even a different virtual machine occurs.
logfile was null filled.
REGRESSION. Last worked in version 8u20
ADDITIONAL REGRESSION INFORMATION:
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
[Console-1]
D:\workspace\LockTest>java -cp bin locktest.Main 1
1
Sat Feb 21 23:51:02 JST 2015
Sat Feb 21 23:51:03 JST 2015
Sat Feb 21 23:51:04 JST 2015
Sat Feb 21 23:51:05 JST 2015
Sat Feb 21 23:51:06 JST 2015
Sat Feb 21 23:51:07 JST 2015
Sat Feb 21 23:51:08 JST 2015
Sat Feb 21 23:51:09 JST 2015
Sat Feb 21 23:51:10 JST 2015
[Console-2]
D:\workspace\LockTest>java -cp bin locktest.Main 2
2
Exception in thread "main" java.io.IOException: プロセスはファイルにアクセスできません。別のプロセスがファイルの一部をロックしてい
ます。
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.FileDispatcherImpl.write(Unknown Source)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.write(Unknown Source)
at sun.nio.ch.FileChannelImpl.write(Unknown Source)
at sun.nio.ch.FileChannelImpl.transferToTrustedChannel(Unknown Source)
at sun.nio.ch.FileChannelImpl.transferTo(Unknown Source)
at locktest.Main.copy(Main.java:56)
at locktest.Main.main(Main.java:25)
D:\workspace\LockTest>
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
----------
Sat Feb 21 23:51:02 JST 2015
Sat Feb 21 23:51:03 JST 2015
Sat Feb 21 23:51:04 JST 2015
Sat Feb 21 23:51:05 JST 2015
Sat Feb 21 23:51:06 JST 2015
Sat Feb 21 23:51:07 JST 2015
Sat Feb 21 23:51:08 JST 2015
Sat Feb 21 23:51:09 JST 2015
Sat Feb 21 23:51:10 JST 2015
ACTUAL -
D:\workspace\LockTest>certutil -dump test.txt
0000 ...
0110
0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0090 00 00 00 00 00 00 00 00 00 00 00 00 53 61 74 20 ............Sat
00a0 46 65 62 20 32 31 20 32 33 3a 35 31 3a 30 37 20 Feb 21 23:51:07
00b0 4a 53 54 20 32 30 31 35 0a 53 61 74 20 46 65 62 JST 2015.Sat Feb
00c0 20 32 31 20 32 33 3a 35 31 3a 30 38 20 4a 53 54 21 23:51:08 JST
00d0 20 32 30 31 35 0a 53 61 74 20 46 65 62 20 32 31 2015.Sat Feb 21
00e0 20 32 33 3a 35 31 3a 30 39 20 4a 53 54 20 32 30 23:51:09 JST 20
00f0 31 35 0a 53 61 74 20 46 65 62 20 32 31 20 32 33 15.Sat Feb 21 23
0100 3a 35 31 3a 31 30 20 4a 53 54 20 32 30 31 35 0a :51:10 JST 2015.
CertUtil: -dump コマンドは正常に完了しました。
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package locktest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Calendar;
public class Main {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
if (args.length<1) {
System.out.println("java -cp bin locktest.Main (1|2)");
System.exit(-1);
}
String op = args[0].trim().toLowerCase();
System.out.println(op);
if (op.equals("1")) {
lock();
return;
} else if (op.equals("2")) {
copy();
return;
}
System.out.println("java -cp bin locktest.Main (1|2)");
System.exit(-1);
}
private static void lock() throws Exception {
FileOutputStream fos = new FileOutputStream("test.txt");
FileLock lock = fos.getChannel().lock();
fos.write("----------\n".getBytes());
fos.flush();
synchronized (lock) {
while (true) {
try {
lock.wait(1000);
String tst = Calendar.getInstance().getTime().toString() + "\n";
System.out.print(tst);
fos.write(tst.getBytes());
fos.flush();
} catch (InterruptedException e) {
fos.close();
break;
}
}
}
}
private static void copy() throws Exception{
FileOutputStream fos = new FileOutputStream("test.txt");
FileChannel destChannel = fos.getChannel();
FileInputStream bis = new FileInputStream("test.txt");
FileChannel srcChannel = bis.getChannel();
srcChannel.transferTo(0, srcChannel.size(), destChannel);
bis.close();
fos.close();
}
}
---------- END SOURCE ----------
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
"This is a confusing bug report. I think the submitter is missing the main point of file locks, that is file locks are for coordinating between programs to the same file. In the same test case there is only one program and the spec is clear that there is no guarantee that a file lock prevents access to the locked region. It is also clear that file locks cannot be used to coordinate access to a file in the same or multiple threads in the same virtual machine."
NO!
Even a different virtual machine occurs.
logfile was null filled.
REGRESSION. Last worked in version 8u20
ADDITIONAL REGRESSION INFORMATION:
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
[Console-1]
D:\workspace\LockTest>java -cp bin locktest.Main 1
1
Sat Feb 21 23:51:02 JST 2015
Sat Feb 21 23:51:03 JST 2015
Sat Feb 21 23:51:04 JST 2015
Sat Feb 21 23:51:05 JST 2015
Sat Feb 21 23:51:06 JST 2015
Sat Feb 21 23:51:07 JST 2015
Sat Feb 21 23:51:08 JST 2015
Sat Feb 21 23:51:09 JST 2015
Sat Feb 21 23:51:10 JST 2015
[Console-2]
D:\workspace\LockTest>java -cp bin locktest.Main 2
2
Exception in thread "main" java.io.IOException: プロセスはファイルにアクセスできません。別のプロセスがファイルの一部をロックしてい
ます。
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.FileDispatcherImpl.write(Unknown Source)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.write(Unknown Source)
at sun.nio.ch.FileChannelImpl.write(Unknown Source)
at sun.nio.ch.FileChannelImpl.transferToTrustedChannel(Unknown Source)
at sun.nio.ch.FileChannelImpl.transferTo(Unknown Source)
at locktest.Main.copy(Main.java:56)
at locktest.Main.main(Main.java:25)
D:\workspace\LockTest>
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
----------
Sat Feb 21 23:51:02 JST 2015
Sat Feb 21 23:51:03 JST 2015
Sat Feb 21 23:51:04 JST 2015
Sat Feb 21 23:51:05 JST 2015
Sat Feb 21 23:51:06 JST 2015
Sat Feb 21 23:51:07 JST 2015
Sat Feb 21 23:51:08 JST 2015
Sat Feb 21 23:51:09 JST 2015
Sat Feb 21 23:51:10 JST 2015
ACTUAL -
D:\workspace\LockTest>certutil -dump test.txt
0000 ...
0110
0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0090 00 00 00 00 00 00 00 00 00 00 00 00 53 61 74 20 ............Sat
00a0 46 65 62 20 32 31 20 32 33 3a 35 31 3a 30 37 20 Feb 21 23:51:07
00b0 4a 53 54 20 32 30 31 35 0a 53 61 74 20 46 65 62 JST 2015.Sat Feb
00c0 20 32 31 20 32 33 3a 35 31 3a 30 38 20 4a 53 54 21 23:51:08 JST
00d0 20 32 30 31 35 0a 53 61 74 20 46 65 62 20 32 31 2015.Sat Feb 21
00e0 20 32 33 3a 35 31 3a 30 39 20 4a 53 54 20 32 30 23:51:09 JST 20
00f0 31 35 0a 53 61 74 20 46 65 62 20 32 31 20 32 33 15.Sat Feb 21 23
0100 3a 35 31 3a 31 30 20 4a 53 54 20 32 30 31 35 0a :51:10 JST 2015.
CertUtil: -dump コマンドは正常に完了しました。
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package locktest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Calendar;
public class Main {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
if (args.length<1) {
System.out.println("java -cp bin locktest.Main (1|2)");
System.exit(-1);
}
String op = args[0].trim().toLowerCase();
System.out.println(op);
if (op.equals("1")) {
lock();
return;
} else if (op.equals("2")) {
copy();
return;
}
System.out.println("java -cp bin locktest.Main (1|2)");
System.exit(-1);
}
private static void lock() throws Exception {
FileOutputStream fos = new FileOutputStream("test.txt");
FileLock lock = fos.getChannel().lock();
fos.write("----------\n".getBytes());
fos.flush();
synchronized (lock) {
while (true) {
try {
lock.wait(1000);
String tst = Calendar.getInstance().getTime().toString() + "\n";
System.out.print(tst);
fos.write(tst.getBytes());
fos.flush();
} catch (InterruptedException e) {
fos.close();
break;
}
}
}
}
private static void copy() throws Exception{
FileOutputStream fos = new FileOutputStream("test.txt");
FileChannel destChannel = fos.getChannel();
FileInputStream bis = new FileInputStream("test.txt");
FileChannel srcChannel = bis.getChannel();
srcChannel.transferTo(0, srcChannel.size(), destChannel);
bis.close();
fos.close();
}
}
---------- END SOURCE ----------
- relates to
-
JDK-8058701 The channel of length 0 can overwrite with a locked channel.
-
- Closed
-