PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
70 stars 36 forks source link

Massive slowdown in using shutdownAsync vs shutdown #129

Closed marcfrankel closed 11 months ago

marcfrankel commented 12 months ago

Bug description

Not sure if this has to do with some issue on the ingestion level or something but recently we noticed that calling await posthog.shutdownAsync() was holding up the execution thread for around 9 seconds! Switching from shutdownAsync to just shutdown seemed to fix the issue.

We're deploying in Next.js using vercel so under the hood these are either edge functions or AWS lambda functions. I think in this case it's an edge function.

How to reproduce

  1. Follow the advice of setting flushAt to 1, flushInterval to 0, and calling shutdownAsync() https://posthog.com/docs/libraries/node#options.
  2. Call shutdownAsync after capturing an event
  3. Experience a 9 second hang time that isn't present on the non-async shutdown version of the call

Related sub-librarie

Additional context

I think this issue might relate to edge functions on vercel. Additionally from digging into the source code, it seems like this is the only difference between the versions in posthog-node.ts:

shutdown(): void {
   void this.shutdownAsync()
}

async shutdownAsync(): Promise<void> {
    this.featureFlagsPoller?.stopPoller()
    return super.shutdownAsync()
}

So maybe it's an issue with feature flags?

Also more than possible this is a user issue and it's something I'm doing wrong. We recently moved to vercel and started noticing this slowing us down...

marcfrankel commented 12 months ago

Update

Using posthog.debug() reveals the problem is:

PostHog Debug error  [PostHogFetchNetworkError: Network error while fetching PostHog] {
  error:  [AxiosError: There is no suitable adapter to dispatch the request since :
- adapter xhr is not supported by the environment
- adapter http is not available in the build] {
  message: 'There is no suitable adapter to dispatch the request since :\n- adapter xhr is not supported by the environment\n- adapter http is not available in the build',
  name: 'AxiosError',
  code: 'ERR_NOT_SUPPORT'
},
  name: 'PostHogFetchNetworkError'
}

So I believe these issues are linked: #127

huw commented 12 months ago

Definitely a duplicate of #127—but I’m commenting to add that yes, shutdown is going to be faster than await shutdownAsync because it’s not awaiting that Promise. But that means your PostHog client won’t tear down properly. Please make sure you’re calling await shutdownAsync once this gets fixed.

benjackwhite commented 11 months ago

This should be fixed in the latest version as of https://github.com/PostHog/posthog-js-lite/pull/132

If not please feel free to re-open.

marandaneto commented 11 months ago

@benjackwhite assuming you forgot to close the issue, closing it!