Skip to content

Commit

Permalink
fix(vm-threads): don't crash on percentage based memoryLimit (#4802)
Browse files Browse the repository at this point in the history
Co-authored-by: Ari Perkkiö <ari.perkkio@gmail.com>
  • Loading branch information
inottn and AriPerkkio committed Jan 1, 2024
1 parent f99fd6c commit 70e8a38
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/vitest/src/node/pools/vm-threads.ts
Expand Up @@ -170,9 +170,10 @@ function getMemoryLimit(config: ResolvedConfig) {
)
}

if (limit && limit > 1)
// If totalmem is not supported we cannot resolve percentage based values like 0.5, "50%"
if ((typeof limit === 'number' && limit > 1) || (typeof limit === 'string' && limit.at(-1) !== '%'))
return stringToBytes(limit)

// just ignore "experimentalVmWorkerMemoryLimit" value because we cannot detect memory limit
// just ignore "memoryLimit" value because we cannot detect memory limit
return null
}
4 changes: 2 additions & 2 deletions packages/vitest/src/utils/memory-limit.ts
Expand Up @@ -23,7 +23,7 @@ export function getWorkerMemoryLimit(config: ResolvedConfig) {
const memoryLimit = config.poolOptions?.vmThreads?.memoryLimit

if (memoryLimit)
return stringToBytes(memoryLimit)
return memoryLimit

return 1 / (config.poolOptions?.vmThreads?.maxThreads ?? getDefaultThreadsCount(config))
}
Expand Down Expand Up @@ -94,7 +94,7 @@ export function stringToBytes(
return Math.floor(input)
}
else {
throw new Error('Unexpected numerical input for "experimentalVmWorkerMemoryLimit"')
throw new Error('Unexpected numerical input for "memoryLimit"')
}
}

Expand Down

0 comments on commit 70e8a38

Please sign in to comment.