From 701019052c02e9d5cd94945d7285092b8257feed Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Thu, 28 Aug 2025 17:09:54 +0100 Subject: [PATCH] MatrixChat: only start session load once (#30642) * MatrixChat: only start session load once When running in development mode, `MatrixChat.componentDidMount` is run twice, meaning it checks the session lock twice. Sometimes, this means it shows the "Element is running in another window" page. The real problem is of course that we're running all this application logic inside the `MatrixChat` component, but as a quick workaround, we can just remember that we've started the session load code, and bail out on the second pass. * Address review comments --- src/components/structures/MatrixChat.tsx | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index 39c0147da0..62831716c3 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -238,6 +238,8 @@ export default class MatrixChat extends React.PureComponent { private readonly stores: SdkContextClass; private loadSessionAbortController = new AbortController(); + private sessionLoadStarted = false; + public constructor(props: IProps) { super(props); this.stores = SdkContextClass.instance; @@ -470,15 +472,20 @@ export default class MatrixChat extends React.PureComponent { this.fontWatcher.start(); initSentry(SdkConfig.get("sentry")); - - if (!checkSessionLockFree()) { - // another instance holds the lock; confirm its theft before proceeding - setTimeout(() => this.setState({ view: Views.CONFIRM_LOCK_THEFT }), 0); - } else { - this.startInitSession(); - } - window.addEventListener("resize", this.onWindowResized); + + // Once we start loading the MatrixClient, we can't stop, even if MatrixChat gets unmounted (as it does + // in React's Strict Mode). So, start loading the session now, but only if this MatrixChat was not previously + // mounted. + if (!this.sessionLoadStarted) { + this.sessionLoadStarted = true; + if (!checkSessionLockFree()) { + // another instance holds the lock; confirm its theft before proceeding + setTimeout(() => this.setState({ view: Views.CONFIRM_LOCK_THEFT }), 0); + } else { + this.startInitSession(); + } + } } public componentDidUpdate(prevProps: IProps, prevState: IState): void {