Skip to content

fix(convert): cap lsof's fd limit and bound the NBD release wait - #172

Merged
imlk0 merged 1 commit into
masterfrom
lsof-is-too-slow
Nov 11, 2026
Merged

imlk0 merged 1 commit into
masterfrom
lsof-is-too-slow

Conversation

@imlk0

@imlk0 imlk0 commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Problem

cryptpilot-convert can look like it hangs at step 11. disk::nbd_disconnect_and_wait polls lsof -t <image> to wait for qemu-nbd to release the write lock. lsof's startup cost grows with RLIMIT_NOFILE, so in a container that inherits a ~1e9 fd limit each call takes minutes before it scans anything. The 30-attempt loop turns a 30 second bound into an hour-long stall, and it prints nothing while waiting.

Fix

  • Add proc::lsof_capped, which lowers the soft fd limit to 1024 and wraps the call in timeout 10 for the lsof process only. The limit shortens lsof's own startup work without narrowing which files it can enumerate.
  • Use the wrapper for the image probe and for the diagnostic lsof in the error trap.
  • Bound the wait by wall-clock seconds instead of attempt count.
  • Treat only lsof exit 0 or 1 as a definitive answer. Timeout or other probe failures are retried, not read as "released".
  • Print progress at the first real holder and every 5 seconds.

Verification

  • bash -n, shellcheck cryptpilot-convert.sh, cargo fmt --check, make clippy, cargo build.
  • Focused harness: at nofile=16777216, a raw lsof takes about 2.7 s while the capped probe takes about 0.1 s; at nofile=1073741816, the capped probe takes about 0.1 s and still finds a holder on fd 3000. No-holder and self-fd-only cases return immediately; a holder is bounded by the wall clock; a failing probe is not read as released; progress prints at 0 s and 5 s.
  • Full convert integration is left to CI.

lsof's startup cost grows with RLIMIT_NOFILE. A container that inherits
a ~1e9 fd limit therefore spends minutes in every lsof call before it
scans anything. The step 11 wait polls lsof up to 30 times, so a wait
meant to last 30 seconds can stall for an hour, and it prints nothing
while doing so.

Add proc::lsof_capped, which lowers the soft fd limit to 1024 and adds a
10 second timeout for the lsof process only. The limit shortens lsof's
own startup work without narrowing which files it can enumerate. Use the
wrapper for the image probe and for the diagnostic lsof in the error
trap.

Bound the wait by wall-clock seconds instead of attempt count, print
progress while a real holder keeps the image busy, and treat only lsof
exit 0 or 1 as a definitive answer. A timeout or other probe failure is
retried rather than read as "released", so the write-lock race the wait
exists to prevent is not reintroduced.

Assisted-by: Claude Code:deepseek-v4.1-flash-1017 [shellcheck]
Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
@ostest-bot

Copy link
Copy Markdown

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

@ostest-bot

Copy link
Copy Markdown

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

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 37.19%. Comparing base (17b5131) to head (6a6b138).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #172      +/-   ##
==========================================
- Coverage   37.23%   37.19%   -0.04%     
==========================================
  Files          93       93              
  Lines        8286     8286              
  Branches     8286     8286              
==========================================
- Hits         3085     3082       -3     
- Misses       4900     4903       +3     
  Partials      301      301              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@imlk0
imlk0 merged commit 6a6b138 into master Nov 11, 2026
57 of 59 checks passed
@imlk0
imlk0 deleted the lsof-is-too-slow branch November 11, 2026 03:05
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