Skip to content

Commit 45b2a56

Browse files
committed
fix(core): consume response body in _flush() and _capture() to prevent Cloudflare Workers warnings
`_flush()` and the single-event `_capture()` path both call `fetchWithRetry()` but discard the returned response without reading the body. In runtimes like Cloudflare Workers that enforce response body consumption, this causes cross-request promise resolution warnings and may silently cancel post-flush error handling continuations. Consume the response body with `res.text().catch(() => {})` at both call sites so the underlying connection is properly cleaned up. The `fetchWithRetry()` return type is preserved so callers that need the response (e.g. surveys API) can still read it. Fixes #3173
1 parent dc59f94 commit 45b2a56

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

packages/core/src/posthog-core-stateless.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,11 @@ export abstract class PostHogCoreStateless {
964964
}
965965

966966
try {
967-
await this.fetchWithRetry(url, fetchOptions)
967+
const res = await this.fetchWithRetry(url, fetchOptions)
968+
// Consume the response body to prevent resource leaks in runtimes like
969+
// Cloudflare Workers that require the body to be read before the handler completes.
970+
// See: https://github.com/PostHog/posthog-js/issues/3173
971+
await res.text().catch(() => {})
968972
} catch (err) {
969973
this._events.emit('error', err)
970974
}
@@ -1139,7 +1143,11 @@ export abstract class PostHogCoreStateless {
11391143
}
11401144

11411145
try {
1142-
await this.fetchWithRetry(url, fetchOptions, retryOptions)
1146+
const res = await this.fetchWithRetry(url, fetchOptions, retryOptions)
1147+
// Consume the response body to prevent resource leaks in runtimes like
1148+
// Cloudflare Workers that require the body to be read before the handler completes.
1149+
// See: https://github.com/PostHog/posthog-js/issues/3173
1150+
await res.text().catch(() => {})
11431151
} catch (err) {
11441152
if (isPostHogFetchContentTooLargeError(err) && batchMessages.length > 1) {
11451153
// if we get a 413 error, we want to reduce the batch size and try again

0 commit comments

Comments
 (0)