From ccccfdd5bb09d4cf32325ab3f150bcbbed9a251e Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sun, 29 Sep 2024 11:10:07 +0100 Subject: [PATCH 1/2] fix(node): Local variables handle error --- packages/node/src/integrations/local-variables/worker.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/node/src/integrations/local-variables/worker.ts b/packages/node/src/integrations/local-variables/worker.ts index 77299c0aff29..c62e15784434 100644 --- a/packages/node/src/integrations/local-variables/worker.ts +++ b/packages/node/src/integrations/local-variables/worker.ts @@ -155,7 +155,11 @@ async function startDebugger(): Promise { // The object must be released after the debugger has resumed or we get a memory leak. // For node v20, setImmediate is enough here but for v22 a longer delay is required setTimeout(async () => { - await session.post('Runtime.releaseObject', { objectId }); + try { + await session.post('Runtime.releaseObject', { objectId }); + } catch (_) { + // + } }, 1_000); } }, From 9418b6816899feca06bef7537dff14a47eaaaec3 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 30 Sep 2024 13:15:59 +0200 Subject: [PATCH 2/2] Change logic and remove memory leak test --- .../local-variables-memory-test.js | 48 ------------------- .../suites/public-api/LocalVariables/test.ts | 26 ---------- .../integrations/local-variables/worker.ts | 18 ++----- 3 files changed, 3 insertions(+), 89 deletions(-) delete mode 100644 dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js diff --git a/dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js b/dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js deleted file mode 100644 index 35c71a3fc6e8..000000000000 --- a/dev-packages/node-integration-tests/suites/public-api/LocalVariables/local-variables-memory-test.js +++ /dev/null @@ -1,48 +0,0 @@ -/* eslint-disable no-unused-vars */ -const Sentry = require('@sentry/node'); -const { loggingTransport } = require('@sentry-internal/node-integration-tests'); - -Sentry.init({ - dsn: 'https://public@dsn.ingest.sentry.io/1337', - includeLocalVariables: true, - transport: loggingTransport, - // Stop the rate limiting from kicking in - integrations: [Sentry.localVariablesIntegration({ maxExceptionsPerSecond: 10000000 })], -}); - -class Some { - two(name) { - throw new Error('Enough!'); - } -} - -function one(name) { - const arr = [1, '2', null]; - const obj = { - name, - num: 5, - }; - const bool = false; - const num = 0; - const str = ''; - const something = undefined; - const somethingElse = null; - - const ty = new Some(); - - ty.two(name); -} - -// Every millisecond cause a caught exception -setInterval(() => { - try { - one('some name'); - } catch (e) { - // - } -}, 1); - -// Every second send a memory usage update to parent process -setInterval(() => { - process.send({ memUsage: process.memoryUsage() }); -}, 1000); diff --git a/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts b/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts index bf01ed999708..779b341d9f40 100644 --- a/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts +++ b/dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts @@ -1,4 +1,3 @@ -import * as childProcess from 'child_process'; import * as path from 'path'; import { conditionalTest } from '../../../utils'; import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; @@ -89,29 +88,4 @@ conditionalTest({ min: 18 })('LocalVariables integration', () => { test('Includes local variables for caught exceptions when enabled', done => { createRunner(__dirname, 'local-variables-caught.js').expect({ event: EXPECTED_LOCAL_VARIABLES_EVENT }).start(done); }); - - test('Should not leak memory', done => { - const testScriptPath = path.resolve(__dirname, 'local-variables-memory-test.js'); - - const child = childProcess.spawn('node', [testScriptPath], { - stdio: ['inherit', 'inherit', 'inherit', 'ipc'], - }); - - let reportedCount = 0; - - child.on('message', msg => { - reportedCount++; - const rssMb = (msg as { memUsage: { rss: number } }).memUsage.rss / 1024 / 1024; - // We shouldn't use more than 135MB of memory - expect(rssMb).toBeLessThan(135); - }); - - // Wait for 20 seconds - setTimeout(() => { - // Ensure we've had memory usage reported at least 15 times - expect(reportedCount).toBeGreaterThan(15); - child.kill(); - done(); - }, 20000); - }); }); diff --git a/packages/node/src/integrations/local-variables/worker.ts b/packages/node/src/integrations/local-variables/worker.ts index c62e15784434..91fb5957cb01 100644 --- a/packages/node/src/integrations/local-variables/worker.ts +++ b/packages/node/src/integrations/local-variables/worker.ts @@ -86,7 +86,7 @@ let rateLimiter: RateLimitIncrement | undefined; async function handlePaused( session: Session, { reason, data: { objectId }, callFrames }: PausedExceptionEvent, -): Promise { +): Promise { if (reason !== 'exception' && reason !== 'promiseRejection') { return; } @@ -126,7 +126,7 @@ async function handlePaused( objectId, }); - return objectId; + await session.post('Runtime.releaseObject', { objectId }); } async function startDebugger(): Promise { @@ -145,23 +145,11 @@ async function startDebugger(): Promise { isPaused = true; handlePaused(session, event.params as PausedExceptionEvent).then( - async objectId => { + async () => { // After the pause work is complete, resume execution! if (isPaused) { await session.post('Debugger.resume'); } - - if (objectId) { - // The object must be released after the debugger has resumed or we get a memory leak. - // For node v20, setImmediate is enough here but for v22 a longer delay is required - setTimeout(async () => { - try { - await session.post('Runtime.releaseObject', { objectId }); - } catch (_) { - // - } - }, 1_000); - } }, async _ => { if (isPaused) {