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

Race condition sometimes resulting in Error: Failed to create parent directory #84

Closed
steffenkleinle opened this issue Nov 27, 2021 · 0 comments · Fixed by #85
Closed

Comments

@steffenkleinle
Copy link

steffenkleinle commented Nov 27, 2021

environment
react-native-blob-util version: 0.13.6
react-native: 0.66.1

code (simplified)

yield* all({
    result1: yield* call(() => RNBlobUtil.fs.writeFile(path1, data1, 'utf8')),
    result2: yield* call(() => RNBlobUtil.fs.writeFile(path2, data2, 'utf8'))
})

error

Error: Failed to create parent directory of '/data/user/0/tuerantuer.app.integreat/files/content/v1/lkvechta/bg/pois.json'

possible explanation
While I was not able to reproduce the error myself, we got some reports via sentry about this issue. It seems like this happens sometimes because of a race condition (e.g. with redux-saga) if attempting to create two files in the same not yet existing directory. Possible execution order:

  • First call to writeFile gets result that parent directory does not exist
  • Second call to writeFile gets result that parent directory does not exist
  • First call to writeFile creates parent directory
  • Second call to writeFile tries to create parent directory but gets result false from Java.io.File.mkdirs() since parent directory does already exist
  • Second call to writeFile throws Failed to create parent directory exception

proposed solution
Only throw an exception if the parent directory does still not exist after attempting to create parent directory, i.e. replace

if (!dir.mkdirs()) {
    promise.reject("ENOTDIR", "Failed to create parent directory of '" + path + "'");
    return;
}

with

if (!dir.mkdirs() && !dir.exists()) {
    promise.reject("ENOTDIR", "Failed to create parent directory of '" + path + "'");
    return;
}

Corresponding lines: https://github.com/joltup/rn-fetch-blob/blob/dcbde6f7e12b666b9fe1c8c4a8e2cb04e0048326/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java#L61

Created a PR for this: #85

Thanks for maintaining this project btw 🙏

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

Successfully merging a pull request may close this issue.

1 participant