Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(mcp): clear stale install debris from ~/.dbx/bin

HARRISON64f00877305f
diff --git a/.github/scripts/install-mcp-powershell.test.ps1 b/.github/scripts/install-mcp-powershell.test.ps1
index 5c0f9c65c3..a90d5438d4 100644
--- a/.github/scripts/install-mcp-powershell.test.ps1
+++ b/.github/scripts/install-mcp-powershell.test.ps1
@@ -113,7 +113,7 @@ try {
$script:count++
Write-Output 'PASS older mirror cannot downgrade installation'
$installDir = Split-Path $binary
- $preservedNames = @('dbx-mcp.exe', '.dbx-mcp-version', 'other.tmp', 'dbx-mcp.exe~RFxyz.TMP', 'dbx-mcp.exe~RF.TMP')
+ $preservedNames = @('dbx-mcp.exe', '.dbx-mcp-version', 'other.tmp', 'dbx-mcp.exe~RFxyz.TMP', 'dbx-mcp.exe~RF.TMP', 'dbx-mcp.exe.RFxyz.TMP', 'dbx-mcp.exe.RF.TMP')
foreach ($name in $preservedNames | Select-Object -Skip 2) {
[IO.File]::WriteAllText((Join-Path $installDir $name), 'preserve')
}
@@ -123,8 +123,12 @@ try {
[IO.File]::WriteAllText($nestedFile, 'nested')
$stale = Join-Path $installDir 'DBX-MCP.EXE~rfABC123.tmp'
[IO.File]::WriteAllText($stale, 'old executable')
+ # ReplaceFile also writes the dot-separated form (#13009); it must be swept too.
+ $staleDot = Join-Path $installDir 'dbx-mcp.exe.RF1A2B3C4D.TMP'
+ [IO.File]::WriteAllText($staleDot, 'old executable')
Remove-McpReplacementTempFiles $installDir
Assert-True (!(Test-Path -LiteralPath $stale)) 'Case-insensitive replacement cleanup failed'
+ Assert-True (!(Test-Path -LiteralPath $staleDot)) 'Dot-form replacement cleanup failed'
foreach ($name in $preservedNames) {
Assert-True (Test-Path -LiteralPath (Join-Path $installDir $name) -PathType Leaf) "Cleanup removed $name"
}
diff --git a/docs/public/install-mcp.ps1 b/docs/public/install-mcp.ps1
index 29cf6176e3..3e0d3e6d02 100644
--- a/docs/public/install-mcp.ps1
+++ b/docs/public/install-mcp.ps1
@@ -47,7 +47,7 @@ function Remove-McpReplacementTempFiles([string]$Directory) {
if (!$item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { return }
Get-ChildItem -LiteralPath $Directory -Force -ErrorAction Stop | Where-Object {
!$_.PSIsContainer -and !($_.Attributes -band [IO.FileAttributes]::ReparsePoint) -and
- $_.Name -imatch '\Adbx-mcp\.exe~RF[0-9a-f]+\.TMP\z'
+ $_.Name -imatch '\Adbx-mcp\.exe[~.]RF[0-9a-f]+\.TMP\z'
} | ForEach-Object {
# A running MCP process may still hold the old executable; retry on the next run.
# A READONLY attribute makes [IO.File]::Delete throw, so one read-only
diff --git a/src-tauri/src/commands/mcp_cleanup.rs b/src-tauri/src/commands/mcp_cleanup.rs
index d27029d364..112e5c9d7f 100644
--- a/src-tauri/src/commands/mcp_cleanup.rs
+++ b/src-tauri/src/commands/mcp_cleanup.rs
@@ -1,14 +1,35 @@
use std::fs::{self, Metadata};
use std::io;
use std::path::Path;
+use std::time::{Duration, SystemTime};
+
+/// A killed install leaves its working directory behind; only sweep it once it is
+/// clearly abandoned, so a concurrent install never loses the directory it is using.
+const INSTALL_DIRECTORY_MINIMUM_AGE: Duration = Duration::from_secs(60 * 60);
fn is_replacement_temp_file(name: &str) -> bool {
let name = name.to_ascii_lowercase();
- name.strip_prefix("dbx-mcp.exe~rf")
+ name.strip_prefix("dbx-mcp.exe")
+ .and_then(|suffix| suffix.strip_prefix('~').or_else(|| suffix.strip_prefix('.')))
+ .and_then(|suffix| suffix.strip_prefix("rf"))
.and_then(|suffix| suffix.strip_suffix(".tmp"))
.is_some_and(|id| !id.is_empty() && id.bytes().all(|byte| byte.is_ascii_hexdigit()))
}
+/// `.dbx-mcp-install.<guid N>`: the working directory one installer run creates.
+/// Its `finally` cleanup never runs when the install is hard-killed (#13009).
+fn is_install_work_directory(name: &str) -> bool {
+ name.to_ascii_lowercase()
+ .strip_prefix(".dbx-mcp-install.")
+ .is_some_and(|id| id.len() == 32 && id.bytes().all(|byte| byte.is_ascii_hexdigit()))
+}
+
+fn is_older_than(metadata: &Metadata, age: Duration) -> bool {
+ metadata
+ .modified()
+ .is_ok_and(|modified| SystemTime::now().duration_since(modified).is_ok_and(|elapsed| elapsed >= age))
+}
+
fn is_link(metadata: &Metadata) -> bool {
#[cfg(windows)]
{
@@ -22,6 +43,10 @@ fn is_link(metadata: &Metadata) -> bool {
}
pub(super) fn cleanup_directory(directory: &Path) -> io::Result<usize> {
+ sweep(directory, INSTALL_DIRECTORY_MINIMUM_AGE)
+}
+
+fn sweep(directory: &Path, minimum_install_age: Duration) -> io::Result<usize> {
let metadata = match fs::symlink_metadata(directory) {
Ok(metadata) => metadata,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(0),
@@ -34,13 +59,24 @@ pub(super) fn cleanup_directory(directory: &Path) -> io::Result<usize> {
let mut removed = 0;
for entry in fs::read_dir(directory)? {
let Ok(entry) = entry else { continue };
- if !entry.file_name().to_str().is_some_and(is_replacement_temp_file) {
- continue;
- }
+ let Ok(name) = entry.file_name().into_string() else { continue };
let path = entry.path();
let Ok(metadata) = fs::symlink_metadata(&path) else { continue };
- // Never recurse or follow links; locked leftovers can be retried next launch.
- if !metadata.is_file() || is_link(&metadata) {
+ // Never follow links; a matching name does not make the target ours. Locked
+ // leftovers can be retried next launch.
+ if is_link(&metadata) {
+ continue;
+ }
+ if metadata.is_dir() {
+ if is_install_work_directory(&name)
+ && is_older_than(&metadata, minimum_install_age)
+ && fs::remove_dir_all(&path).is_ok()
+ {
+ removed += 1;
+ }
+ continue;
+ }
+ if !metadata.is_file() || !is_replacement_temp_file(&name) {
continue;
}
// A READONLY attribute makes fs::remove_file fail with Access Denied on
@@ -92,7 +128,13 @@ mod tests {
#[test]
fn recognizes_only_mcp_replacement_temp_names() {
- for name in ["dbx-mcp.exe~RF8c73e4f.TMP", "DBX-MCP.EXE~rfAb123.tmp", "dbx-mcp.exe~RF1.TMP"] {
+ for name in [
+ "dbx-mcp.exe~RF8c73e4f.TMP",
+ "DBX-MCP.EXE~rfAb123.tmp",
+ "dbx-mcp.exe~RF1.TMP",
+ "dbx-mcp.exe.RF1A2B3C4D.TMP",
+ "DBX-MCP.EXE.rfabc.tmp",
+ ] {
assert!(is_replacement_temp_file(name), "{name}");
}
for name in [
@@ -104,11 +146,33 @@ mod tests {
"dbx-mcp.exe~RF123.TMP\n",
"other.exe~RF123.TMP",
"dbx-mcp.exe.tmp",
+ "dbx-mcp.exe.RF.TMP",
+ "dbx-mcp.exe.RFxyz.TMP",
+ "dbx-mcp.exe.1234.TMP",
] {
assert!(!is_replacement_temp_file(name), "{name}");
}
}
+ #[test]
+ fn removes_only_abandoned_install_work_directories() {
+ let directory = TestDirectory::new();
+ let abandoned = directory.0.join(".dbx-mcp-install.0123456789abcdef0123456789abcdef");
+ fs::create_dir(&abandoned).unwrap();
+ fs::write(abandoned.join("npm.tgz"), b"partial download").unwrap();
+ let unrelated = directory.0.join(".dbx-mcp-install.not-a-guid");
+ fs::create_dir(&unrelated).unwrap();
+
+ assert_eq!(sweep(&directory.0, Duration::ZERO).unwrap(), 1);
+ assert!(!abandoned.exists());
+ assert!(unrelated.exists());
+
+ let live = directory.0.join(".dbx-mcp-install.fedcba9876543210fedcba9876543210");
+ fs::create_dir(&live).unwrap();
+ assert_eq!(sweep(&directory.0, INSTALL_DIRECTORY_MINIMUM_AGE).unwrap(), 0);
+ assert!(live.exists());
+ }
+
#[test]
fn removes_only_matching_files_without_recursing() {
let directory = TestDirectory::new();
@@ -167,9 +231,12 @@ mod tests {
symlink(&target_file, &file_link).unwrap();
let dir_link = directory.0.join("linked-bin");
symlink(&target.0, &dir_link).unwrap();
+ let work_link = directory.0.join(".dbx-mcp-install.0123456789abcdef0123456789abcdef");
+ symlink(&target.0, &work_link).unwrap();
assert_eq!(cleanup_directory(&directory.0).unwrap(), 0);
assert_eq!(cleanup_directory(&dir_link).unwrap(), 0);
+ assert!(work_link.symlink_metadata().unwrap().file_type().is_symlink());
assert!(file_link.symlink_metadata().unwrap().file_type().is_symlink());
assert_eq!(fs::read(&target_file).unwrap(), b"keep");
}