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

Detect resource leaks #134

Open
mizosoft opened this issue Jan 30, 2021 · 0 comments
Open

Detect resource leaks #134

mizosoft opened this issue Jan 30, 2021 · 0 comments
Assignees
Labels
P3 type=addition A new feature

Comments

@mizosoft
Copy link

mizosoft commented Jan 30, 2021

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):

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 type=addition A new feature
Projects
None yet
Development

No branches or pull requests

3 participants