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

Detect infinite-loop style situations and error out #533

Closed
2 tasks
benjackwhite opened this issue Feb 10, 2023 · 4 comments
Closed
2 tasks

Detect infinite-loop style situations and error out #533

benjackwhite opened this issue Feb 10, 2023 · 4 comments

Comments

@benjackwhite
Copy link
Collaborator

Consider the following:

const MyComponent = (): JSX.Element => {
    const [count, setCount] = useState(0)

    useEffect(() => {
        requestAnimationFrame(() => {
            setCount(count + 1)
        })
    }, [count])

    posthog.capture('MyComponent loaded', { leet: 'coding' })

    return (
        <div>
            <p>Hey there</p>
        </div>
    )
}

If someone would forget to wrap the capture call in a useEffect (or similar situation) then they would end up tracking 1000s of events in a short period of time. We can deal with this on the backend but it would be much better if we could simply detect this in the frontend and immediately stop the ingestion, logging a warning about why this has been stopped (and potentially the stack trace) so that a dev could investigate further.

Things to consider:

  • There may be some cases where this is wanted (rapid events in succession) so we should have a somewhat high threshold (100 events maybe) to allow these burst situations to occur
  • We might want to track a special event $client_rate_limited so that this is visible in PostHog to the user otherwise they might just remain confused why they see no events coming in (also good for us to be able to track this happening and pro-actively reach out to the customer)
@ellie
Copy link

ellie commented Feb 10, 2023

We also need to consider when people do not correctly specify the useEffect, and have it run way more often than it should

@benjackwhite
Copy link
Collaborator Author

I'm thinking of this more abstractly from React as a simple rate limiter on the underlying capture code. Went for the React example because I figured that is an actual example that is not unreasonable for even a senior engineer to accidentally do 😅

@lharries
Copy link
Contributor

lharries commented Feb 10, 2023

For reference, the way I managed to trigger the problem was as follows:

const MyComponent = (): JSX.Element => {
    const [count, setCount] = useState(0)

    useEffect(() => {
        posthog.capture('send message')
    })

    return (
        <div>
            <p>Hey there</p>
        </div>
    )
}

So without the [] in the useEffect

@pauldambra
Copy link
Member

i'm thinking fixed in #1051

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

No branches or pull requests

4 participants