destr
destr copied to clipboard
Memory leak in `destr` package, Nuxt 3 production usage
Hello, @pi0.
I found a memory leak, that affects Nuxt 3 framework, because the destr
package is used as default parseResponse
option in the ohmyfetch
, that used in the Nuxt 3 useFetch()
function as $fetch
.
Memory leak
Flamegraph
Table
Memory usage
Temporary fix for Nuxt 3
-
Create a local TypeScript file (
lib/use-fetch.ts
) with copy ofuseFetch()
function. -
Add custom
parseResponse()
function:import type { FetchOptions } from 'ohmyfetch'; /** * Parse response * @description Replace of `destr()` function to prevent memory leak * @param responseText */ const parseResponse: FetchOptions['parseResponse'] = (responseText) => { // Send empty string if zero length if (responseText.length === 0) { return ''; } try { return JSON.parse(responseText); } catch (_e) { return responseText; } };
-
Replace
_fetchOptions
constant in the copy ofuseFetch()
function:const _fetchOptions = { ...opts, parseResponse, cache: typeof opts.cache === 'boolean' ? undefined : opts.cache }
-
Use patched
useFetch()
function instead of build-in.
Because, right now there is no way to provide custom parseResponse()
function as option of the useFetch()
without error.
Best wishes, Sergey.
Hi @mrauhu thanks for making the issue and providing a workaround but are you sure the memory leak originates from destr and memory is not back after a while with GC? Can you please provide a minimal reproduction? 🙏🏼 (without nuxt but a direct Node.js repo using destr)
Any update on this?
Any update on this?
@pi0 unfortunately, I can't create a reproduction in the isolated environment only for destr
package.
Changes after replacing destr()
to JSON.parse()
function for the production server, a week of observations:
$fetch parseResponse option |
Max memory usage, MB |
---|---|
destr() |
509 |
JSON.parse() |
112 |
Memory usage
Flamegraph
Table
Higher memory usage is likely the overhead of regex checkers over a heavy payload. What happens after max memory usage? Do you auto restart? Also what is prod Node.js version?
Higher memory usage is likely the overhead of regex checkers over a heavy payload.
API responses is nested tree JSON documents, 50—100 KB size.
What happens after max memory usage?
Restart of a container.
Do you auto restart?
Yes.
Also what is prod Node.js version?
Node.js 16.14.2, a Docker image based on the https://github.com/astefanutti/scratch-node.
@mrauhu with which application did you examine the memory leak with?
@dargmuesli it's part of the Datadog APM.
The dd-trace-js
package was used for tracing. Under the hood it's using the @datadog/pprof
package, fork of pprof
.
The Datadog Continuous Profiler used for visualization.
@mrauhu Might I ask how you implemented the dd-trace-js in your application? Did you use a module or a server plugin? Thanks!
Hi. Kindly this issue still needs a minimal reproduction. Please ping to reopen if you still believe there is a possible memory leak in destr itself.