Skip to content

Fix app hang when sending long comments - #8691

Merged
luacmartins merged 7 commits into
mainfrom
cmartins-longComments
Apr 25, 2022
Merged

luacmartins merged 7 commits into
mainfrom
cmartins-longComments

Conversation

@luacmartins

@luacmartins luacmartins commented Apr 19, 2022

Copy link
Copy Markdown
Contributor

Details

  1. Disables MD to HTML parsing for comments longer than 10k chars.
  2. Displays an error and prevents the user to submit messages longer than 60k chars.

cc @Julesssss

Fixed Issues

$ https://github.com/Expensify/Expensify/issues/205451

Tests

  1. Open the app in accounts A and B
  2. From account A, try to send a message longer than 60k characters to account B.
  3. Verify that:
  • The compose box border turns red
  • A character counter is displayed at the bottom right of the box
  • The send button is disabled
  • Pressing enter does not submit the message
  1. While you see the error in the compose box, start typing on account B and verify that you see the User is typing message and the UI is not broken.
  2. Disconnect from the internet and verify that you see the You appear to be offline message and the UI is not broken.
  • Verify that no errors appear in the JS console

PR Review Checklist

Contributor (PR Author) Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there’s a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained “why” the code was doing something instead of only explaining “what” the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named “index.js”. All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • Any functional components have the displayName property
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose and it is
  • If a new CSS style is added I verified that:
    • A similar style doesn’t already exist
    • The style can’t be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.

PR Reviewer Checklist

  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there’s a console error not related to the PR, report it or open an issue for it to be fixed)
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained “why” the code was doing something instead of only explaining “what” the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named “index.js”. All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • Any functional components have the displayName property
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn’t already exist
    • The style can’t be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.

QA Steps

Same steps from the Tests section.

  • Verify that no errors appear in the JS console

Screenshots

Web

web.mov

Mobile Web

mweb.mov

Desktop

desktop.mov

iOS

ios.mov

Android

android.mov

@luacmartins luacmartins self-assigned this Apr 19, 2022
@luacmartins
luacmartins force-pushed the cmartins-longComments branch from fe65104 to f4fca97 Compare April 20, 2022 20:46
@luacmartins
luacmartins marked this pull request as ready for review April 20, 2022 21:48
@luacmartins
luacmartins requested review from a team and marcaaron as code owners April 20, 2022 21:48
@luacmartins
luacmartins removed the request for review from marcaaron April 20, 2022 21:48
@melvin-bot
melvin-bot Bot requested review from Julesssss and removed request for a team April 20, 2022 21:48

@marcaaron marcaaron left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leaving a few thoughts.

// For longer comments, skip parsing and display plaintext for performance reasons. It takes over 40s to parse a 100k long string!!
const parser = new ExpensiMark();
const commentText = parser.replace(text);
const commentText = text.length < 10000 ? parser.replace(text) : text;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Does this mean we will not render markdown for these?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10000 characters feels kind of magic. How did we land on this? Also what about a solution that accounts for the restrictions in Auth and this issue? e.g. could we chunk this into a few different messages so that it stays under the limit?

@luacmartins luacmartins Apr 20, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Does this mean we will not render markdown for these?

Correct!

10000 characters feels kind of magic. How did we land on this? Also what about a solution that accounts for the restrictions in Auth and this issue? e.g. could we chunk this into a few different messages so that it stays under the limit?

10k came from the timing in my tests here, i.e. 10k took 600ms to parse vs 20k took 2.2s.

I tried chunking, i.e. calling addAction multiple times with chunks of 1k, 5k, 10k and 20k chars, but that didn't seem to have any effect on the performance. I think that the regex parsing is at least a O(n) operation, so breaking it down into chunks wouldn't matter when parsing n or m*n/m chars. Having said that, maybe I'm missing something in your suggestion?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we perform those tests on a development or production build?
Should we expect this work to be relatively consistent from one machine to the next?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we perform those tests on a development or production build?

That was on a development build.

Should we expect this work to be relatively consistent from one machine to the next?

The amount of work (number of operations) should be the same across machines, but the timing will vary. A prod build running on a mobile device would probably be the best benchmark to use, although I think it's more common for users to send large messages over web/desktop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we put in a large markdown comment it will work as text, but not get converted. That seems kind of broken, but also unsure if it's worth optimizing right now.

Having said that, maybe I'm missing something in your suggestion?

I see. So chunking didn't improve the performance? Where is the performance bottleneck exactly - in the JS thread? If we are having an issue with the performance of our markdown parser the first thoughts that come to mind are...

  • Can we make the parser more efficient?
  • If not, can we move the work the parser has to do onto a different thread (assuming our issue is that it's blocking the main JS)?

Separately, I took a look at the original issue which mentions stuff about how someone could "crash" another client by sending a message and unclear how it is related to parsing when we are adding a comment or how this change will fix that.

Anyways, just curious about this, thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we put in a large markdown comment it will work as text, but not get converted. That seems kind of broken, but also unsure if it's worth optimizing right now.

Agreed. With the current solution, we could limit the number of characters to 10k and not disable markdown.

So chunking didn't improve the performance?

I didn't see any improvements in performance.

Where is the performance bottleneck exactly - in the JS thread?

Correct!

Can we make the parser more efficient?

Probably and I considered it, but the more I looked into it the more of a rabbit hole it became 😅 specially considering this is a bit of an edge case. We could look at some external parsing library (assuming that it would be faster than ours).

If not, can we move the work the parser has to do onto a different thread (assuming our issue is that it's blocking the main JS)?

This looks like a possibility. It would stop blocking the JS thread, but wouldn't it still take a long time to parse the message?

Separately, I took a look at the original issue which mentions stuff about how someone could "crash" another client by sending a message and unclear how it is related to parsing when we are adding a comment or how this change will fix that.

Ah yea, but that assumed that sending such a large message to a user would succeed. Auth actually returns a 402 Maximum size exceeded error to that API call.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a possibility. It would stop blocking the JS thread, but wouldn't it still take a long time to parse the message?

Yeah I'm not sure if this is a great idea or even possible, but at a high level it seems the user could at least do other things while the parsing happens.

Ah yea, but that assumed that sending such a large message to a user would succeed.

2022-04-20_14-54-08

Ok got it. So that's not our problem then. And we have an issue with the markdown parser performance. Thanks!

Not necessarily against the idea to prevent large comments if there is no way to support them, but the parser will still be slow if we do this - so wondering if we need to look to improve the performance of the parsing in general or what exactly is happening there. I think it's not the first time I heard it was slow, but can't remember the exact context.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I'm gonna look into improving the performance next 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I created an issue to investigate our parser performance and the slow regex test - https://github.com/Expensify/Expensify/issues/208027.

Comment thread src/pages/home/report/ReportActionCompose.js Outdated
@marcaaron
marcaaron requested review from marcaaron and removed request for marcaaron April 21, 2022 01:01

@Julesssss Julesssss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fine solution for now, the main problem is solved (you can crash any user client lol) and we can improve parser performance later on too.

@luacmartins

Copy link
Copy Markdown
Contributor Author

Gonna merge this one and keep investigating the performance on this issue.

@luacmartins
luacmartins merged commit c308f6b into main Apr 25, 2022
@luacmartins
luacmartins deleted the cmartins-longComments branch April 25, 2022 17:03
@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by @luacmartins in version: 1.1.57-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by @chiragsalian in version: 1.1.57-17 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@Gonals

Gonals commented Mar 16, 2023

Copy link
Copy Markdown
Contributor

Heyhey! We forgot to apply this behavior when editing comments! #15307

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants