Skip to content

fix(convert): wait for nbd device release before qemu-img convert - #144

Merged
imlk0 merged 1 commit into
masterfrom
fix-nbd-disconnect-race
Sep 29, 2026
Merged

imlk0 merged 1 commit into
masterfrom
fix-nbd-disconnect-race

Conversation

@imlk0

@imlk0 imlk0 commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

test-convert intermittently fails at step 11 with:

qemu-img: Could not open '.../input.qcow2.work': Failed to get shared "write" lock
Is another process using the image [ .../input.qcow2.work ]?
make: *** [Makefile:215: run-convert-test-case] Error 1

Example failing run: https://github.com/openanolis/cryptpilot/actions/runs/36511833625/job/109231935489

Root cause

At step 11 (cryptpilot-convert.sh), the nbd device was detached with a fixed guard:

qemu-nbd --disconnect "${device}"
sleep 2 # wait for the qemu-nbd daemon to release the file lock
...
qemu-img convert -p -O qcow2 "${work_file}" "${output_file}"

qemu-nbd --disconnect returns before the nbd daemon finishes exiting and releases the OFD write lock it holds on the backing image. The fixed sleep 2 covers it most of the time but is a race: under CI load the lock can still be held when qemu-img convert opens the image, so it bails out.

A first attempt polled blockdev --getsize64 (== 0 means free device). That is not a reliable signal — confirmed on this PR's own CI, the device reports size 0 within the same millisecond as --disconnect while the daemon still holds the file lock, so the poll exits immediately and the race is back.

Fix

Replace the fixed sleep with disk::nbd_disconnect_and_wait, which disconnects and then polls lsof on the image file (lsof -t "${image}"): once no process holds the file open, the daemon has exited and the OFD write lock is released. qemu-img convert only runs after the lock is truly gone. The loop is bounded by interval + max attempts (never blocks forever); on timeout it warns and proceeds rather than hard-failing.

lsof is already installed by the test harness (tool_packages=(qemu-img cryptsetup veritysetup lvm2 parted e2fsprogs lsof ...)).

Assisted-by

Assisted-by: Claude:claude-opus-4-8

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,您的请求已接收,请耐心等待结果。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,未检测到有镜像需要构建,如需重新检测请评论 /start 。

qemu-nbd --disconnect returns before the nbd daemon finishes exiting and
releases the OFD write lock it holds on the backing image. The previous
fixed "sleep 2" guard at step 11 was a race: under load (e.g. CI
containers) qemu-img convert intermittently failed with

  qemu-img: Could not open '...input.qcow2.work': Failed to get shared "write" lock

Polling blockdev --getsize64 is not a reliable fix either — the device
reports size 0 while the daemon still holds the file lock, so the poll
exits immediately and the race is back (reproduced on PR CI).

Replace the fixed sleep with disk::nbd_disconnect_and_wait, which polls
lsof on the image file: once no process holds it open the daemon has
exited and the lock is released. Bounded by interval + max attempts so it
never blocks forever. qemu-img convert then runs once the lock is truly
gone.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
@imlk0
imlk0 force-pushed the fix-nbd-disconnect-race branch from a3aa5ad to 29c39b2 Compare September 29, 2026 04:50
@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,您的请求已接收,请耐心等待结果。

@ostest-bot

Copy link
Copy Markdown

@imlk0 ,您好,未检测到有镜像需要构建,如需重新检测请评论 /start 。

@imlk0
imlk0 merged commit 29c39b2 into master Sep 29, 2026
105 of 108 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants