diff --git a/packages/electron-builder-lib/templates/nsis/include/allowOnlyOneInstallerInstance.nsh b/packages/electron-builder-lib/templates/nsis/include/allowOnlyOneInstallerInstance.nsh index e02af56167a..a907f972464 100644 --- a/packages/electron-builder-lib/templates/nsis/include/allowOnlyOneInstallerInstance.nsh +++ b/packages/electron-builder-lib/templates/nsis/include/allowOnlyOneInstallerInstance.nsh @@ -1,4 +1,5 @@ !include "getProcessInfo.nsh" +!include "nsProcess.nsh" # http://nsis.sourceforge.net/Allow_only_one_installer_instance !macro ALLOW_ONLY_ONE_INSTALLER_INSTANCE @@ -27,10 +28,13 @@ Var pid !macro CHECK_APP_RUNNING ${GetProcessInfo} 0 $pid $1 $2 $3 $4 ${if} $3 != "${APP_EXECUTABLE_FILENAME}" - nsProcess::_FindProcess /NOUNLOAD "${APP_EXECUTABLE_FILENAME}" - Pop $R0 - nsProcess::_Unload - ${If} $R0 == 0 + ${if} ${isUpdated} + # allow app to exit without explicit kill + Sleep 100 + ${endIf} + + ${nsProcess::FindProcess} "${APP_EXECUTABLE_FILENAME}" $R0 + ${if} $R0 == 0 ${if} ${isUpdated} # allow app to exit without explicit kill Sleep 1000 @@ -38,16 +42,29 @@ Var pid ${endIf} MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "$(appRunning)" /SD IDOK IDOK doStopProcess Quit + doStopProcess: - DetailPrint 'Closing running "${PRODUCT_NAME}"...' - ExecWait 'taskkill /f /t /im "${APP_EXECUTABLE_FILENAME}" /fi "PID ne $pid"' $R0 - ${If} $R0 == 0 - # to ensure that files are not "in-use" - Sleep 100 - ${else} + + DetailPrint 'Closing running "${PRODUCT_NAME}"...' + + # https://github.com/electron-userland/electron-builder/issues/2516#issuecomment-372009092 + ExecWait 'taskkill /im "${APP_EXECUTABLE_FILENAME}" /fi "PID ne $pid"' $R0 + # to ensure that files are not "in-use" + Sleep 100 + + ${nsProcess::FindProcess} "${APP_EXECUTABLE_FILENAME}" $R0 + ${if} $R0 == 0 + # wait to give a chance to exit gracefully + Sleep 1000 + # do not use /t tree kill - app was killed softly already + ExecWait 'taskkill /f /im "${APP_EXECUTABLE_FILENAME}" /fi "PID ne $pid"' $R0 + ${If} $R0 != 0 DetailPrint 'Waiting for "${PRODUCT_NAME}" to close (taskkill exit code $R0).' Sleep 2000 ${endIf} + ${endIf} ${endIf} ${endIf} + + ${nsProcess::Unload} !macroend \ No newline at end of file diff --git a/packages/electron-builder-lib/templates/nsis/include/nsProcess.nsh b/packages/electron-builder-lib/templates/nsis/include/nsProcess.nsh new file mode 100644 index 00000000000..9ef6098d226 --- /dev/null +++ b/packages/electron-builder-lib/templates/nsis/include/nsProcess.nsh @@ -0,0 +1,28 @@ +!define nsProcess::FindProcess `!insertmacro nsProcess::FindProcess` + +!macro nsProcess::FindProcess _FILE _ERR + nsProcess::_FindProcess /NOUNLOAD `${_FILE}` + Pop ${_ERR} +!macroend + + +!define nsProcess::KillProcess `!insertmacro nsProcess::KillProcess` + +!macro nsProcess::KillProcess _FILE _ERR + nsProcess::_KillProcess /NOUNLOAD `${_FILE}` + Pop ${_ERR} +!macroend + +!define nsProcess::CloseProcess `!insertmacro nsProcess::CloseProcess` + +!macro nsProcess::CloseProcess _FILE _ERR + nsProcess::_CloseProcess /NOUNLOAD `${_FILE}` + Pop ${_ERR} +!macroend + + +!define nsProcess::Unload `!insertmacro nsProcess::Unload` + +!macro nsProcess::Unload + nsProcess::_Unload +!macroend