Skip to content

Commit f8d3b5b

Browse files
Merge pull request #201 from community-scripts/fix/orphan-cleanup-race
fix: harden orphan cleanup to avoid false deletions
2 parents cc8fd3e + e4de9cc commit f8d3b5b

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/server/api/routers/installedScripts.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -908,21 +908,44 @@ export const installedScriptsRouter = createTRPCRouter({
908908
// Check if the container config file still exists
909909
const checkCommand = `test -f "/etc/pve/lxc/${scriptData.container_id}.conf" && echo "exists" || echo "not_found"`;
910910

911+
// Await full command completion to avoid early false negatives
911912
const containerExists = await new Promise<boolean>((resolve) => {
912-
913+
let combinedOutput = '';
914+
let resolved = false;
915+
916+
const finish = () => {
917+
if (resolved) return;
918+
resolved = true;
919+
const out = combinedOutput.trim();
920+
if (out.includes('exists')) {
921+
resolve(true);
922+
} else if (out.includes('not_found')) {
923+
resolve(false);
924+
} else {
925+
// Unknown output; treat as not found but log for diagnostics
926+
console.warn(`cleanupOrphanedScripts: unexpected output for ${String(scriptData.script_name)} (${String(scriptData.container_id)}): ${out}`);
927+
resolve(false);
928+
}
929+
};
930+
931+
// Add a guard timeout so we don't hang indefinitely
932+
const timer = setTimeout(() => {
933+
console.warn(`cleanupOrphanedScripts: timeout while checking ${String(scriptData.script_name)} on server ${String((server as any).name)}`);
934+
finish();
935+
}, 15000);
936+
913937
void sshExecutionService.executeCommand(
914-
915938
server as Server,
916939
checkCommand,
917940
(data: string) => {
918-
resolve(data.trim() === 'exists');
941+
combinedOutput += data;
919942
},
920943
(error: string) => {
921-
console.error(`Error checking container ${scriptData.script_name}:`, error);
922-
resolve(false);
944+
combinedOutput += error;
923945
},
924946
(_exitCode: number) => {
925-
resolve(false);
947+
clearTimeout(timer);
948+
finish();
926949
}
927950
);
928951
});

0 commit comments

Comments
 (0)