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.

chore(cdp): check hog-exec yielding by timer ticks, not wall-clock gaps (#122202)

Paweł Ledwoń1e573e8e1689
diff --git a/nodejs/src/cdp/utils/hog-exec.test.ts b/nodejs/src/cdp/utils/hog-exec.test.ts
index e899661ca556..ceec12abc88a 100644
--- a/nodejs/src/cdp/utils/hog-exec.test.ts
+++ b/nodejs/src/cdp/utils/hog-exec.test.ts
@@ -7,9 +7,7 @@ describe('hog-exec', () => {
describe('thread relief', () => {
jest.setTimeout(10000)
let interval: NodeJS.Timeout
-
- let lastCheck = Date.now()
- let longestDelay = 0
+ let timerTicks = 0
const blockTime = 100
let originalThresholdMs: number
@@ -21,9 +19,7 @@ describe('hog-exec', () => {
configureEventLoopYield(blockTime)
interval = setInterval(() => {
- // Sets up an interval loop so we can see how long the longest delay between ticks is
- longestDelay = Math.max(longestDelay, Date.now() - lastCheck)
- lastCheck = Date.now()
+ timerTicks++
}, 0)
})
@@ -46,13 +42,16 @@ describe('hog-exec', () => {
`)
const numberToTest = 10
+ const timerTicksAtStart: number[] = []
const results = await Promise.all(
- Array.from({ length: numberToTest }, () =>
+ Array.from({ length: numberToTest }, (_, index) =>
execHog(evilFunctionCode, {
timeout: blockTime,
functions: {
- print: () => {},
+ print: () => {
+ timerTicksAtStart[index] ??= timerTicks
+ },
},
})
)
@@ -64,11 +63,9 @@ describe('hog-exec', () => {
// Timings is semi random so we can't test for exact values
expect(total).toBeGreaterThan(100 * numberToTest)
expect(total).toBeLessThan(200 * numberToTest) // the hog exec limiter isn't exact
- await new Promise((resolve) => setTimeout(resolve, 1))
- // Rough upper bound: with the semaphore serializing calls, the
- // event loop should not be starved for more than ~2.5× one block.
- // (If yielding were broken, this would be ~10× the block.)
- expect(longestDelay).toBeLessThan(blockTime * 2.5)
+ // A timer callback runs between every two executions. Without yielding, all executions
+ // run in one macrotask and see the same tick count.
+ expect(new Set(timerTicksAtStart).size).toBe(numberToTest)
})
})
})