Skip to content

Make bitcore-cli lockfile logic more robust - #4249

Open
kajoseph wants to merge 2 commits into
bitpay:masterfrom
kajoseph:fixBitcoreCliFileLock
Open

kajoseph wants to merge 2 commits into
bitpay:masterfrom
kajoseph:fixBitcoreCliFileLock

Conversation

@kajoseph

@kajoseph kajoseph commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Description

The wallet lock file was overwritten if another process was running the wallet with options before the name.

e.g.:

bitcore-cli -H http://localhost:3232 myWallet

Similarly, it would be overwritten if the cli.js file was being run directly in a variety of ways.
e.g.:

node build/src/cli.js myWallet
/User/ibtcore/packages/bitcore-cli/build/src/cli.js myWallet

Changelog

  • Made the logic more robust when evaluating if the lock file was stale or truly held by another process

Testing Notes


Checklist

  • I have read CONTRIBUTING.md and verified that this PR follows the guidelines and requirements outlined in it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Command matching may misidentify unrelated processes and does not verify the wallet name; regression coverage is also missing.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Improves bitcore-cli wallet lock-file detection for varied CLI invocation forms and stale-lock cleanup.

Changes:

  • Broadens process-command matching.
  • Adds stale-lock warnings and explanatory comments.
  • Updates wallet lock ownership validation.
File summaries
File Reviewed changes
packages/bitcore-cli/src/wallet.ts Updates process matching and stale wallet-lock detection.
Review details

Suppressed comments (3)

packages/bitcore-cli/src/wallet.ts:269

  • Removing this.name from the ownership check means a stale .myWallet.LOCK is treated as live when its PID has been reused by a running bitcore-cli otherWallet. The lock filename identifies the wallet whose lock is stale, but it does not prove that the current process owns it; parse the arguments and require the wallet-name token while still allowing options before it.
            if (!command.includes('bitcore-cli ') && !/^\s*(node\s)?.*(src\/)?cli\.js /m.test(command)) {

packages/bitcore-cli/src/wallet.ts:266

  • This comment has an extra article; change “for the this wallet” to “for this wallet.”
            // Furthermore, the lock file name includes the wallet name, so the lock should indeed be for the this wallet.

packages/bitcore-cli/src/wallet.ts:269

  • This check no longer verifies that the running CLI was opened for this.name, and the .*cli.js pattern also accepts unrelated processes. If the PID in a stale lock is later reused by bitcore-cli -H ... otherWallet or by another node /path/to/cli.js process, this wallet's lock is treated as active indefinitely even though that process does not own it. Parse the command line and require both the actual Bitcore CLI executable and this wallet argument (including the options-before-name form), or use a stronger lock ownership token.
            if (!command.includes('bitcore-cli ') && !/^\s*(node\s)?.*(src\/)?cli\.js /m.test(command)) {
  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/bitcore-cli/src/wallet.ts Outdated
Comment thread packages/bitcore-cli/src/wallet.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active legacy lock files may be incorrectly treated as stale; the parsing API type also needs alignment.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (2)

packages/bitcore-cli/src/wallet.ts:269

  • Matching only argv[1] does not prove that the PID still owns this wallet: after PID reuse, another bitcore-cli process using the same cli.js path but a different wallet will pass this check and keep the stale lock in place, preventing the intended wallet from opening. Persist and compare the wallet identity (or robustly parse the live argv) in addition to the script path.
            const isSameProcess = !!runningScriptPath && !!lockedScriptPath && path.resolve(runningScriptPath) === path.resolve(lockedScriptPath);

packages/bitcore-cli/src/wallet.ts:269

  • path.resolve resolves relative paths against the contender's current working directory, not the working directory of the process that wrote the lock. Thus relative invocations from different directories can either compare equal for unrelated cli.js files or fail to identify the same script. Store a canonical/absolute script path (or the lock owner's cwd) and compare against that.
            const isSameProcess = !!runningScriptPath && !!lockedScriptPath && path.resolve(runningScriptPath) === path.resolve(lockedScriptPath);
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Lite

const pid = fs.readFileSync(lockFilename, 'utf-8')?.trim();
const lockFileData = fs.readFileSync(lockFilename, 'utf-8')?.trim();
const [pid, lockedFullCommand] = lockFileData.split('\n');
const lockedScriptPath: string = CWCUtils.tryParse(lockedFullCommand)?.[1];
Comment on lines +181 to +184
export function tryParse(json: string, fallback?: any) {
try {
if (typeof json === 'object') return json; // already parsed
return JSON.parse(json);
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