Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent ClasspathResourceDirectoryReader from accessing closed NativeImageResourceFileSystem #30346

Merged
merged 3 commits into from
Feb 29, 2024

Conversation

linghengqian
Copy link
Member

Fixes #30345.

Changes proposed in this pull request:


Before committing this PR, I'm sure that I have checked the following options:

  • My code follows the code of conduct of this project.
  • I have self-reviewed the commit code.
  • I have (or in comment I request) added corresponding labels for the pull request.
  • I have passed maven check locally : ./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e.
  • I have made corresponding changes to the documentation.
  • I have added corresponding unit tests for my changes.

@iamhucong
Copy link
Contributor

@linghengqian Hi, I believe Stream can lazily consume resources. Are there any potential issues that I haven't considered?

@linghengqian
Copy link
Member Author

linghengqian commented Feb 29, 2024

@linghengqian Hi, I believe Stream can lazily consume resources. Are there any potential issues that I haven't considered?

  • The Steam obtained by the ClasspathResourceDirectoryReader.read() function is not read immediately, which leads to a problem with the ClasspathResourceDirectoryReader.readDirectoryInFileSystem() function.
  • com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystem is opened and then closed, which causes the Stream instance returned by the ClasspathResourceDirectoryReader.read() function of the JDBCRepositorySQLLoader.load() operation to try to access a non-existent File System reads the contents of the Stream.
  • Stream is still read lazily on Jar/jdk.nio.zipfs.ZipFileSystem. That won't change with this PR.

@iamhucong
Copy link
Contributor

@linghengqian Hi, I believe Stream can lazily consume resources. Are there any potential issues that I haven't considered?

  • The Steam obtained by the ClasspathResourceDirectoryReader.read() function is not read immediately, which leads to a problem with the ClasspathResourceDirectoryReader.readDirectoryInFileSystem() function.
  • com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystem is opened and then closed, which causes the Stream instance returned by the ClasspathResourceDirectoryReader.read() function of the JDBCRepositorySQLLoader.load() operation to try to access a non-existent File System reads the contents of the Stream.
  • Stream is still read lazily on Jar/jdk.nio.zipfs.ZipFileSystem. That won't change with this PR.
FileSystem resourceFileSystem = FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap())
return loadFromDirectory(directoryUrl).onClose(resourceFileSystem::close());

Could this code resolve those problems?

@linghengqian
Copy link
Member Author

FileSystem resourceFileSystem = FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap())
return loadFromDirectory(directoryUrl).onClose(resourceFileSystem::close());

Could this code resolve those problems?

  try (Stream<String> resourceNameStream = ClasspathResourceDirectoryReader.read(JDBCRepositorySQLLoader.class.getClassLoader(), ROOT_DIRECTORY)) {
             Iterable<String> resourceNameIterable = resourceNameStream::iterator;
             for (String each : resourceNameIterable) {

@iamhucong
Copy link
Contributor

FileSystem resourceFileSystem = FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap())
return loadFromDirectory(directoryUrl).onClose(resourceFileSystem::close());

Could this code resolve those problems?

  try (Stream<String> resourceNameStream = ClasspathResourceDirectoryReader.read(JDBCRepositorySQLLoader.class.getClassLoader(), ROOT_DIRECTORY)) {
             Iterable<String> resourceNameIterable = resourceNameStream::iterator;
             for (String each : resourceNameIterable) {

The resourceFileSystem.close() method will be called only when the resourceNameStream is closed, therefore the resourceFileSystem will be closed after the resourceNameStream has been used.

@iamhucong
Copy link
Contributor

https://github.com/oracle/graal/blob/486451e0af9772d24e604668fb3c6058233bdce3/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/resources/NativeImageResourceFileSystem.java#L178-L204

    public void close() throws IOException {
        beginWrite();
        try {
            if (!isOpen) {
                return;
            }
            isOpen = false;
        } finally {
            endWrite();
        }


        if (!inputStreams.isEmpty()) {    // Unlock and close all remaining input streams.
            Set<InputStream> copy = new HashSet<>(inputStreams);
            for (InputStream is : copy) {
                is.close();
            }
        }


        if (!outputStreams.isEmpty()) {    // Unlock and close all remaining output streams.
            Set<OutputStream> copy = new HashSet<>(outputStreams);
            for (OutputStream os : copy) {
                os.close();
            }
        }


        provider.removeFileSystem();
    }

Alternatively, is it possible to consider not closing the resource file system? It seems that the close() method only closes some input/output streams, but I'm not very familiar with GraalVM.

@linghengqian
Copy link
Member Author

The resourceFileSystem.close() method will be called only when the resourceNameStream is closed, therefore the resourceFileSystem will be closed after the resourceNameStream has been used.

  • I chose this approach and made a new commit.

Alternatively, is it possible to consider not closing the resource file system? It seems that the close() method only closes some input/output streams, but I'm not very familiar with GraalVM.

if ("resource".equals(directoryUrl.getProtocol())) {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
try (FileSystem ignored = FileSystems.getFileSystem(directoryUrl.toURI())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should a file system that has already been created not be closed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terrymanu terrymanu added this to the 5.5.0 milestone Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment