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

Address potential races in fs.read and fs.seek operations #3420

Conversation

oleiade
Copy link
Member

@oleiade oleiade commented Oct 25, 2023

What?

This Pull Request aims to address #3354, and avoid data races involved in using the currently proposed implementation of fs.read and fs.seek.

Why?

Namely, it modifies the behavior of the proposed fs.read and fs.seek methods to avoid data races due to:

  1. accessing the fs.read buffer argument concurrently
  2. updating the file offset concurrently when calling seek

In a nutshell, we addressed 1 by ensuring that the provided buffer was only modified from the JS runtime's main thread, by performing it as a side-effect of the promise resolution's. We addressed 2 by making sure the internal file structure's offset is an atomic int that cannot, as a result, be modified by two concurrent operations at the same time.

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make tests) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Related PR(s)/Issue(s)

ref #3309
ref #2974
closes #3354

@oleiade oleiade added the bug label Oct 25, 2023
@oleiade oleiade added this to the v0.48.0 milestone Oct 25, 2023
@oleiade oleiade requested review from mstoykov and codebien October 25, 2023 12:28
@oleiade oleiade self-assigned this Oct 25, 2023
@codecov-commenter
Copy link

codecov-commenter commented Oct 25, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (experimental/fs-readseek@0a4f80b). Click here to learn what that means.

❗ Current head 4199fd1 differs from pull request most recent head b79656b. Consider uploading reports for the commit b79656b to get more accurate results

Additional details and impacted files
@@                     Coverage Diff                     @@
##             experimental/fs-readseek    #3420   +/-   ##
===========================================================
  Coverage                            ?   73.21%           
===========================================================
  Files                               ?      263           
  Lines                               ?    20101           
  Branches                            ?        0           
===========================================================
  Hits                                ?    14716           
  Misses                              ?     4448           
  Partials                            ?      937           
Flag Coverage Δ
ubuntu 73.15% <0.00%> (?)
windows 73.05% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, added a comment regarding f.size usage polish, and to make explicit the assumption we are making regarding the concurrent use of Read and Seek.

js/modules/k6/experimental/fs/file.go Outdated Show resolved Hide resolved
js/modules/k6/experimental/fs/file.go Outdated Show resolved Hide resolved
js/modules/k6/experimental/fs/file.go Outdated Show resolved Hide resolved
js/modules/k6/experimental/fs/file.go Show resolved Hide resolved
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, mostly waiting for the TODO comment as expressed on #3420 (comment)

js/modules/k6/experimental/fs/file.go Outdated Show resolved Hide resolved
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left some none blocking comments

js/modules/k6/experimental/fs/module.go Outdated Show resolved Hide resolved
js/modules/k6/experimental/fs/module.go Show resolved Hide resolved
js/modules/k6/experimental/fs/file.go Show resolved Hide resolved
@oleiade
Copy link
Member Author

oleiade commented Oct 31, 2023

Created issue #3433 to track the issue related to operations not being treated sequentially (cc @codebien ) 👍🏻

@oleiade oleiade requested a review from codebien October 31, 2023 14:43
@oleiade oleiade merged commit efdd43f into experimental/fs-readseek Nov 1, 2023
@oleiade oleiade deleted the experimental/fix/3354-read-seek-race-conditions branch November 1, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants