Skip to content

Commit

Permalink
[lib] Stop processOutboundMessages loop when it becomes outdated
Browse files Browse the repository at this point in the history
Summary:
This addresses [ENG-9454](https://linear.app/comm/issue/ENG-9454/cannot-read-properties-of-undefined-when-leaving-thread) and [ENG-9448](https://linear.app/comm/issue/ENG-9448/robotext-doesnt-display-for-first-farcaster-mutual).

When we update this callback, we should make sure we stop running old versions of the callback. Otherwise, there's a risk that we'll process new data with callbacks that are bound to old versions of the Redux state.

Test Plan:
I had a repro of ENG-9448 where I simply logged out and back in with my `t125` test user. After this diff:

1. I no longer see `Cannot read properties of undefined` errors
2. The robotext messages are delivered for all of my Farcaster friends

Reviewers: varun, will, tomek, kamil

Reviewed By: varun

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D13563
  • Loading branch information
Ashoat committed Oct 1, 2024
1 parent 93a00ce commit 11b7e6f
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/tunnelbroker/peer-to-peer-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,10 @@ function PeerToPeerProvider(props: Props): React.Node {
[allPeerDevices],
);

const processOutboundMessages = React.useCallback(
(
const processOutboundMessagesLatestVersionRef = React.useRef(-1);
const processOutboundMessages = React.useMemo(() => {
const currentVersion = ++processOutboundMessagesLatestVersionRef.current;
return (
outboundMessageIDs: ?$ReadOnlyArray<string>,
dmOpID: ?string,
notificationsCreationData: ?NotificationsCreationData,
Expand All @@ -306,6 +308,11 @@ function PeerToPeerProvider(props: Props): React.Node {
promiseRunning.current = true;
void (async () => {
do {
if (
currentVersion !== processOutboundMessagesLatestVersionRef.current
) {
break;
}
const queueFront = processingQueue.current.shift();
try {
const [result] = await Promise.all([
Expand Down Expand Up @@ -343,15 +350,14 @@ function PeerToPeerProvider(props: Props): React.Node {
promiseRunning.current = false;
})();
}
},
[
allPeerDevicesSet,
sendMessageToDevice,
identityContext,
peerOlmSessionsCreator,
sendPushNotifs,
],
);
};
}, [
allPeerDevicesSet,
sendMessageToDevice,
identityContext,
peerOlmSessionsCreator,
sendPushNotifs,
]);

const broadcastEphemeralMessage = React.useCallback(
async (
Expand Down

0 comments on commit 11b7e6f

Please sign in to comment.