google / google/jimfs

Detect resource leaks

Open
#134 0 comments 0 reactions 1 assignee Claimed by @cgdecker View on GitHub
P3 type=addition
Dominant language
Java
Stars
2.6k
Forks
296
Avg merge
9m
Merged PRs (30d)
7

Description

Hi! First of all, thanks for this library 👍🏾.

So... I use Jimfs in my tests and I reckon that's what many people do. I think it'd be fruitful if Jimfs could detect open resources that aren't released properly (by resources I mean anything created by the FS that's implementing `Closeable`, including channels, directory streams, etc...). This makes it really easy to ensure tested components properly close file resources. I realize it'll be silly if this suddenly became the default behavior, so I'd expect this to be explicitly enabled in the FS's `Configuration`. It'd be preferable if this could be checked in `FileSystem::close`, so the following pattern can be utilized (depending on the testing framework):

```java
private FileSystem fs;

@BeforeEach
void setUp() {
var config = Configuration.builder(PathType.unix()).setDetectLeaksOnClosure(true).build();
fs = Jimfs.newFileSystem(config);
}

@AfterEach
void tearDown() throws IOException {
if (fs != null) {
fs.close(); // Throws in case of resource leaks
}
}
...
```
I notice resource registration is handled by the `FileSystemState` class. Currently, it seems to only close registered resources. I think most behavior could be implemented there, but you guys know better. The class seems to receive possibly custom `Closeable` instances, so it may need to check the classes of registered resources before complaining. Perhaps, specific resource types could explicitly opt-in for the checks by the configuration (e.g. only check FileChannels).

Thanks in advance!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.