lukehutch opened a new issue, #9761:
URL: https://github.com/apache/netbeans/issues/9761
(Comment added by Claude)
### Apache NetBeans version
Apache NetBeans 33 latest daily build
### What happened
`JarFileSystem` returns truncated or wrong content for jar entries over
100000 bytes when they are read concurrently, or when two jar paths have the
same hash code.
`inputStream(name)` reads an entry under `MEM_STREAM_SIZE` (100000 bytes)
into memory. A larger entry goes through `getTemporaryInputStream`, which
copies the entry to a file in `$java.io.tmpdir/jarfscache<user.name>/` and
returns a `FileInputStream` on it. The file name is `temporaryName(jarPath,
entryName)`, which is `"f" + jarPath.hashCode() + "e" + entryName.hashCode()`,
so every read of the same entry uses the same file.
The copy is made inside `synchronized (closeSync)`, but:
1. `forceRecreate` is `strongCache != null`, and `strongCache` is non-null
while any `FileObject` of the filesystem is alive, so in practice every read
copies the entry again through `new FileOutputStream(f)`, which truncates the
file first.
2. The streams returned to earlier callers read the file after the lock is
released. Each of them reads whatever part of the file the latest copy has
written so far.
`FileObject.asBytes()` then fails with `IOException: Just 479232 bytes read
from bin/07.bin` (or `Just 0 bytes`); `FileObject.getInputStream()` returns the
short content with no error.
The file is also shared across lock boundaries: by two `JarFileSystem`
instances on the same jar, by two JVMs of the same user, and by two jars whose
paths have the same `String.hashCode()`. In the last case a stream opened on
one jar returns the other jar's entry. For example, `Aa.jar` and `BB.jar` in
the same directory have paths with equal hash codes.
### Language / Project Type / NetBeans Component
platform/openide.filesystems (`org.openide.filesystems.JarFileSystem`), used
as a library
### How to reproduce
Build a jar with 32 entries `bin/00.bin` … `bin/31.bin` of 1 MB of random
bytes each, then:
```java
JarFileSystem fs = new JarFileSystem(jarFile);
// N threads, each doing 500 times:
byte[] b = fs.findResource(String.format("bin/%02d.bin",
random.nextInt(32))).asBytes();
```
Results with RELEASE310 on Linux, JDK 26 (the same for stored and deflated
entries):
| Threads | Failed reads |
|---|---|
| 1 | 0 of 500 |
| 2 | 21–39 of 1000 |
| 16 | about 250 of 8000 |
The shared-file case is deterministic with one thread:
```java
FileObject a = new JarFileSystem(new File(dir,
"Aa.jar")).findResource("big.bin");
FileObject b = new JarFileSystem(new File(dir,
"BB.jar")).findResource("big.bin");
try (InputStream inA = a.getInputStream(); InputStream inB =
b.getInputStream()) {
inA.readAllBytes(); // returns the content of BB.jar's big.bin
}
```
### Did this work correctly in an earlier version?
No / Don't know
### Operating System
Fedora Linux 44, kernel 7.2, x86_64
### JDK
Oracle JDK 26.0.2
### Apache NetBeans packaging
Apache NetBeans platform
### Anything else
Found while benchmarking JVM virtual filesystems for ClassGraph
(https://github.com/classgraph/classgraph/tree/main/classgraph-vfs); NetBeans
had to be left out of the concurrent large-entry benchmarks.
A fix is to give every read its own temporary file (`Files.createTempFile`
in the same directory) and return `Files.newInputStream(file,
StandardOpenOption.DELETE_ON_CLOSE)`. This removes the shared name, the
10-second reuse and `deleteOnExit`. It costs nothing extra in the common case,
since every read already makes a new copy while a `FileObject` is alive.
### Are you willing to submit a pull request?
Yes
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists