[ 
https://issues.apache.org/jira/browse/COMPRESS-708?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

赵钰玮 updated COMPRESS-708:
-------------------------
    Description: 
{code:java}
@Test
void testZstdInputStreamErrorCloseWhenGc() throws Exception  {
    final File archive = getFile("COMPRESS-692/compress-692.zip");
    for (int i =0; i< 50;i++) {
        try (FileInputStream fileInputStream = new FileInputStream(archive);
             ZipArchiveInputStream zipArchiveInputStream = new 
ZipArchiveInputStream(fileInputStream)){
            ArchiveEntry entry;
            while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                System.gc();
                IOUtils.toByteArray(zipArchiveInputStream);
            }
        } catch (IOException e) {
            fail("testZstdInputStreamErrorCloseWhenGc error, test error at 
batch " + (i+1), e);
        }
    }
} {code}
Here is the test method. Without the line {{{}System.gc();{}}}, it may take 
multiple execution rounds to trigger the bug, but with this line added, the bug 
is almost immediately triggered. Below is the error stack log.

!image-2025-09-03-22-46-38-243.png!

I found that this is caused by the following: The bug occurred because the 
ZstdInputStream used in ZstdCompressorInputStream would close the underlying 
InputStream during GC, which in turn caused errors like "stream closed" for 
other wrapper streams (such as ZipArchiveInputStream) that depend on this 
underlying stream.

  was:
{code:java}
@Test
void testZstdInputStreamErrorCloseWhenGc() throws Exception  {
    final File archive = getFile("COMPRESS-692/compress-692.zip");
    for (int i =0; i< 50;i++) {
        try (FileInputStream fileInputStream = new FileInputStream(archive);
             ZipArchiveInputStream zipArchiveInputStream = new 
ZipArchiveInputStream(fileInputStream)){
            ArchiveEntry entry;
            while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }
                System.gc();
                IOUtils.toByteArray(zipArchiveInputStream);
            }
        } catch (IOException e) {
            fail("testZstdInputStreamErrorCloseWhenGc error, test error at 
batch " + (i+1), e);
        }
    }
} {code}
Here is the test method. Without the line {{{}System.gc();{}}}, it may take 
multiple execution rounds to trigger the bug, but with this line added, the bug 
is almost immediately triggered. Below is the error stack log.

!image-2025-09-03-22-46-38-243.png!

I found that this is caused by the following: When Java triggers GC, 
ZstdInputStream is closed, and it incorrectly closes the InputStream held by 
ZipArchiveInputStream. Since all streams are wrappers around the InputStream 
held by ZstdInputStream, closing this InputStream results in "Caused by: 
java.io.IOException: Stream closed".


> ZstdCompressorInputStream has a bug where the InputStream held by 
> ZipArchiveInputStream gets closed during garbage collection (GC)
> ----------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: COMPRESS-708
>                 URL: https://issues.apache.org/jira/browse/COMPRESS-708
>             Project: Commons Compress
>          Issue Type: Bug
>            Reporter: 赵钰玮
>            Priority: Major
>         Attachments: image-2025-09-03-22-46-38-243.png
>
>
> {code:java}
> @Test
> void testZstdInputStreamErrorCloseWhenGc() throws Exception  {
>     final File archive = getFile("COMPRESS-692/compress-692.zip");
>     for (int i =0; i< 50;i++) {
>         try (FileInputStream fileInputStream = new FileInputStream(archive);
>              ZipArchiveInputStream zipArchiveInputStream = new 
> ZipArchiveInputStream(fileInputStream)){
>             ArchiveEntry entry;
>             while ((entry = zipArchiveInputStream.getNextEntry()) != null) {
>                 if (entry.isDirectory()) {
>                     continue;
>                 }
>                 System.gc();
>                 IOUtils.toByteArray(zipArchiveInputStream);
>             }
>         } catch (IOException e) {
>             fail("testZstdInputStreamErrorCloseWhenGc error, test error at 
> batch " + (i+1), e);
>         }
>     }
> } {code}
> Here is the test method. Without the line {{{}System.gc();{}}}, it may take 
> multiple execution rounds to trigger the bug, but with this line added, the 
> bug is almost immediately triggered. Below is the error stack log.
> !image-2025-09-03-22-46-38-243.png!
> I found that this is caused by the following: The bug occurred because the 
> ZstdInputStream used in ZstdCompressorInputStream would close the underlying 
> InputStream during GC, which in turn caused errors like "stream closed" for 
> other wrapper streams (such as ZipArchiveInputStream) that depend on this 
> underlying stream.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to