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

fs.read race condition when using Promise.all #3354

Closed
Tracked by #3141
oleiade opened this issue Sep 27, 2023 · 1 comment
Closed
Tracked by #3141

fs.read race condition when using Promise.all #3354

oleiade opened this issue Sep 27, 2023 · 1 comment
Assignees

Comments

@oleiade
Copy link
Member

oleiade commented Sep 27, 2023

Description

The current implementation of the experimental/fs module read (and potentially seek) method seems vulnerable to race conditions under specific circumstances. Below are the issues identified:

  1. Concurrent reads on the same buffer can lead to unpredictable behavior
let file = fs.open(name);
let buffer = new Uint8Array(5);
let p1 = file.read(buffer);
let p2 = file.read(buffer);
await Promise.all([p1, p2]);
  1. A buffer modification prior to the resolution of a promise can lead to uncertainty about the final state of the buffer:
let file = fs.open(name);
let buffer = new Uint8Array(5);
let p1 = file.read(buffer);
buffer[2] = 3;
console.log(buffer); 
await p1;

In this case, it's unclear if the buffer will hold the data read from the file, just the third value 3, or a combination of both.

This behavior seems inconsistent with the promise paradigm, where state changes should only be observable once the promise resolves.

Additional Observations

Preliminary tests with Deno suggest that:

  • Sequential read calls populate data once one of the promises is awaited.
  • Awaiting a promise from an already completed read doesn't affect the buffer.
  • The buffer remains unchanged if a read promise isn't awaited.

Reviewing how Deno and Node handle such situations might be beneficial for insight.

Concerns

While the current behavior might be acceptable for an experimental module, allowing race conditions in stable code could lead to unpredictable behavior and potential bugs. We expect this issue to be addressed as a prerequisite for the experimental/fs module to become stable.

References

#2977
#3141
#3309
#3145
#3148

@oleiade
Copy link
Member Author

oleiade commented Nov 13, 2023

This was addressed by #3309

Here's a related follow-up issue to keep tracking: #3433

@oleiade oleiade closed this as completed Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant