Skip to content

Fix recursion issue #20

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

Merged
merged 2 commits into from
Feb 10, 2022
Merged

Fix recursion issue #20

merged 2 commits into from
Feb 10, 2022

Conversation

simon-abbott
Copy link
Contributor

Creating a heapdump forces a garbage collection which can then trigger the OOM flow which then calls OnOOMError which then creates a heapdump which then forces GC which then triggers the OOM flow which...

This recursive loop is bad for several reasons, not the least of which being if addTimestamp is true it will keep appending the timestamp to the filename, even past the end of the allotted 256 chars. This PR solves this in two parts:

  1. Temporarily raise the limit while capturing the heapdump, so the GC logic thinks there is enough room.
  2. Fatal error if OnOOMError is called more than once, just in case #1 didn't work.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Creating a heapdump forces a garbage collection, which can then trigger
the OOM flow again, which then calls OnOOMError which then creates a
heapdump which forces GC which triggers OOM which... This recursive loop
is bad for several reasons, not the least of which being if addTimestamp
is true it will keep appending the timestamp to the filename, even past
the end of the allotted 256 chars. Rather than let this happen let's
instead treat it as an error condition and bail.
As noted in the last commit, capturing a heap snapshot forces a garbage
collection which can recursively trigger the OOM flow. To prevent this
we now raise the heap size limit while capturing this last dump. As far
as I can tell this is safe, even in memory-constrained environments,
because by the time we get to this section the JS execution has already
been halted, so the JS heap won't grow any further.
@paulrutter
Copy link
Contributor

paulrutter commented Feb 9, 2022

Thanks for the PR, i missed it apparently.
I'm busy with removing the native code altogether in a new version, as Node offers out of the box tooling to create heapdumps on OoM since node 14.x.

So not sure what to do with this PR. Any ideas?
I would suggest looking into the native functionality in Node if you're using Node > 14 .

See https://github.com/blueconic/node-oom-heapdump/tree/feature/3.0.0

@paulrutter
Copy link
Contributor

@simon-abbott i tried your PR out, and it works perfect on Node 16.
Even though there is native support in Node nowadays, it might be good to just have a working version of this module for Node 14-16 as well.

I'll work on it some work (i want to update some deprecated libraries) and that works out, i'll publish a new version to NPM.

@paulrutter paulrutter mentioned this pull request Feb 10, 2022
@paulrutter paulrutter merged commit 5b0c64b into blueconic:master Feb 10, 2022
@paulrutter
Copy link
Contributor

paulrutter commented Feb 10, 2022

@simon-abbott i published a new (tagged alpha) version 3.0.0 here: https://www.npmjs.com/package/node-oom-heapdump/v/3.0.0. You can test it by using npm install node-oom-heapdump@alpha --save.

I'm gonna test a bit more myself and also #22 need to be fixed still.
If that's all ok, i can publish 3.0.0 as "latest".

@simon-abbott
Copy link
Contributor Author

@paulrutter

Node offers out of the box tooling to create heapdumps on OoM since node 14.x.

Yes it technically does, but when I was testing it last it didn't work as well as I needed it to. I don't remember the exact problem(s) I ran into, but it definitely didn't check all the boxes for my use-case. This package does, however, so I'm glad to see it's still being at least lightly maintained.

@simon-abbott simon-abbott deleted the fix-recursion branch February 10, 2022 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants