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

Local image import does not acquire image lock #13855

Closed
dmitry-lyfar opened this issue Aug 2, 2024 · 0 comments · Fixed by #14100
Closed

Local image import does not acquire image lock #13855

dmitry-lyfar opened this issue Aug 2, 2024 · 0 comments · Fixed by #14100
Assignees
Labels
Bug Confirmed to be a bug
Milestone

Comments

@dmitry-lyfar
Copy link

Required information

  • Distribution: ubuntu
  • Distribution version: 24.04
  • The output of "snap list --all lxd core20 core22 core24 snapd":
Name    Version         Rev    Tracking       Publisher   Notes
core20  20240227        2264   latest/stable  canonical✓  base,disabled
core20  20240416        2318   latest/stable  canonical✓  base
core22  20240111        1122   latest/stable  canonical✓  base,disabled
core22  20240408        1380   latest/stable  canonical✓  base
core24  20240426        405    latest/stable  canonical✓  base,disabled
core24  20240528        423    latest/stable  canonical✓  base
lxd     5.21.1-d46c406  28460  5.21/stable    canonical✓  disabled
lxd     5.21.2-34459c8  29568  5.21/stable    canonical✓  -
snapd   2.62            21465  latest/stable  canonical✓  snapd,disabled
snapd   2.63            21759  latest/stable  canonical✓  snapd
  • The output of "lxc info" or if that fails:
    info.txt

Issue description

If one downloads the image using CopyImage AND uses lxc import with the same image fingerprint concurrently, LXD returns an error due to the latter not locking for the image access, apparently.

Steps to reproduce

  1. lxc export <image> .
  2. Build and run the following API request:
const LxdSock = "/var/snap/lxd/common/lxd/unix.socket"

func main() {
	if srv, err := lxd.ConnectLXDUnix(LxdSock, nil); err == nil {
		defer srv.Disconnect()
		srv := srv.UseProject("default")

		imageServer, err := lxd.ConnectSimpleStreams("https://cloud-images.ubuntu.com/releases", nil)
		if err != nil {
			panic(err)
		}
		defer imageServer.Disconnect()

		var imageInfo *api.Image
		alias, _, err := imageServer.GetImageAlias("22.04/amd64")
		if err != nil {
			panic(err)
		}

		imageInfo, _, err = imageServer.GetImage(alias.Target)
		if err != nil {
			panic(err)
		}

		copyArgs := lxd.ImageCopyArgs{
			AutoUpdate: true,
			Public:     false,
			Type:       "container",
		}

		copyop, err := srv.CopyImage(imageServer, *imageInfo, &copyArgs)
		if err != nil {
			panic(err)
		}

		err = copyop.Wait()
		if err != nil {
			panic(err)
		}
	}
}
  1. While the image is being downloaded, in the second terminal, run lxc image import <local-file-meta> <local-file-squashfs> (you get both files in step 1).

Problem

Step 2 will fail with the following:

Failed remote image download: Failed creating image record: Failed saving main image record: UNIQUE constraint failed: images.project_id, images.fingerprint

Expected

It should not be possible to import an image if it is locked by another operation.

@tomponline tomponline added the Bug Confirmed to be a bug label Aug 2, 2024
@tomponline tomponline added this to the lxd-6.2 milestone Aug 2, 2024
@MggMuggins MggMuggins self-assigned this Sep 12, 2024
MggMuggins added a commit to MggMuggins/lxd that referenced this issue Sep 16, 2024
Fixes canonical#13855

This needs to happen late in the upload process because we need to verify
the uploaded image's fingerprint before acquiring a lock.

Signed-off-by: Wesley Hershberger <[email protected]>
tomponline added a commit that referenced this issue Sep 17, 2024
Fixes #13855 

`imageOperationLock`s are not being taken at the start of a `POST
/1.0/images` because the image fingerprint can't be trusted until the
file has been uploaded (see
[lxd/images/go](https://github.com/canonical/lxd/blob/main/lxd/images.go#L1225)).
This makes it fairly easy to forget to take a lock at all.
tomponline pushed a commit to tomponline/lxd that referenced this issue Oct 1, 2024
Fixes canonical#13855

This needs to happen late in the upload process because we need to verify
the uploaded image's fingerprint before acquiring a lock.

Signed-off-by: Wesley Hershberger <[email protected]>
(cherry picked from commit 8a59f7f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Confirmed to be a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants