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

[Android] read() and readFile() lead to "A resource failed to call close" #10

Closed
raphaelheinz opened this issue Oct 10, 2023 · 5 comments
Labels
P2 Important issue. Ready Ready for release.

Comments

@raphaelheinz
Copy link

Hi @birdofpreyru,

thanks for taking over the maintenance of this library. We appreciate your time and effort.

Currently, I am facing a problem that is also reproducible with the original library. I would like to read a large file (>20MB) in Android and for performance reasons I read it in chunks within a for-loop.

import * as RNFS from '@dr.pogodin/react-native-fs';

const filepath="/storage/emulated/0/Android/data/com.testpackage.app/files/Download/largeFile.bin";

const size = (await RNFS.stat(filepath)).size;
const chunkSize = 32*1024;

for(let i = 0; i < size; i += chunkSize) {
  const data = await RNFS.read(filepath, chunkSize, i, 'base64');
  // do sth with data
}

Unfortunately a lot of Android system warnings are logged in the loop:
A resource failed to call close.

Are you aware of this problems? I do not feel comfortable releasing my app which contains tons of such warnings. Can I close the file manually to prevent this warning? Or do you recommend other techniques for my use case?

Thanks very much!

@birdofpreyru birdofpreyru added the P2 Important issue. label Oct 10, 2023
@birdofpreyru
Copy link
Owner

Hey @raphaelheinz , thanks for reporting.

a problem that is also reproducible with the original library

Yeah... there are lots of minor inconsistencies and bugs in the original library, also inherited into my fork.

Are you aware of this problems?

No, I haven't noticed it, but had no chance to carefully test everything either.

Can I close the file manually to prevent this warning? Or do you recommend other techniques for my use case?

I guess, it just requires a fix in this function implementation for Android (and perhaps other functions may have the same issue):

@ReactMethod
public void read(
String filepath,
double length,
double position,
Promise promise
) {
try {
InputStream inputStream = getInputStream(filepath);
byte[] buffer = new byte[(int)length];
inputStream.skip((int)position);
int bytesRead = inputStream.read(buffer, 0, (int)length);
String base64Content = Base64.encodeToString(buffer, 0, bytesRead, Base64.NO_WRAP);
promise.resolve(base64Content);
} catch (Exception ex) {
ex.printStackTrace();
reject(promise, filepath, ex);
}
}

☝️ Looking at the code, and your warning, I guess it should close the file stream at function exit, but it never does.

@raphaelheinz
Copy link
Author

raphaelheinz commented Oct 10, 2023

Thanks for your input!

I can confirm that this was indeed the issue. After closing the file in the Android native module, the warning has disappeared.

If you like, I can submit a pull request after work :)

@birdofpreyru
Copy link
Owner

If you like, I can submit a pull request after work :)

It will be awesome if you do :)

@birdofpreyru birdofpreyru added In Progress Work in progress. Ready Ready for release. and removed In Progress Work in progress. labels Oct 10, 2023
@ISnowFoxI
Copy link

Hello, @birdofpreyru it also doesn't close on ios as well. When I read chunks in a loop, the memory consumption just keeps on getting higher and higher. Sadly, I couldn't figure out how to close the file on my own.

@birdofpreyru
Copy link
Owner

birdofpreyru commented Oct 11, 2023

@ISnowFoxI this does not sound surprising to me. I created a new issue to track it (#12), though no idea when I'll have capacity to look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Important issue. Ready Ready for release.
Projects
None yet
Development

No branches or pull requests

3 participants