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

fix(twap): fix infinite loop in orders hook #3348

Merged
merged 1 commit into from
Nov 7, 2023
Merged

Conversation

shoom3301
Copy link
Collaborator

@shoom3301 shoom3301 commented Nov 7, 2023

Summary

While testing I noticed that my M2 Max processor sometimes freezes.
By stopping the main thread periodically I noticed that I almost always get into CreatedInOrderBookOrdersUpdater.
After adding a console.log here I saw that it gets tons of recalculations for partOrdersFromProd.
After some bisection on useAsyncMemo deps I got into useSWRProdOrders and here I found the cause of the problem.
So, the main problem in the fact that we always return a new array and it makes useAsyncMemo() recalculate everything many times.

tttt.mov

To Test

  1. I just add a console.log like:
  const partOrdersFromProd = useAsyncMemo(
    async () => {
      console.log('TRIGGER')
...

Before the fix I got tons of console logs. After only few

@shoom3301 shoom3301 added the Bug Something isn't working label Nov 7, 2023
@shoom3301 shoom3301 requested a review from a team November 7, 2023 12:01
@shoom3301 shoom3301 self-assigned this Nov 7, 2023
Copy link

vercel bot commented Nov 7, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
cosmos ✅ Ready (Inspect) Visit Preview Nov 7, 2023 0:01am
swap-dev ✅ Ready (Inspect) Visit Preview Nov 7, 2023 0:01am
widget-configurator ✅ Ready (Inspect) Visit Preview Nov 7, 2023 0:01am

// Fetch PROD orders only when current env is not prod
// We need them for TWAP, because WatchTower creates orders only on Prod
export function useSWRProdOrders(): EnrichedOrder[] {
const { chainId } = useWalletInfo()
const requestParams = useSWROrdersRequest()
const apiOrders = useApiOrders()

const { data: loadedProdOrders = [] } = useSWR<EnrichedOrder[]>(
const { data: loadedProdOrders = EMPTY_ORDERS } = useSWR<EnrichedOrder[]>(
Copy link
Contributor

Choose a reason for hiding this comment

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

ok, great, we return always the same empty object, so in case there's no orders, the depending components and hooks won't re-render cause is the same identity (i assume the issue was the fact [] !== [])

My only concern is that this might be covering up the real place where the issue originated

Isn't the issue that we are re-rendering somewhere everything even if the loader orders doesn't change?

Copy link
Collaborator Author

@shoom3301 shoom3301 Nov 7, 2023

Choose a reason for hiding this comment

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

i assume the issue was the fact [] !== []

Exactly

Isn't the issue that we are re-rendering somewhere everything even if the loader orders doesn't change?

I've revisited all hook deps and found problem only here

@shoom3301 shoom3301 merged commit 097728c into release/1.49.0 Nov 7, 2023
10 of 11 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2023
@alfetopito alfetopito deleted the fix/twap-loop branch November 8, 2023 13:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants