@@ -46,6 +46,42 @@ function getCurrentPlatform() {
4646 return platformKey ;
4747}
4848
49+ async function killProcessesUsingBinary ( binaryPath ) {
50+ if ( process . platform === 'win32' ) {
51+ // Windows: use handle.exe or just try to copy
52+ return ;
53+ }
54+
55+ try {
56+ // Use lsof to find processes using the binary
57+ const { execSync } = await import ( 'node:child_process' ) ;
58+ const output = execSync ( `lsof "${ binaryPath } " 2>/dev/null || true` , { encoding : 'utf-8' } ) ;
59+
60+ if ( ! output . trim ( ) ) {
61+ return ; // No processes using the file
62+ }
63+
64+ // Extract PIDs (skip header line)
65+ const lines = output . trim ( ) . split ( '\n' ) . slice ( 1 ) ;
66+ const pids = [ ...new Set ( lines . map ( line => line . split ( / \s + / ) [ 1 ] ) . filter ( Boolean ) ) ] ;
67+
68+ if ( pids . length > 0 ) {
69+ console . log ( `⚠️ Found ${ pids . length } process(es) using ${ path . basename ( binaryPath ) } , stopping them...` ) ;
70+ for ( const pid of pids ) {
71+ try {
72+ execSync ( `kill ${ pid } ` , { stdio : 'ignore' } ) ;
73+ } catch ( e ) {
74+ // Process might already be dead
75+ }
76+ }
77+ // Give processes time to exit
78+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
79+ }
80+ } catch ( e ) {
81+ // lsof not available or other error, continue anyway
82+ }
83+ }
84+
4985async function copyBinary ( binaryName , platformKey ) {
5086 const isWindows = platformKey . startsWith ( 'windows-' ) ;
5187 const sourceExt = isWindows ? '.exe' : '' ;
@@ -67,6 +103,14 @@ async function copyBinary(binaryName, platformKey) {
67103 // Ensure destination directory exists
68104 await fs . mkdir ( destDir , { recursive : true } ) ;
69105
106+ // Kill any processes using the destination binary
107+ try {
108+ await fs . access ( destPath ) ;
109+ await killProcessesUsingBinary ( destPath ) ;
110+ } catch ( e ) {
111+ // Destination doesn't exist yet, that's fine
112+ }
113+
70114 // Copy binary
71115 await fs . copyFile ( sourcePath , destPath ) ;
72116
0 commit comments