From 603f6924736f20014f39c6b57dffe4e39c9ba7d4 Mon Sep 17 00:00:00 2001 From: Angelika Serwa Date: Thu, 24 Oct 2024 21:20:55 +0200 Subject: [PATCH] [lib] Close tunnelbroker socket when app is in background Summary: This diff closes tunnelbroker socket when app goes to background in order to prevent messages from decrypting twice and weird socket state. https://linear.app/comm/issue/ENG-9741/tunnelbroker-socket-onmessage-is-triggered-before-re-opening-socket Test Plan: 1. Run the app 2. Put the app in the background 3. Send some DMs to the app 4. Bring the app to the foreground 5. Observe in logs that the socket is closed after going to the background and verify that no message is received before closing the socket Reviewers: tomek, kamil Reviewed By: kamil Subscribers: ashoat Differential Revision: https://phab.comm.dev/D13787 --- lib/tunnelbroker/tunnelbroker-context.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/tunnelbroker/tunnelbroker-context.js b/lib/tunnelbroker/tunnelbroker-context.js index 32975d6b84..cccb0a3423 100644 --- a/lib/tunnelbroker/tunnelbroker-context.js +++ b/lib/tunnelbroker/tunnelbroker-context.js @@ -110,6 +110,9 @@ function TunnelbrokerProvider(props: Props): React.Node { const accessToken = useSelector(state => state.commServicesAccessToken); const userID = useSelector(state => state.currentUserInfo?.id); + const isAppActive = useSelector( + state => state.lifecycleState !== 'background', + ); const [unauthorizedDeviceID, setUnauthorizedDeviceID] = React.useState(null); @@ -440,6 +443,12 @@ function TunnelbrokerProvider(props: Props): React.Node { [secondaryTunnelbrokerConnection], ); + React.useEffect(() => { + if (!isAppActive) { + socket.current?.close(); + } + }, [isAppActive]); + const addListener = React.useCallback( (listener: TunnelbrokerSocketListener) => { listeners.current.add(listener);