Skip to content

Commit

Permalink
Merge pull request #1413 from SevInf/feat/future-leak-repro
Browse files Browse the repository at this point in the history
test: Memory leak reproduction for futures
  • Loading branch information
Brooooooklyn committed Dec 28, 2022
2 parents 1e8d20a + b7ba068 commit a131c8a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions memory-testing/index.mjs
Expand Up @@ -5,3 +5,4 @@ await createSuite('tokio-future')
await createSuite('serde')
await createSuite('tsfn')
await createSuite('buffer')
await createSuite('returns-future')
26 changes: 26 additions & 0 deletions memory-testing/returns-future.mjs
@@ -0,0 +1,26 @@
import { createRequire } from 'module'

import { displayMemoryUsageFromNode } from './util.mjs'

const initialMemoryUsage = process.memoryUsage()

const require = createRequire(import.meta.url)

const api = require(`./index.node`)

async function main() {
let i = 1
// eslint-disable-next-line no-constant-condition
while (true) {
await api.returnsFuture()
if (i % 100000 === 0) {
displayMemoryUsageFromNode(initialMemoryUsage)
}
i++
}
}

main().catch((e) => {
console.error(e)
process.exit(1)
})
3 changes: 3 additions & 0 deletions memory-testing/src/lib.rs
Expand Up @@ -171,3 +171,6 @@ pub fn buffer_pass_through(buffer: Buffer) -> Buffer {
pub fn array_buffer_pass_through(array_buffer: Uint8Array) -> Uint8Array {
array_buffer
}

#[napi]
pub async fn returns_future() {}
15 changes: 9 additions & 6 deletions memory-testing/test-util.mjs
Expand Up @@ -44,14 +44,19 @@ export async function createSuite(testFile, maxMemoryUsage) {
const stats = await container.stats()

let shouldAssertMemoryUsage = false

const initialMemoryUsage = await new Promise((resolve, reject) => {
let initialMemoryUsage
await new Promise((resolve, reject) => {
const initialDate = Date.now()
stats.on('data', (d) => {
const { memory_stats } = JSON.parse(d.toString('utf8'))
resolve(memory_stats.usage)
if (Date.now() - initialDate > 10000 && !shouldAssertMemoryUsage) {
resolve()
initialMemoryUsage = memory_stats.usage
shouldAssertMemoryUsage = true
}
if (shouldAssertMemoryUsage && memory_stats?.usage) {
const memoryGrowth = memory_stats.usage - initialMemoryUsage
if (memoryGrowth > maxMemoryUsage ?? initialMemoryUsage) {
if (memoryGrowth > (maxMemoryUsage ?? initialMemoryUsage)) {
console.info(
chalk.redBright(
`Potential memory leak, memory growth: ${prettyBytes(
Expand All @@ -72,8 +77,6 @@ export async function createSuite(testFile, maxMemoryUsage) {

await sleep(60000)

shouldAssertMemoryUsage = true

try {
await container.stop()
await container.remove()
Expand Down

0 comments on commit a131c8a

Please sign in to comment.