Created
February 4, 2025 14:45
-
-
Save swankjesse/d995ba7c79b27193fa7a908c7334ba46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun closeAJarFileWhileSomebodyIsReadingIt() { | |
val someJarResource = this::class.java.classLoader.getResource("LICENSE-junit.txt")!! | |
val connectionA = someJarResource.openConnection() | |
val sourceA = connectionA.getInputStream().source().buffer() | |
val connectionB = someJarResource.openConnection() | |
val sourceB = connectionB.getInputStream().source().buffer() | |
connectionA.useCaches = false | |
assertThat(sourceA.readUtf8()).contains("JUnit") | |
sourceA.close() | |
assertThat(sourceB.readUtf8()).contains("JUnit") | |
sourceB.close() | |
} |
Setting useCaches
to false causes sourceA
to close a .jar
file that it doesn’t own.
awesome! explains the race condition
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It doesn’t crash if you remove this line:
And it doesn’t crash if you don’t open
sourceB
until after closingsourceA
.