: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(cdp): fall back to the node vm when the rust vm ffi boundary throws (#70077)
José Sequeira7f080213252d
diff --git a/nodejs/src/cdp/hog-transformations/rust-vm-executor.test.ts b/nodejs/src/cdp/hog-transformations/rust-vm-executor.test.tsindex af9c912f84e5..f4eb4dbd7dc5 100644--- a/nodejs/src/cdp/hog-transformations/rust-vm-executor.test.ts+++ b/nodejs/src/cdp/hog-transformations/rust-vm-executor.test.ts@@ -1,3 +1,5 @@+import { logger } from '~/common/utils/logger'+import { createExampleInvocation } from '../_tests/fixtures'import { resetHogvmNodeModuleCacheForTests } from './rust-vm'import { RustVmExecutor } from './rust-vm-executor'@@ -104,6 +106,26 @@ describe('RustVmExecutor', () => {await expect(executor.execute(createExampleInvocation(), [])).resolves.toBeNull()})+ it('falls back to the node vm when the ffi boundary throws instead of returning an error', async () => {+ // e.g. globals containing NaN/Infinity, which serde_json can't represent.+ mockHogvmNode.executeBatch.mockRejectedValue(new Error('Failed to convert js number to serde_json::Number'))++ await expect(executor.execute(createExampleInvocation(), [])).resolves.toBeNull()+ })++ it('redacts sensitive values from fallback logs', async () => {+ // Marshalling errors and panic messages can embed values from the invocation globals.+ const warnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {})+ mockHogvmNode.executeBatch.mockRejectedValue(new Error('failed to convert value "secret-token" at inputs'))++ await expect(executor.execute(createExampleInvocation(), ['secret-token'])).resolves.toBeNull()++ const fallbackCalls = warnSpy.mock.calls.filter((call) => String(call[1]).includes('fell back'))+ expect(fallbackCalls).toHaveLength(1)+ expect(JSON.stringify(fallbackCalls)).not.toContain('secret-token')+ expect(JSON.stringify(fallbackCalls)).toContain('***REDACTED***')+ })+it('falls back to the node vm when the native addon is unavailable', async () => {mockHogvmNode.init.mockImplementation(() => {throw new Error('addon not built')diff --git a/nodejs/src/cdp/hog-transformations/rust-vm-executor.ts b/nodejs/src/cdp/hog-transformations/rust-vm-executor.tsindex ce802f79fdd3..27aa05052b21 100644--- a/nodejs/src/cdp/hog-transformations/rust-vm-executor.ts+++ b/nodejs/src/cdp/hog-transformations/rust-vm-executor.ts@@ -1,6 +1,8 @@import { DateTime } from 'luxon'import { Counter, Histogram } from 'prom-client'+import { logger } from '~/common/utils/logger'+import { CyclotronJobInvocationHogFunction, CyclotronJobInvocationResult } from '../types'import { createAddLogFunction, sanitizeLogMessage } from '../utils'import { createInvocationResult } from '../utils/invocation-utils'@@ -33,6 +35,28 @@ export class RustVmExecutor {return loadHogvmNodeModule({ mmdbPath: this.options.mmdbPath })}+ /**+ * Count and log a fallback so every node-vm handoff is attributable to a function. Returns+ * null for the caller to pass through.+ */+ private fallback(+ outcome: 'fallback_unsupported' | 'fallback_exception' | 'fallback_empty_result',+ invocation: CyclotronJobInvocationHogFunction,+ sensitiveValues: string[],+ error: unknown+ ): null {+ rustVmExecution.inc({ outcome })+ logger.warn('🦀', 'Rust HogVM invocation fell back to the node vm', {+ outcome,+ functionId: invocation.functionId,+ teamId: invocation.teamId,+ // Same redaction as hog print logs: marshalling errors and panic messages can embed+ // values from the invocation globals, which include secret inputs.+ error: error !== undefined ? sanitizeLogMessage([String(error)], sensitiveValues) : undefined,+ })+ return null+ }+/*** Execute one transformation invocation on the Rust VM. Returns null when the Node VM must* run it instead.@@ -49,21 +73,32 @@ export class RustVmExecutor {): Promise<CyclotronJobInvocationResult<CyclotronJobInvocationHogFunction> | null> {const module_ = this.getModule()if (!module_) {+ // No per-invocation log: a missing addon affects every invocation and the loader+ // already warned once with the load error.rustVmExecution.inc({ outcome: 'fallback_unavailable' })return null}- const [rust] = await module_.executeBatch(invocation.hogFunction.bytecode, [invocation.state.globals], {- maxSteps: RUST_MAX_STEPS,- })+ let rust+ try {+ ;[rust] = await module_.executeBatch(invocation.hogFunction.bytecode, [invocation.state.globals], {+ maxSteps: RUST_MAX_STEPS,+ })+ } catch (error) {+ // A throw here is the boundary or the native side, not the program's own error path —+ // marshalling failures (e.g. globals containing NaN or Infinity, which serde_json+ // can't represent), rust panics, addon bugs. Deliberately broad: the node vm can run+ // all of these, so correctness wins and the invocation falls back — while the warn log+ // and the fallback_exception outcome carry the error so native faults stay visible+ // rather than being silently healed.+ return this.fallback('fallback_exception', invocation, sensitiveValues, error)+ }if (!rust) {- rustVmExecution.inc({ outcome: 'fallback_unavailable' })- return null+ return this.fallback('fallback_empty_result', invocation, sensitiveValues, undefined)}if (rust.error && isUnsupportedByRustVm(rust.error)) {- rustVmExecution.inc({ outcome: 'fallback_unsupported' })- return null+ return this.fallback('fallback_unsupported', invocation, sensitiveValues, rust.error)}const durationMs = rust.durationUs / 1000