Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround Squirrel.Mac wedging app restart after failed update check #629

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { autoUpdater, ipcMain } from "electron";
import { app, autoUpdater, ipcMain } from "electron";

const UPDATE_POLL_INTERVAL_MS = 60 * 60 * 1000;
const INITIAL_UPDATE_DELAY_MS = 30 * 1000;
Expand All @@ -26,15 +26,45 @@ function installUpdate(): void {
autoUpdater.quitAndInstall();
}

function pollForUpdates(): void {
// Workaround for Squirrel.Mac wedging auto-restart if latest check for update failed
// From https://github.com/vector-im/element-web/issues/12433#issuecomment-1508995119
async function safeCheckForUpdate(): Promise<void> {
if (process.platform === "darwin") {
const feedUrl = autoUpdater.getFeedURL();
// On Mac if the user has already downloaded an update but not installed it and
// we check again and no additional new update is available the app ends up in a
// bad state and doesn't restart after installing any updates that are downloaded.
Comment on lines +34 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

long sentence is long. Punctuation ftw.

// To avoid this we check manually whether an update is available and call the
// autoUpdater.checkForUpdates() when something new is there.
Comment on lines +37 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// To avoid this we check manually whether an update is available and call the
// autoUpdater.checkForUpdates() when something new is there.
// To avoid this we check manually whether an update is available and only call
// autoUpdater.checkForUpdates() when something new is there.

try {
const res = await global.fetch(feedUrl);
const { currentRelease } = await res.json();
const latestVersionDownloaded = latestUpdateDownloaded.releaseName;
console.info(
`Latest version from release download: ${currentRelease} (current: ${app.getVersion()}, most recent downloaded ${latestVersionDownloaded}})`,
);
if (currentRelease === app.getVersion() || currentRelease === latestVersionDownloaded) {
ipcChannelSendUpdateStatus(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ipcChannelSendUpdateStatus(false);
// We are already running, or have downloaded, the latest version.
// Skip the update check.
ipcChannelSendUpdateStatus(false);

return;
}
} catch (err) {
console.error(`Error checking for updates ${feedUrl}`, err);
ipcChannelSendUpdateStatus(false);
return;
}
}
autoUpdater.checkForUpdates();
}

async function pollForUpdates(): Promise<void> {
try {
// If we've already got a new update downloaded, then stop trying to check for new ones, as according to the doc
// at https://github.com/electron/electron/blob/main/docs/api/auto-updater.md#autoupdatercheckforupdates
// we'll just keep re-downloading the same update.
// As a hunch, this might also be causing https://github.com/vector-im/element-web/issues/12433
// due to the update checks colliding with the pending install somehow
if (!latestUpdateDownloaded) {
autoUpdater.checkForUpdates();
await safeCheckForUpdate();
} else {
console.log("Skipping update check as download already present");
global.mainWindow?.webContents.send("update-downloaded", latestUpdateDownloaded);
Expand Down