Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(ai-research): stop leaking an async work per neon task (#110220)

Robbied384eedc2d81
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/native-anonymize-task.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/native-anonymize-task.test.ts
new file mode 100644
index 000000000000..cd0bba13e366
--- /dev/null
+++ b/nodejs/src/ingestion/pipelines/sessionreplay/ml-mirror/native-anonymize-task.test.ts
@@ -0,0 +1,68 @@
+import { AsyncLocalStorage } from 'node:async_hooks'
+import { setFlagsFromString } from 'node:v8'
+import { runInNewContext } from 'node:vm'
+
+import { logger } from '~/common/utils/logger'
+
+let rustAddon: typeof import('@posthog/replay-anonymizer') | null = null
+try {
+ rustAddon = require('@posthog/replay-anonymizer')
+} catch (e) {
+ if (process.env.CI) {
+ throw new Error(`replay-anonymizer addon failed to load; the native task test cannot run in CI: ${String(e)}`)
+ }
+ logger.warn('🙈', 'replay_anonymizer_addon_not_built_skipping_native_task_test')
+}
+
+const describeAddon = rustAddon ? describe : describe.skip
+describeAddon('native anonymize task', () => {
+ setFlagsFromString('--expose-gc')
+ const gc = runInNewContext('gc') as () => void
+ const nextMacrotask = (): Promise<void> => new Promise((resolve) => setImmediate(resolve))
+
+ function payload(): Buffer {
+ const inner = JSON.stringify({
+ event: '$snapshot_items',
+ properties: {
+ $snapshot_items: [
+ {
+ type: 2,
+ timestamp: 1_700_000_000_000,
+ data: { node: { type: 0, childNodes: [] }, initialOffset: { top: 0, left: 0 } },
+ },
+ ],
+ $session_id: 's-1',
+ $window_id: 'w',
+ },
+ })
+ return Buffer.from(JSON.stringify({ distinct_id: 'd-1', data: inner }))
+ }
+
+ const storage = new AsyncLocalStorage<{ id: number }>()
+
+ // A store created in the test body would stay reachable from the suspended test function itself.
+ async function anonymizeWithinStore(id: number, stores: WeakRef<{ id: number }>[]): Promise<void> {
+ const store = { id }
+ stores.push(new WeakRef(store))
+ const result = await storage.run(store, () => rustAddon!.anonymizeKafkaPayload(payload()))
+ expect(result.failed).toBe(false)
+ }
+
+ it('lets the async context of a finished call be collected', async () => {
+ // Only a release build of the addon can fail this, because Neon deletes the async work inside
+ // a debug assertion. CI builds the addon in release mode.
+ rustAddon!.initAnonymizer({ text: [], url: [] })
+ // The first call of a process keeps its context once, so it runs outside the measured stores.
+ await rustAddon!.anonymizeKafkaPayload(payload())
+ const stores: WeakRef<{ id: number }>[] = []
+ for (let id = 0; id < 20; id++) {
+ await anonymizeWithinStore(id, stores)
+ }
+
+ await nextMacrotask()
+ gc()
+ await nextMacrotask()
+
+ expect(stores.map((store) => store.deref()?.id).filter((id) => id !== undefined)).toEqual([])
+ })
+})
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index d5c71ec9bdef..a30b97a47bf1 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -117,6 +117,12 @@ js-source-scopes = { git = "https://github.com/getsentry/js-source-scopes", rev
[profile.release]
debug = "line-tables-only"
+# Neon 1.1 deletes each napi async work inside `debug_assert_eq!` (src/sys/async_work.rs), so a
+# release build without debug assertions leaks every `cx.task()`, with the two V8 global handles
+# that hold its resource object and async context frame.
+[profile.release.package.neon]
+debug-assertions = true
+
# The stateright model checker exhaustively explores millions of protocol
# states; its hot loop (state hashing, BTreeMap clones) is ~11x slower
# unoptimized, which would put the exhaustive tests at ~25 minutes in