microsoft / microsoft/vcpkg-tool

Errors frequently occur when debugging portfile.cmake

Open
#2,115 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
607
Forks
368
Avg merge
5d 13m
Merged PRs (30d)
12

Description

I noticed that the following errors will occur when debugging some port's portfile:

/Users/jackyu/Documents/vcpkg/installed/vcpkg/vcpkg-running.lock: note: waiting to take filesystem lock... 
/Users/jackyu/Documents/vcpkg/installed/vcpkg/vcpkg-running.lock: note: waiting to take filesystem lock... 
/Users/jackyu/Documents/vcpkg/installed/vcpkg/vcpkg-running.lock: note: waiting to take filesystem lock... 
/Users/jackyu/Documents/vcpkg/installed/vcpkg/vcpkg-running.lock: error: calling try_take_exclusive_file_lock failed with 16 (Resource busy) 
/Users/jackyu/Documents/vcpkg/installed/vcpkg/vcpkg-running.lock: error: failed to take lock, another vcpkg may be running against the same directory

Example command:

rm -f /tmp/vcpkg_ext_portfile_dbg /tmp/vscode-vcpkg-cmakelists-debugger-pipe && "/Users/jackyu/Documents/vcpkg/vcpkg" remove cpp-httplib --recurse && "/Users/jackyu/Documents/vcpkg/vcpkg" install "cpp-httplib[zlib]"  --editable --no-binarycaching --x-cmake-debug /tmp/vcpkg_ext_portfile_dbg --x-cmake-configure-debug /tmp/vscode-vcpkg-cmakelists-debugger-pipe 

Analysis from AI:

Root Cause Analysis

In vcpkg-tool, the vcpkg-running.lock error was caused by:

  1. Extremely Short Retry Timeout in try_take_exclusive_file_lock (only 1.4s): In src/vcpkg/base/files.cpp, the retry backoff was:
    • 1st retry: wait 200 ms
    • 2nd retry: wait 400 ms
    • 3rd retry: wait 800 ms
    • Once total wait exceeded 1.4 seconds (1400 ms), it immediately failed with EBUSY (Resource busy) and exited the process.
  2. Concurrency / Race Conditions in IDE Environments: When chained commands (e.g., vcpkg remove ... && vcpkg install ...) or background IDE tasks (such as tree view dependency queries) run in quick succession, the previous process might take a few hundred milliseconds to fully release the kernel file descriptor lock. With a 1.4s threshold, vcpkg would frequently fail with error: calling try_take_exclusive_file_lock failed with 16 (Resource busy).

Changes Implemented in vcpkg-tool (/Users/jackyu/Documents/vcpkg-tool)

  1. Extended Retry Timeout with Smooth Exponential Backoff (src/vcpkg/base/files.cpp):
    • Increased the total wait duration in try_take_exclusive_file_lock from 1.4s to 30 seconds (30000 ms).
    • Implemented exponential backoff with a 1-second interval cap (200ms $\to$ 400ms $\to$ 800ms $\to$ 1000ms $\dots$). As soon as the preceding process releases the lock, the waiting command immediately acquires it and proceeds smoothly.
  2. Added Environment Variable Support for X_VCPKG_WAIT_FOR_LOCK:
    • Declared EnvironmentVariableXVcpkgWaitForLock = "X_VCPKG_WAIT_FOR_LOCK" in include/vcpkg/base/contractual-constants.h.
    • Handled the environment variable in src/vcpkg/vcpkgcmdarguments.cpp so that X_VCPKG_WAIT_FOR_LOCK=1 globally enables blocking lock acquisition (wait_for_lock = true).

Fix from AI:
fix_lock_exist.diff.zip

I tried the fix and it works fine. And I checked the changes, looks good to me.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/vcpkg/base/files.cpp and reproduce the lock failure using the example vcpkg remove/install command with debugging enabled. Review include/vcpkg/base/contractual-constants.h and src/vcpkg/vcpkgcmdarguments.cpp for the proposed X_VCPKG_WAIT_FOR_LOCK handling; done means debugging no longer fails during the reported lock race and the relevant behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.