Skip to content

Conversation

@artemvmin
Copy link
Contributor

What type of PR is this?
/kind feature

What this PR does / why we need it:
This PR adds a mutex to the FakeExec struct.

The real executor shares no state between osexec.Command() calls. Meanwhile, FakeExec shares all the state stored on the struct.

I am working with a CSI driver that uses a single object for all provisioning calls. This change will allow me to test its behavior when calls are made concurrently.

This change is backwards compatible, since the existing behavior is undefined (and nondeterministic) for concurrent calls.

Release note:

The FakeExec test double is now safe for concurrency.

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Feb 2, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @artemvmin!

It looks like this is your first PR to kubernetes/utils 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/utils has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Feb 2, 2023
@artemvmin
Copy link
Contributor Author

/assign @msau42

@msau42
Copy link
Member

msau42 commented Feb 2, 2023

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 2, 2023
@msau42
Copy link
Member

msau42 commented Feb 2, 2023

/assign @dims


// LookPath is for finding the path of a file
func (fake *FakeExec) LookPath(file string) (string, error) {
fake.mu.Lock()
Copy link
Member

Choose a reason for hiding this comment

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

Can this lead to a lock ordering inversion or even just a recursive lock?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Command() and LookPath() don't explicitly call each other. This means that once either function acquires a lock, it will be able to finish and unlock. Still, the caller could embed one method inside the other, which would cause deadlock.

I went ahead and tightened the lock to only the section violating the invariant, i.e. when CommandCalls is incremented and CommandScript accessed non-atomically.

There's still the implication that the fields in the fake object shouldn't be modified while it's being used in a concurrent context, but there are easier ways to make a brittle test.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I would also at least mention this in the documentation for the function that the risk exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done.

@artemvmin artemvmin force-pushed the concurrent-fake-exec branch from ea2a921 to e46aff0 Compare February 2, 2023 18:00
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 2, 2023
@artemvmin artemvmin force-pushed the concurrent-fake-exec branch from e46aff0 to fad585f Compare February 2, 2023 18:16
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 2, 2023
@apelisse
Copy link
Member

apelisse commented Feb 2, 2023

/approve

@msau42 do you mind re-applying lgtm?

Thanks!

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 2, 2023
@msau42
Copy link
Member

msau42 commented Feb 2, 2023

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 2, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: apelisse, artemvmin, msau42

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 3401372 into kubernetes:master Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants