Skip to content

Commit 370ff74

Browse files
Filip Mösnerpanther7
authored andcommitted
feat(nsis): terminate only processes running in installation folder
Modified the process termination logic in NSIS scripts to only kill application processes that are running from the installation directory. This prevents accidental termination of processes with the same name running from other locations during installation or update.
1 parent 6755006 commit 370ff74

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

.changeset/flat-trees-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"app-builder-lib": minor
3+
---
4+
5+
feat(nsis): terminate only processes running in installation folder

packages/app-builder-lib/templates/nsis/include/allowOnlyOneInstallerInstance.nsh

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
!ifndef nsProcess::FindProcess
2-
!include "nsProcess.nsh"
3-
!endif
4-
51
!ifmacrondef customCheckAppRunning
62
!include "getProcessInfo.nsh"
73
Var pid
@@ -37,14 +33,9 @@
3733
!endif
3834
!macroend
3935

40-
!macro FIND_PROCESS _FILE _ERR
41-
!ifdef INSTALL_MODE_PER_ALL_USERS
42-
${nsProcess::FindProcess} "${_FILE}" ${_ERR}
43-
!else
44-
# find process owned by current user
45-
nsExec::Exec `"$SYSDIR\cmd.exe" /c tasklist /FI "USERNAME eq %USERNAME%" /FI "IMAGENAME eq ${_FILE}" /FO csv | "$SYSDIR\find.exe" "${_FILE}"`
46-
Pop ${_ERR}
47-
!endif
36+
!macro FIND_PROCESS _PATH _ERR
37+
nsExec::Exec `powershell -Command "if ((Get-Process | Where-Object {$$_.Path -and $$_.Path.StartsWith('${_PATH}')}).Count -gt 0) {exit 0} else {exit 1}"`
38+
Pop ${_ERR}
4839
!macroend
4940

5041
!macro _CHECK_APP_RUNNING
@@ -55,7 +46,7 @@
5546
Sleep 300
5647
${endIf}
5748

58-
!insertmacro FIND_PROCESS "${APP_EXECUTABLE_FILENAME}" $R0
49+
!insertmacro FIND_PROCESS "$INSTDIR" $R0
5950
${if} $R0 == 0
6051
${if} ${isUpdated}
6152
# allow app to exit without explicit kill
@@ -69,12 +60,7 @@
6960

7061
DetailPrint `Closing running "${PRODUCT_NAME}"...`
7162

72-
# https://github.com/electron-userland/electron-builder/issues/2516#issuecomment-372009092
73-
!ifdef INSTALL_MODE_PER_ALL_USERS
74-
nsExec::Exec `taskkill /im "${APP_EXECUTABLE_FILENAME}" /fi "PID ne $pid"`
75-
!else
76-
nsExec::Exec `"$SYSDIR\cmd.exe" /c taskkill /im "${APP_EXECUTABLE_FILENAME}" /fi "PID ne $pid" /fi "USERNAME eq %USERNAME%"`
77-
!endif
63+
nsExec::Exec `powershell -Command "Get-Process | ?{$$_.Path -and $$_.Path.StartsWith('$INSTDIR')} | Stop-Process -Force"`
7864
# to ensure that files are not "in-use"
7965
Sleep 300
8066

@@ -84,16 +70,12 @@
8470
loop:
8571
IntOp $R1 $R1 + 1
8672

87-
!insertmacro FIND_PROCESS "${APP_EXECUTABLE_FILENAME}" $R0
73+
!insertmacro FIND_PROCESS "$INSTDIR" $R0
8874
${if} $R0 == 0
8975
# wait to give a chance to exit gracefully
9076
Sleep 1000
91-
!ifdef INSTALL_MODE_PER_ALL_USERS
92-
nsExec::Exec `taskkill /f /im "${APP_EXECUTABLE_FILENAME}" /fi "PID ne $pid"`
93-
!else
94-
nsExec::Exec `"$SYSDIR\cmd.exe" /c taskkill /f /im "${APP_EXECUTABLE_FILENAME}" /fi "PID ne $pid" /fi "USERNAME eq %USERNAME%"`
95-
!endif
96-
!insertmacro FIND_PROCESS "${APP_EXECUTABLE_FILENAME}" $R0
77+
nsExec::Exec `powershell -Command "Get-Process | ?{$$_.Path -and $$_.Path.StartsWith('$INSTDIR')} | Stop-Process -Force"`
78+
!insertmacro FIND_PROCESS "$INSTDIR" $R0
9779
${If} $R0 == 0
9880
DetailPrint `Waiting for "${PRODUCT_NAME}" to close.`
9981
Sleep 2000

0 commit comments

Comments
 (0)