Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Fix a couple of memory leaks in test setups #16655

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/client/src/runtime/getPrismaClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, context } from '@opentelemetry/api'
import Debug from '@prisma/debug'
import Debug, { clearLogs } from '@prisma/debug'
import {
BatchTransactionOptions,
BinaryEngine,
Expand Down Expand Up @@ -591,6 +591,11 @@ export function getPrismaClient(config: GetPrismaClientConfig) {
e.clientVersion = this._clientVersion
throw e
} finally {
// Debug module keeps a list of last 100 logs regardless of environment variables.
// This can cause a memory leak. It's especially bad in jest environment where keeping an
// error in this list will prevent jest sandbox from being GCed. Clearing logs on disconnect
// helps to avoid that
clearLogs()
SevInf marked this conversation as resolved.
Show resolved Hide resolved
if (!this._dataProxy) {
this._dmmf = undefined
}
Expand Down
22 changes: 21 additions & 1 deletion packages/client/tests/functional/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
'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: [],
// By default, jest passes every file it loads thorough a transform and caches result both on disk and in memory
// That includes all generated clients as well. So, unless we ignore them, they'd be kept in memory until test process
// is finished, even though they are needed for 1 test only
transformIgnorePatterns: [
SevInf marked this conversation as resolved.
Show resolved Hide resolved
'[\\/]node_modules[\\/]',
escapeRegex(runtimeDir),
`${escapeRegex(packagesDir)}[\\/][^\\/]+[\\/]dist[\\/]`,
],
reporters: [
'default',
[
Expand Down Expand Up @@ -47,3 +58,12 @@ module.exports = () => {
},
}
}

/**
* https://stackoverflow.com/a/6969486
* @param {string} str
* @returns {string}
*/
function escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
4 changes: 4 additions & 0 deletions packages/debug/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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