Skip to content

driver/overlay: fix concurrent map write on stagingDirsLocks #2300

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

Merged
merged 2 commits into from
Mar 27, 2025

Conversation

Luap99
Copy link
Member

@Luap99 Luap99 commented Mar 27, 2025

Seen in podman CI, a map by default in not concurrent safe. As the
driver can be used in parallel by callers we must guard against this
here.

Fixes #2297


drivers/overlay: use clear() for stagingDirsLocks

That should be more memory efficient as we do not have to allocate a new map.

Luap99 added 2 commits March 27, 2025 12:12
Seen in podman CI, a map by default in not concurrent safe. As the
driver can be used in parallel by callers we must guard against this
here.

Fixes containers#2297

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
That should be more memory efficient as we do not have to allocate a new
map.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Copy link
Contributor

@cgwalters cgwalters left a comment

Choose a reason for hiding this comment

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

Looks sane to me.

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

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

/lgtm

Copy link
Contributor

openshift-ci bot commented Mar 27, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cgwalters, giuseppe, Luap99

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

@openshift-merge-bot openshift-merge-bot bot merged commit a6623f0 into containers:main Mar 27, 2025
20 checks passed
@Luap99 Luap99 deleted the stagingDirsLocks-race branch March 27, 2025 17:23
Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

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

LGTM, for the record.

@@ -130,6 +130,9 @@ type Driver struct {
usingMetacopy bool
usingComposefs bool

stagingDirsLocksMutex *sync.Mutex
Copy link
Collaborator

Choose a reason for hiding this comment

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

For future reference, I think it’s a bit more idiomatic not to use pointers for Mutex fields — that way linters can warn about unexpectedly making copies of the containing struct.

Copy link
Member Author

Choose a reason for hiding this comment

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

When Driver is copied it also copy the same stagingDirsLocks map, as such it should copy the same Mutex which is why I used the pointer as it will keep pointing to the same Mutex.

Maybe that is just an invalid pattern which we cannot support anyway. Looking at this again we only return interfaces and not the type so it should be impossible for callers to use Driver directly to copy so I think you are right we can remove the extra pointer redirection here and rely on the linters to catch misuse

Copy link
Collaborator

@mtrmac mtrmac Mar 27, 2025

Choose a reason for hiding this comment

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

As you say, in this case, things are perfectly fine: the protected stagingDirsLocks, as a map, is implicitly a reference type — so if something did make a copy, both copies would point to the same lock and the same map; the map would be protected and accesses should not crash (whether such uses would mean anything useful is a different matter).

We 100% don’t support copying the Driver struct. (As usual, Go doesn’t let us explicitly express that.) So this is all pedantic anyway.

  • Actually, the supposedly-recommended/idiomatic way to express “don’t copy this type” is to add a mutex (-like method). Compare
    // noCopy may be added to structs which must not be copied

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, the supposedly-recommended/idiomatic way to express “don’t copy this type” is to add a mutex (-like method). Compare

LOL, I keep the rewrite in rust comments to myself.

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.

concurrent map writes in overlay driver
4 participants