Skip to content

Potential file leak in FileSystem.read() method #1364

@pablobaxter

Description

@pablobaxter

Continued investigation in file descriptor leaks led to this stack trace:

#1 /Users/pablobaxter/.gradle/caches/modules-2/files-2.1/com.squareup.wire/wire-schema-jvm/4.7.0/4f9c99c8217fc7d53fd11fa3e885559c62280227/wire-schema-jvm-4.7.0.jar by thread:main on Tue Oct 10 12:47:57 PDT 2023
        at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:173)
        at java.base/java.util.jar.JarFile.<init>(JarFile.java:347)
        at java.base/sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:103)
        at java.base/sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:72)
        at java.base/sun.net.www.protocol.jar.JarFileFactory.getOrCreate(JarFileFactory.java:106)
        at java.base/sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:132)
        at java.base/sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:175)
        at java.base/java.net.URL.openStream(URL.java:1161)
        at java.base/java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1735)
        at okio.internal.ResourceFileSystem.source(ResourceFileSystem.kt:129)
        at com.squareup.wire.schema.CoreLoader.load(CoreLoader.kt:54)
        at com.squareup.wire.schema.internal.CommonSchemaLoader.load(CommonSchemaLoader.kt:148)
        at com.squareup.wire.schema.Linker.getFileLinker$wire_schema(Linker.kt:74)
        at com.squareup.wire.schema.Linker.link(Linker.kt:95)
        at com.squareup.wire.schema.internal.CommonSchemaLoader.loadSchema(CommonSchemaLoader.kt:103)
        at com.squareup.wire.schema.SchemaLoader.loadSchema(SchemaLoader.kt:71)
        at com.squareup.wire.schema.WireRun.execute(WireRun.kt:219)
        at com.squareup.wire.schema.WireRun.execute(WireRun.kt:211)

From what I've been able to gather, this is the culprit code:

@Throws(IOException::class)
@JvmName("-read")
actual inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T {
  return source(file).buffer().use {
    it.readerAction()
  }
}

From what I can tell, the use {} lambda is not closing the source property of the RealBufferedSource object, but it's unclear why.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions