Fix CrashHandler failing to recover and showing gray screen instead
This commit is contained in:
parent
cc0d9a90bc
commit
38beb93e5f
|
@ -25,7 +25,6 @@ import definePlugin, { OptionType } from "@utils/types";
|
|||
import { maybePromptToUpdate } from "@utils/updater";
|
||||
import { filters, findBulk, proxyLazyWebpack } from "@webpack";
|
||||
import { FluxDispatcher, NavigationRouter, SelectedChannelStore } from "@webpack/common";
|
||||
import type { ReactElement } from "react";
|
||||
|
||||
const CrashHandlerLogger = new Logger("CrashHandler");
|
||||
const { ModalStack, DraftManager, DraftType, closeExpressionPicker } = proxyLazyWebpack(() => {
|
||||
|
@ -57,13 +56,12 @@ const settings = definePluginSettings({
|
|||
}
|
||||
});
|
||||
|
||||
let crashCount: number = 0;
|
||||
let lastCrashTimestamp: number = 0;
|
||||
let shouldAttemptNextHandle = false;
|
||||
let hasCrashedOnce = false;
|
||||
let shouldAttemptRecover = true;
|
||||
|
||||
export default definePlugin({
|
||||
name: "CrashHandler",
|
||||
description: "Utility plugin for handling and possibly recovering from Crashes without a restart",
|
||||
description: "Utility plugin for handling and possibly recovering from crashes without a restart",
|
||||
authors: [Devs.Nuckyz],
|
||||
enabledByDefault: true,
|
||||
|
||||
|
@ -74,51 +72,47 @@ export default definePlugin({
|
|||
find: ".Messages.ERRORS_UNEXPECTED_CRASH",
|
||||
replacement: {
|
||||
match: /(?=this\.setState\()/,
|
||||
replace: "$self.handleCrash(this)||"
|
||||
replace: "$self.handleCrash(this);"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
handleCrash(_this: ReactElement & { forceUpdate: () => void; }) {
|
||||
if (Date.now() - lastCrashTimestamp <= 1_000 && !shouldAttemptNextHandle) return true;
|
||||
|
||||
shouldAttemptNextHandle = false;
|
||||
|
||||
if (++crashCount > 5) {
|
||||
handleCrash(_this: any) {
|
||||
// 1 ms timeout to avoid react breaking when re-rendering
|
||||
setTimeout(() => {
|
||||
if (!shouldAttemptRecover) {
|
||||
try {
|
||||
showNotification({
|
||||
color: "#eed202",
|
||||
title: "Discord has crashed!",
|
||||
body: "Awn :( Discord has crashed more than five times, not attempting to recover.",
|
||||
body: "Awn :( Discord has crashed two times rapidly, not attempting to recover.",
|
||||
noPersist: true,
|
||||
});
|
||||
} catch { }
|
||||
|
||||
lastCrashTimestamp = Date.now();
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
setTimeout(() => crashCount--, 60_000);
|
||||
shouldAttemptRecover = false;
|
||||
// This is enough to avoid a crash loop
|
||||
setTimeout(() => shouldAttemptRecover = true, 500);
|
||||
|
||||
try {
|
||||
if (crashCount === 1) maybePromptToUpdate("Uh oh, Discord has just crashed... but good news, there is a Vencord update available that might fix this issue! Would you like to update now?", true);
|
||||
if (!hasCrashedOnce) {
|
||||
hasCrashedOnce = true;
|
||||
maybePromptToUpdate("Uh oh, Discord has just crashed... but good news, there is a Vencord update available that might fix this issue! Would you like to update now?", true);
|
||||
}
|
||||
|
||||
if (settings.store.attemptToPreventCrashes) {
|
||||
this.handlePreventCrash(_this);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (err) {
|
||||
CrashHandlerLogger.error("Failed to handle crash", err);
|
||||
return false;
|
||||
} finally {
|
||||
lastCrashTimestamp = Date.now();
|
||||
}
|
||||
}, 1);
|
||||
},
|
||||
|
||||
handlePreventCrash(_this: ReactElement & { forceUpdate: () => void; }) {
|
||||
if (Date.now() - lastCrashTimestamp >= 1_000) {
|
||||
handlePreventCrash(_this: any) {
|
||||
try {
|
||||
showNotification({
|
||||
color: "#eed202",
|
||||
|
@ -127,7 +121,6 @@ export default definePlugin({
|
|||
noPersist: true,
|
||||
});
|
||||
} catch { }
|
||||
}
|
||||
|
||||
try {
|
||||
const channelId = SelectedChannelStore.getChannelId();
|
||||
|
@ -177,8 +170,7 @@ export default definePlugin({
|
|||
}
|
||||
|
||||
try {
|
||||
shouldAttemptNextHandle = true;
|
||||
_this.forceUpdate();
|
||||
_this.setState({ error: null, info: null });
|
||||
} catch (err) {
|
||||
CrashHandlerLogger.debug("Failed to update crash handler component.", err);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue