From 8db2db0ab1e4cf56e91ce5e739e3497b0eb2b580 Mon Sep 17 00:00:00 2001 From: Sergey Tatarintsev Date: Tue, 6 Dec 2022 18:15:57 +0100 Subject: [PATCH] ci: Fix a couple of memory leaks in test setups This fixes 2 memory leaks in our test setup: 1. Jest passes every file through transform and caches the results on disk and in memory. That included every generated client as well, so they were kept in memory from the time they were first loaded until process finishes, even though they are needed only for 1 test suite. Fixed by using `transformIgnorePatterns` options in jest config. 2. `debug` library keeps a list of last 100 logs that would be used for panic error messages later. If error object gets into this list, it will keep whole jest sandbox alive until process finishes. Fixed by clearing logs on `$disconnect` Fix #16594 --- .../client/src/runtime/getPrismaClient.ts | 3 ++- .../client/tests/functional/jest.config.js | 19 ++++++++++++++++++- packages/debug/src/index.ts | 4 ++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/client/src/runtime/getPrismaClient.ts b/packages/client/src/runtime/getPrismaClient.ts index eddc9b8b36b5..5257012a3fcc 100644 --- a/packages/client/src/runtime/getPrismaClient.ts +++ b/packages/client/src/runtime/getPrismaClient.ts @@ -1,5 +1,5 @@ import { Context, context } from '@opentelemetry/api' -import Debug from '@prisma/debug' +import Debug, { clearLogs } from '@prisma/debug' import { BatchTransactionOptions, BinaryEngine, @@ -591,6 +591,7 @@ export function getPrismaClient(config: GetPrismaClientConfig) { e.clientVersion = this._clientVersion throw e } finally { + clearLogs() if (!this._dataProxy) { this._dmmf = undefined } diff --git a/packages/client/tests/functional/jest.config.js b/packages/client/tests/functional/jest.config.js index 11f3bdc5fd9a..64eb154a7b41 100644 --- a/packages/client/tests/functional/jest.config.js +++ b/packages/client/tests/functional/jest.config.js @@ -1,10 +1,18 @@ 'use strict' const os = require('os') +const path = require('path') + +const runtimeDir = path.dirname(require.resolve('../../runtime')) +const packagesDir = path.resolve('..', '..', '..') module.exports = () => { const configCommon = { testMatch: ['**/*.ts', '!(**/*.d.ts)', '!(**/_utils/**)', '!(**/_*.ts)', '!(**/.generated/**)'], - transformIgnorePatterns: [], + transformIgnorePatterns: [ + '[\\/]node_modules[\\/]', + escapeRegex(runtimeDir), + `${escapeRegex(packagesDir)}[\\/][^\\/]+[\\/]dist[\\/]`, + ], reporters: [ 'default', [ @@ -47,3 +55,12 @@ module.exports = () => { }, } } + +/** + * https://stackoverflow.com/a/6969486 + * @param {string} str + * @returns {string} + */ +function escapeRegex(str) { + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') +} diff --git a/packages/debug/src/index.ts b/packages/debug/src/index.ts index 1b02e99b1154..4b5137c12b1d 100644 --- a/packages/debug/src/index.ts +++ b/packages/debug/src/index.ts @@ -81,5 +81,9 @@ export function getLogs(numChars = 7500): string { return output.slice(-numChars) } +export function clearLogs() { + debugArgsHistory.length = 0 +} + export { Debug } export default Debug