solver: fix possible race for provenance ResolveImageConfig #4157
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ResolveImageConfig can be called concurrently - for example, by dockerfile2llb during conversion, we loop through each stage and resolve the base image for that stage.
In the case that two calls to ResolveImageConfig finish at roughly the same time, we can hit an edge case where we attempt to modify the bridge's image records at the same time.
To fix this, we just need to use the bridge's mutex to prevent concurrent access here.
This should fix the following stack trace found in CI (https://github.com/moby/buildkit/actions/runs/5889475633/job/15972815280?pr=4041):
No other explanation for this failure makes sense -
b
cannot benil
at this point, since a call tob.llbBridge.ResolveImageConfig
has just succeeded (also because that would be very strange).Note: I can't manage to reproduce this with the race checker, so I'm not actually 100% sure that this is the issue that caused the CI failure, but the code here is definitely not thread-safe, so at the very least, this improves that.