From b9d793ea04fb8ca02bbd7cb3776cd72e9e25d7b4 Mon Sep 17 00:00:00 2001 From: Liang Gong Date: Fri, 28 Jun 2024 18:12:47 -0700 Subject: [PATCH] chore(core): guard check oversize trace (#121) Summary: OSS ticket: https://github.com/facebook/memlab/issues/121 Reviewed By: twobassdrum Differential Revision: D59178943 fbshipit-source-id: 3ad7870b3cb46d0fa52ff8fdae5c6eb38190aa99 --- .../core/src/logger/LeakTraceDetailsLogger.ts | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/core/src/logger/LeakTraceDetailsLogger.ts b/packages/core/src/logger/LeakTraceDetailsLogger.ts index f5adff9ec..1d4637f54 100644 --- a/packages/core/src/logger/LeakTraceDetailsLogger.ts +++ b/packages/core/src/logger/LeakTraceDetailsLogger.ts @@ -19,6 +19,7 @@ import serializer from '../lib/Serializer'; import utils from '../lib/Utils'; import fs from 'fs'; import path from 'path'; +import info from '../lib/Console'; class LeakTraceDetailsLogger { _wrapPathJSONInLoader(jsonContent: string): string { @@ -41,11 +42,22 @@ class LeakTraceDetailsLogger { ): Nullable { const options = {leakedIdSet, nodeIdsInSnapshots}; const gcTrace = serializer.JSONifyPath(trace, snapshot, options); - const traceJSON = JSON.stringify(gcTrace, null, 2); - const content = this._wrapPathJSONInLoader(traceJSON); - fs.writeFile(filepath, content, 'UTF-8', () => { - // noop - }); + try { + const traceJSON = JSON.stringify(gcTrace, null, 2); + const content = this._wrapPathJSONInLoader(traceJSON); + fs.writeFile(filepath, content, 'UTF-8', () => { + // noop + }); + } catch (ex) { + const error = utils.getError(ex); + if (error.message.includes('Invalid string length')) { + info.warning( + 'Trace details JSON not saved because it exceeded the size limit.', + ); + } else { + utils.haltOrThrow(error); + } + } return gcTrace; }