@@ -210,33 +210,40 @@ public static boolean loadNativeLibrary(Path path) {
210210 Path extractedFilePath = Paths .get (targetDirectory , fileName );
211211
212212 try {
213- // Extract a native library file into the target directory
214- try (InputStream reader = LlamaLoader .class .getResourceAsStream (nativeLibraryFilePath )) {
215- if (reader == null ) {
216- return null ;
217- }
218- Files .copy (reader , extractedFilePath , StandardCopyOption .REPLACE_EXISTING );
219- } finally {
220- // Delete the extracted lib file on JVM exit.
213+ // Fast path: a byte-identical copy already exists — extracted by a previous run or by a
214+ // concurrent JVM sharing this tmpdir. Reuse it rather than rewriting in place: replacing a
215+ // file another process has already loaded fails on Windows (the lib is locked), and an
216+ // in-place rewrite risks a partial file a concurrent loader could observe.
217+ if (Files .exists (extractedFilePath ) && resourceMatchesFile (nativeLibraryFilePath , extractedFilePath )) {
218+ permissionSetter .apply (extractedFilePath .toFile ());
221219 extractedFilePath .toFile ().deleteOnExit ();
220+ return extractedFilePath ;
222221 }
223222
224- // Set executable (x) flag to enable Java to load the native library
225- permissionSetter . apply ( extractedFilePath . toFile ());
226-
227- // Check whether the contents are properly copied from the resource folder
228- try (InputStream nativeIn = LlamaLoader .class .getResourceAsStream (nativeLibraryFilePath );
229- InputStream extractedLibIn = Files . newInputStream ( extractedFilePath ) ) {
230- if ( nativeIn == null ) {
231- System . err . println ( String . format ( "Native library resource missing at %s" , nativeLibraryFilePath ));
232- return null ;
223+ // Otherwise extract into a per-attempt unique temp file, verify it, then atomically move it
224+ // into place so a concurrent loader never observes a half-written library.
225+ Path tempFile = Files . createTempFile ( Paths . get ( targetDirectory ), fileName + "." , ".tmp" );
226+ try {
227+ try (InputStream reader = LlamaLoader .class .getResourceAsStream (nativeLibraryFilePath )) {
228+ if ( reader == null ) {
229+ return null ;
230+ }
231+ Files . copy ( reader , tempFile , StandardCopyOption . REPLACE_EXISTING ) ;
233232 }
234- if (!contentsEquals ( nativeIn , extractedLibIn )) {
233+ if (!resourceMatchesFile ( nativeLibraryFilePath , tempFile )) {
235234 System .err .println (String .format ("Failed to write a native library file at %s" , extractedFilePath ));
236235 return null ;
237236 }
237+ moveIntoPlace (tempFile , extractedFilePath );
238+ } finally {
239+ // No-op once moveIntoPlace consumed it; cleans up if any step above bailed out.
240+ Files .deleteIfExists (tempFile );
238241 }
239242
243+ // Set executable (x) flag to enable Java to load the native library.
244+ permissionSetter .apply (extractedFilePath .toFile ());
245+ extractedFilePath .toFile ().deleteOnExit ();
246+
240247 System .out .println ("Extracted '" + fileName + "' to '" + extractedFilePath + "'" );
241248 return extractedFilePath ;
242249 } catch (IOException e ) {
@@ -245,6 +252,29 @@ public static boolean loadNativeLibrary(Path path) {
245252 }
246253 }
247254
255+ /**
256+ * Atomically replace {@code target} with {@code source}, falling back to a plain move when the
257+ * filesystem does not support atomic moves.
258+ */
259+ private static void moveIntoPlace (Path source , Path target ) throws IOException {
260+ try {
261+ Files .move (source , target , StandardCopyOption .REPLACE_EXISTING , StandardCopyOption .ATOMIC_MOVE );
262+ } catch (IOException atomicUnsupported ) {
263+ Files .move (source , target , StandardCopyOption .REPLACE_EXISTING );
264+ }
265+ }
266+
267+ /** Whether the classpath resource at {@code resourcePath} is byte-identical to {@code file}. */
268+ static boolean resourceMatchesFile (String resourcePath , Path file ) throws IOException {
269+ try (InputStream resource = LlamaLoader .class .getResourceAsStream (resourcePath );
270+ InputStream onDisk = Files .newInputStream (file )) {
271+ if (resource == null ) {
272+ return false ;
273+ }
274+ return contentsEquals (resource , onDisk );
275+ }
276+ }
277+
248278 /**
249279 * Extracts and loads the specified library file to the target folder
250280 *
0 commit comments