Skip to content

Add workaround for JDK-8166253#2074

Merged
Godin merged 2 commits intojacoco:masterfrom
Godin:JDK-8166253
Mar 12, 2026
Merged

Add workaround for JDK-8166253#2074
Godin merged 2 commits intojacoco:masterfrom
Godin:JDK-8166253

Conversation

@Godin
Copy link
Member

@Godin Godin commented Mar 11, 2026

This was overlooked in #2065


Following test

	@Test
	public void test() throws Exception {
		final File file = sourceFolder.newFile();
		Thread thread0 = new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < 100; i++) {
					System.gc();
					System.runFinalization();
				}
			}
		});
		Thread thread1 = newThread(file);
		Thread thread2 = newThread(file);
		thread0.start();
		thread1.start();
		// Give some time for thread1 to start writing data,
		// during which its FileLock might be GCed
		Thread.sleep(100);
		thread2.start();
		thread0.join();
		thread1.join();
		thread2.join();
		new ExecFileLoader().load(file);
	}

	private static Thread newThread(final File file) {
		return new Thread() {
			public void run() {
				try {
					ExecFileLoader loader = new ExecFileLoader();
					loader.getExecutionDataStore()
							.get(42L, "Example", 1024 * 1024 * 8 * 4)
							.getProbes()[0] = true;
					loader.save(file, true);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		};
	}

demonstrates data corruption on JDK versions from 6 to 10 affected by https://bugs.openjdk.org/browse/JDK-8166253

java.io.IOException: Unknown block type 0.
	at org.jacoco.core.data.ExecutionDataReader.readBlock(ExecutionDataReader.java:120)
	at org.jacoco.core.data.ExecutionDataReader.read(ExecutionDataReader.java:93)
	at org.jacoco.core.tools.ExecFileLoader.load(ExecFileLoader.java:60)
	at org.jacoco.core.tools.ExecFileLoader.load(ExecFileLoader.java:74)
	at org.jacoco.core.tools.ExecFileLoaderTest.test(ExecFileLoaderTest.java:132)

whereas expected outcome is

Exception in thread "Thread-2" 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:1062)
	at java.nio.channels.FileChannel.lock(FileChannel.java:1053)
	at org.jacoco.core.tools.ExecFileLoader.save(ExecFileLoader.java:114)
	at org.jacoco.core.tools.ExecFileLoaderTest$2.run(ExecFileLoaderTest.java:143)

@Godin Godin added this to the 0.8.15 milestone Mar 11, 2026
@Godin Godin self-assigned this Mar 11, 2026
@Godin Godin added type: bug 🐛 Something isn't working component: core labels Mar 11, 2026
@Godin Godin requested a review from marchof March 11, 2026 21:35
@Godin Godin marked this pull request as ready for review March 12, 2026 16:42
@Godin Godin merged commit 0bb0906 into jacoco:master Mar 12, 2026
55 checks passed
@Godin Godin deleted the JDK-8166253 branch March 12, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: core type: bug 🐛 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants