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

[Awaiting Payment April 29th] HIGH: [Performance] [$500] Reliably unsubscribe from isTyping events when leaving rooms #38905

Closed
quinthar opened this issue Mar 25, 2024 · 38 comments
Assignees
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 Engineering External Added to denote the issue can be worked on by a contributor Improvement Item broken or needs improvement. Task

Comments

@quinthar
Copy link
Contributor

quinthar commented Mar 25, 2024

Problem:

When you join a room, you subscribe to various Pusher channels that should only be active while in that room. However, when you leave the room, sometimes it doesn't unsubscribe you from the channels. This "leak" will gradually slow down the app. But particularly concerning is that it will cause you to receive Pusher events that do not relate to the current room (such as "istyping" events in rooms you aren't looking at), and thus drag down your performance.

Solution:

Reliably unsubscribe from the room's Pusher channel when leaving by any method. There is a theory that we do this when leaving a room to switch to another, but perhaps not when leaving the room to go to a non-room page (eg, Settings).

See: https://expensify.slack.com/archives/C049HHMV9SM/p1711131919203929

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~012a36c87d591ea59b
  • Upwork Job ID: 1773645194916745216
  • Last Price Increase: 2024-03-29
  • Automatic offers:
    • jjcoffee | Reviewer | 0
    • tienifr | Contributor | 0
@quinthar quinthar converted this from a draft issue Mar 25, 2024
@quinthar quinthar added Engineering Weekly KSv2 Improvement Item broken or needs improvement. Task labels Mar 25, 2024
Copy link

melvin-bot bot commented Mar 25, 2024

Auto-assigning issues to engineers is no longer supported. If you think this issue should receive engineering attention, please raise it in #whatsnext.

@iwiznia iwiznia removed their assignment Mar 25, 2024
@iwiznia iwiznia added the Bug Something is broken. Auto assigns a BugZero manager. label Mar 25, 2024
Copy link

melvin-bot bot commented Mar 25, 2024

Triggered auto assignment to @dylanexpensify (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

@melvin-bot melvin-bot bot added Daily KSv2 and removed Weekly KSv2 labels Mar 25, 2024
@dylanexpensify
Copy link
Contributor

Nice! @quinthar is this something we wanna get a contributor on?

@quinthar
Copy link
Contributor Author

yes please!

@melvin-bot melvin-bot bot added the Overdue label Mar 29, 2024
@dylanexpensify dylanexpensify added the External Added to denote the issue can be worked on by a contributor label Mar 29, 2024
@melvin-bot melvin-bot bot changed the title HIGH: [Performance] Reliably unsubscribe from isTyping events when leaving rooms [$500] HIGH: [Performance] Reliably unsubscribe from isTyping events when leaving rooms Mar 29, 2024
Copy link

melvin-bot bot commented Mar 29, 2024

Job added to Upwork: https://www.upwork.com/jobs/~012a36c87d591ea59b

@melvin-bot melvin-bot bot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Mar 29, 2024
Copy link

melvin-bot bot commented Mar 29, 2024

Triggered auto assignment to Contributor-plus team member for initial proposal review - @jjcoffee (External)

@melvin-bot melvin-bot bot removed the Overdue label Mar 29, 2024
@dylanexpensify
Copy link
Contributor

Let's get it!

@ShridharGoel
Copy link
Contributor

ShridharGoel commented Mar 29, 2024

Proposal

Please re-state the problem that we are trying to solve in this issue.

We need to unsubscribe from isTyping events when leaving rooms.

What is the root cause of that problem?

Pusher remains subscribed even after leaving the room.

What changes do you think we should make in order to solve the problem?

In ReportActionsView.ts, we are calling Report.subscribeToReportTypingEvents(reportID);.

We can call the below to unsubscribe in a useEffect when component unmounts:

// Unsubscribe from Pusher events when component unmounts
return () => {
    Report.unsubscribeFromReportTypingEvents(reportID);
};

@tienifr
Copy link
Contributor

tienifr commented Mar 29, 2024

Proposal

Please re-state the problem that we are trying to solve in this issue.

When you join a room, you subscribe to various Pusher channels that should only be active while in that room. However, when you leave the room, sometimes it doesn't unsubscribe you from the channels. This "leak" will gradually slow down the app.

What is the root cause of that problem?

In here, we already have the logic to unsubscribe from the isTyping event. However this only happens when the report screen is unmounted, it won't happen if the report screen simply loses focus (is no longer the topmost report)

What changes do you think we should make in order to solve the problem?

We need to unsubscribe from report channel if the screen loses focus, and the reportId is no longer the topmostReportId.

Then we'll resubscribe if the report screen gains focus.

We might want to do it in ReportActionsView because that's where we're subscribing to the event, but moving both to ReportActionsList is also fine.

Pseudo-code (to replace this event subscription logic, some more polishing might be needed)

useEffect(() => {
       if (route?.params?.reportID !== reportID) {
           return;
       }
       
       if (isFocused) {
           const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat);
           
           if (!didSubscribeToReportTypingEvents.current && didCreateReportSuccessfully) {
               const interactionTask = InteractionManager.runAfterInteractions(() => {
                   Report.subscribeToReportTypingEvents(reportID);
                   didSubscribeToReportTypingEvents.current = true;
               });
               return () => {
                   interactionTask.cancel();
               };
           }
       } else {
           const topmostReportId = Navigation.getTopmostReportId();

           if (topmostReportId !== report.reportID && didSubscribeToReportTypingEvents.current) {
               didSubscribeToReportTypingEvents.current = false;
               InteractionManager.runAfterInteractions(() => {
                   Report.unsubscribeFromReportChannel(report.reportID);
               });
           }
       }
   }, [isFocused, report.reportID, report.pendingFields, didSubscribeToReportTypingEvents, route, reportID, prevIsFocused]);

Then we can remove the existing unsubscription logic here

What alternative solutions did you explore? (Optional)

NA

@jjcoffee
Copy link
Contributor

I like the sound of @tienifr's proposal! The RCA looks to be correct, since we already unsubscribe on unmount (which is why it only sometimes sends the isTyping events). The solution also seems decent from a quick test run, e.g. navigating to the settings page correctly unsubscribes, whereas opening the room details modal does not unsubscribe (so you'd still see the ... is typing messages in the background).

@ShridharGoel's proposal is lacking an RCA and generally in details, for example the solution refers to a non-existent method unsubscribeFromReportTypingEvents and details of where code changes are being proposed to be made are missing. I recommend working on fleshing out your proposals with plenty of detail for the future. Best of luck!

🎀👀🎀 C+ reviewed

Copy link

melvin-bot bot commented Mar 29, 2024

Triggered auto assignment to @Beamanator, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

@tienifr
Copy link
Contributor

tienifr commented Apr 1, 2024

@jjcoffee PR #39347 is ready to review

@jjcoffee
Copy link
Contributor

jjcoffee commented Apr 1, 2024

There are a couple of extra cases I've found that we need to handle on the PR. Waiting on @tienifr to look into it 🙏

@quinthar
Copy link
Contributor Author

quinthar commented Apr 2, 2024

@jjcoffee do you mean taht @tienifr should re-review? @tienifr can you please do this today, or what's your ETA?

@tienifr
Copy link
Contributor

tienifr commented Apr 3, 2024

@quinthar There is a case we need the confirmation from internal team. Asked here

@mvtglobally
Copy link

Issue not reproducible during KI retests. (First week)

@quinthar
Copy link
Contributor Author

quinthar commented Apr 4, 2024

@tienifr - I see @Beamanator responded. What are the next steps, and your ETA?

@tienifr
Copy link
Contributor

tienifr commented Apr 4, 2024

@quinthar I updated the PR (See comment).

@tienifr
Copy link
Contributor

tienifr commented Apr 4, 2024

It is delayed because I need time to check if the new changes work and do not cause errors.

@quinthar
Copy link
Contributor Author

What's the update here?

@quinthar
Copy link
Contributor Author

oh I see it's merged!

@Beamanator
Copy link
Contributor

Yes sorry for not updating here! Was merged 3 days ago, on staging as of 7 hours ago 🙏 🚀

@quinthar
Copy link
Contributor Author

Can we close this?

@Beamanator
Copy link
Contributor

Hmm this has been in prod for only 3 days now, so i don't believe we should close - we normally wait 7 days before payment & before considering the PR having caused no regressions

@quinthar
Copy link
Contributor Author

Should this be tagged Awaiting Payment then?

@Beamanator Beamanator added Awaiting Payment Auto-added when associated PR is deployed to production and removed Reviewing Has a PR in review labels Apr 29, 2024
@Beamanator Beamanator changed the title HIGH: [Performance] [$500] Reliably unsubscribe from isTyping events when leaving rooms [Awaiting Payment April 29th] HIGH: [Performance] [$500] Reliably unsubscribe from isTyping events when leaving rooms Apr 29, 2024
@Beamanator
Copy link
Contributor

Aah true, I think that automation wasn't working one day last week, so I manually updated 👍

@dylanexpensify FYI looks like payment is due today! 😅

@dylanexpensify
Copy link
Contributor

Wahooo! Will do it!

@dylanexpensify
Copy link
Contributor

Payment summary:

Please apply/request!

@dylanexpensify
Copy link
Contributor

done! @jjcoffee can we get checklist sorted?

@jjcoffee
Copy link
Contributor

  • The PR that introduced the bug has been identified. Link to the PR: N/A - this behaviour was always present
  • The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment: N/A
  • A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion: N/A
  • Determine if we should create a regression test for this bug. Yes
  • If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.

Regression Test Proposal

  1. Device A: Sign in with user A and open report with user B
  2. Device B: Sign in with user B and open report with user A
  3. Device A: Type anything in composer
  4. Device B: Verify that there is a text "User A is typing..." appears below the composer for a while
  5. Device B: Open another report
  6. Device A: Type anything in composer
  7. Device B: Open developer mode, choose "Console" tab. Verify that there is no reportUserIsTyping... log in console, e.g. [info] [Onyx] merge() called for key: reportUserIsTyping_4555629044294735 properties: 15225462 - ""

Do we agree 👍 or 👎

@melvin-bot melvin-bot bot added Daily KSv2 Overdue and removed Weekly KSv2 labels May 5, 2024
Copy link

melvin-bot bot commented May 8, 2024

@Beamanator, @jjcoffee, @dylanexpensify, @tienifr Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

@dylanexpensify
Copy link
Contributor

nice!

@melvin-bot melvin-bot bot removed the Overdue label May 8, 2024
@dylanexpensify
Copy link
Contributor

done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 Engineering External Added to denote the issue can be worked on by a contributor Improvement Item broken or needs improvement. Task
Projects
No open projects
Development

No branches or pull requests

8 participants