clientupdate: don't uninstall the Windows client after a failed update

If msiexec failed to install an update for any reason, installMSI
assumed it was a downgrade, which msiexec does not permit, uninstalled
the current version and tried again. If the second attempt failed too,
such as when a custom action in the new package kept failing,
the device was left without Tailscale.

Uninstall the current version only if the requested version is older
than it, and do so before running the installer. Otherwise, return
the error and let Windows Installer roll back to the current version.

If the current version has been uninstalled and Windows Installer
is busy with another installation, keep retrying with a backoff
for up to 30 minutes rather than leaving the device without Tailscale.

Fixes tailscale/corp#45537
Updates tailscale/corp#52316

Change-Id: I066480c718b3bfca53a65c54ebf948562bc6ba2b
Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl authored and Nick Khyl committed 2026-12-01 14:43:23 -08:00
1 parent fbec7529aa
commit cd4f621399
1 file changed
+106 -64
+106 -64
View File
@@ -7,6 +7,7 @@
package clientupdate
import (
"context"
"errors"
"fmt"
"io"
@@ -20,6 +21,8 @@ import (
"github.com/google/uuid"
"golang.org/x/sys/windows"
"tailscale.com/util/backoff"
"tailscale.com/util/cmpver"
"tailscale.com/util/winutil"
"tailscale.com/util/winutil/authenticode"
)
@@ -32,7 +35,8 @@ const (
winMSIEnv = "TS_UPDATE_WIN_MSI"
// winVersionEnv is the environment variable that is set along with
// winMSIEnv and carries the version of tailscale that is being installed.
// It is used for logging purposes.
// It is used to name the log files and to tell whether the update is a downgrade,
// in which case the current version is uninstalled first.
winVersionEnv = "TS_UPDATE_WIN_VERSION"
// updaterPrefix is the prefix for the temporary executable created by [makeSelfCopy].
updaterPrefix = "tailscale-updater"
@@ -187,71 +191,109 @@ you can run the command prompt as Administrator one of these ways:
panic("unreachable")
}
func (up *Updater) installMSI(msi string) error {
var err error
for tries := 0; tries < 2; tries++ {
// msiexec.exe requires exclusive access to the log file, so create a dedicated one for each run.
installLogPath := up.startNewLogFile("tailscale-installer", os.Getenv(winVersionEnv))
up.Logf("Install log: %s", installLogPath)
cmd := exec.Command("msiexec.exe", "/i", filepath.Base(msi), "/quiet", "/norestart", "/qn", "/L*v", installLogPath)
cmd.Dir = filepath.Dir(msi)
cmd.Stdout = up.Stdout
cmd.Stderr = up.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
switch err := err.(type) {
case nil:
// Success.
return nil
case *exec.ExitError:
// For possible error codes returned by Windows Installer, see
// https://web.archive.org/web/20250409144914/https://learn.microsoft.com/en-us/windows/win32/msi/error-codes
switch windows.Errno(err.ExitCode()) {
case windows.ERROR_SUCCESS_REBOOT_REQUIRED:
// In most cases, updating Tailscale should not require a reboot.
// If it does, it might be because we failed to close the GUI
// and the installer couldn't replace its executable.
// The old GUI will continue to run until the next reboot.
// Not ideal, but also not a retryable error.
up.Logf("[unexpected] reboot required")
return nil
case windows.ERROR_SUCCESS_REBOOT_INITIATED:
// Same as above, but perhaps the device is configured to prompt
// the user to reboot and the user has chosen to reboot now.
up.Logf("[unexpected] reboot initiated")
return nil
case windows.ERROR_INSTALL_ALREADY_RUNNING:
// The Windows Installer service is currently busy.
// It could be our own install initiated by user/MDM/GP, another MSI install or perhaps a Windows Update install.
// Anyway, we can't do anything about it right now. The user (or tailscaled) can retry later.
// Retrying now will likely fail, and is risky since we might uninstall the current version
// and then fail to install the new one, leaving the user with no Tailscale at all.
//
// TODO(nickkhyl,awly): should we check if this is actually a downgrade before uninstalling the current version?
// Also, maybe keep retrying the install longer if we uninstalled the current version due to a failed install attempt?
up.Logf("another installation is already in progress")
return err
}
default:
// Everything else is a retryable error.
}
// installBusyRetryTimeout is how long installMSI keeps retrying while
// Windows Installer is busy, if it has already uninstalled the current version.
const installBusyRetryTimeout = 30 * time.Minute
up.Logf("Install attempt failed: %v", err)
uninstallVersion := up.currentVersion
if v := os.Getenv("TS_DEBUG_UNINSTALL_VERSION"); v != "" {
uninstallVersion = v
}
uninstallLogPath := up.startNewLogFile("tailscale-uninstaller", uninstallVersion)
// Assume it's a downgrade, which msiexec won't permit. Uninstall our current version first.
up.Logf("Uninstalling current version %q for downgrade...", uninstallVersion)
up.Logf("Uninstall log: %s", uninstallLogPath)
cmd = exec.Command("msiexec.exe", "/x", msiUUIDForVersion(uninstallVersion), "/norestart", "/qn", "/L*v", uninstallLogPath)
cmd.Stdout = up.Stdout
cmd.Stderr = up.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
up.Logf("msiexec uninstall: %v", err)
func (up *Updater) installMSI(msi string) error {
// msiexec won't permit a downgrade, so uninstall the current version first.
// Only do it if we know it is a downgrade. A failed upgrade is rolled back
// by Windows Installer, whereas uninstalling the current version after
// a failure would leave the device without Tailscale if the install fails again.
var uninstalled bool
if ver := os.Getenv(winVersionEnv); ver != "" && cmpver.Compare(ver, up.currentVersion) < 0 {
uninstalled = up.uninstallCurrentMSI() == nil
}
if !uninstalled {
return up.runMSIInstall(msi)
}
// The device has no Tailscale until the install succeeds,
// so don't give up right away if Windows Installer is busy.
ctx, cancel := context.WithTimeout(context.Background(), installBusyRetryTimeout)
defer cancel()
bo := backoff.NewBackoff("msiexec", up.Logf, time.Minute, backoff.WithInitialBackoff(10*time.Second))
for {
err := up.runMSIInstall(msi)
if err == nil || ctx.Err() != nil {
return err
}
if !isRetryableInstallError(err) {
return err
}
bo.BackOff(ctx, err)
}
}
func isRetryableInstallError(err error) bool {
ee, ok := err.(*exec.ExitError)
if !ok {
return false
}
// ERROR_INSTALL_ALREADY_RUNNING indicates that the Windows Installer
// is busy installing/uninstalling/updating another package.
// We can retry the installation later.
return windows.Errno(ee.ExitCode()) == windows.ERROR_INSTALL_ALREADY_RUNNING
}
// runMSIInstall runs msiexec to install msi once.
func (up *Updater) runMSIInstall(msi string) error {
// msiexec.exe requires exclusive access to the log file, so create a dedicated one for each run.
installLogPath := up.startNewLogFile("tailscale-installer", os.Getenv(winVersionEnv))
up.Logf("Install log: %s", installLogPath)
return up.runMSIExec(filepath.Dir(msi), "/i", filepath.Base(msi), "/quiet", "/norestart", "/qn", "/L*v", installLogPath)
}
// runMSIExec runs msiexec.exe with the specified args in dir,
// or in the current directory if dir is empty.
// It returns nil if msiexec succeeded, even if a reboot is required
// to complete the installation or uninstallation.
func (up *Updater) runMSIExec(dir string, args ...string) error {
cmd := exec.Command("msiexec.exe", args...)
cmd.Dir = dir
cmd.Stdout = up.Stdout
cmd.Stderr = up.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err, ok := err.(*exec.ExitError); ok {
// For possible error codes returned by Windows Installer, see
// https://web.archive.org/web/20250409144914/https://learn.microsoft.com/en-us/windows/win32/msi/error-codes
switch windows.Errno(err.ExitCode()) {
case windows.ERROR_SUCCESS_REBOOT_REQUIRED:
// In most cases, updating Tailscale should not require a reboot.
// If it does, it might be because we failed to close the GUI
// and the installer couldn't replace its executable.
// The old GUI will continue to run until the next reboot.
// Not ideal, but also not an error.
up.Logf("[unexpected] reboot required")
return nil
case windows.ERROR_SUCCESS_REBOOT_INITIATED:
// Same as above, but perhaps the device is configured to prompt
// the user to reboot and the user has chosen to reboot now.
up.Logf("[unexpected] reboot initiated")
return nil
case windows.ERROR_INSTALL_ALREADY_RUNNING:
// The Windows Installer service is currently busy.
// It could be our own install initiated by user/MDM/GP, another MSI install or perhaps a Windows Update install.
// installMSI retries if it has uninstalled the current version.
// Otherwise, the user (or tailscaled) can retry later.
up.Logf("another installation is already in progress")
}
}
return err
}
// uninstallCurrentMSI uninstalls the currently installed version of Tailscale.
func (up *Updater) uninstallCurrentMSI() error {
uninstallVersion := up.currentVersion
if v := os.Getenv("TS_DEBUG_UNINSTALL_VERSION"); v != "" {
uninstallVersion = v
}
uninstallLogPath := up.startNewLogFile("tailscale-uninstaller", uninstallVersion)
up.Logf("Uninstalling current version %q for downgrade...", uninstallVersion)
up.Logf("Uninstall log: %s", uninstallLogPath)
err := up.runMSIExec("", "/x", msiUUIDForVersion(uninstallVersion), "/norestart", "/qn", "/L*v", uninstallLogPath)
up.Logf("msiexec uninstall: %v", err)
return err
}