-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
Memory leak in node::fs::ReadFileUtf8(const FunctionCallbackInfo<Value>& args) when args[0] is a string. #57800
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
Comments
Humble ping to @nodejs/platform-windows and @nodejs/libuv |
At a quick look it's missing a diff --git a/src/node_file.cc b/src/node_file.cc
index b2e3889b412..ecc682d2f0c 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -2709,8 +2709,8 @@ static void ReadFileUtf8(const FunctionCallbackInfo<Value>& args) {
FS_SYNC_TRACE_BEGIN(open);
file = uv_fs_open(nullptr, &req, *path, flags, 0666, nullptr);
FS_SYNC_TRACE_END(open);
+ uv_fs_req_cleanup(&req);
if (req.result < 0) {
- uv_fs_req_cleanup(&req);
// req will be cleaned up by scope leave.
return env->ThrowUVException(
static_cast<int>(req.result), "open", nullptr, path.out()); The |
@bnoordhuis There is a deferred Lines 2720 to 2727 in 437c6aa
Before #49691, there was a separate |
@tniessen yes, it's tied to the |
I tested this change locally too. It fixed the issue without any obvious side effects. |
@bnoordhuis are you sending a PR? |
No. Feel free to take the diff, no attribution needed. |
I can make a PR with my branch. I'm re-running all the tests now. |
Always call uv_fs_req_cleanup after calling uv_fs_open instead of just when uv_fs_open returns a negative result. I referenced ReadFileSync from node:js2c when making this change. https://github.com/bnoordhuis made the same suggestion based on the PR nodejs#49691. Fixes: nodejs#57800
After pulling latest, something changed and started preventing me from running Feel free to take over if I'm not doing this right. I'm not super familiar with working on open-source projects. Is this issue with spaces in paths on Windows a known thing? Any way to get around it?
|
Free req.file.pathw in fs::ReadFileUtf8(). Always call uv_fs_req_cleanup after calling uv_fs_open instead of just when uv_fs_open returns a negative result. I referenced ReadFileSync from node:js2c when making this change. https://github.com/bnoordhuis made the same suggestion based on the PR nodejs#49691. Fixes: nodejs#57800
Always call uv_fs_req_cleanup after calling uv_fs_open instead of just when uv_fs_open returns a negative result. I referenced ReadFileSync from node:js2c when making this change. https://github.com/bnoordhuis made the same suggestion based on the PR nodejs#49691. Fixes: nodejs#57800
Added a unit test for testing the memory usage of readFileSync. Test is looking specifically for the the issue caused by failing to free the filepath buffer in fs::ReadFileUtf8(), but it will also catch other significant memory leaks in readFileSync() as well. Refs: nodejs#57800
Added a unit test for testing the memory usage of readFileSync. Test is looking specifically for the the issue caused by failing to free the filepath buffer in fs::ReadFileUtf8(), but it will also catch other significant memory leaks in readFileSync() as well. Refs: #57800 PR-URL: #57811 Fixes: #57800 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Always call uv_fs_req_cleanup after calling uv_fs_open instead of just when uv_fs_open returns a negative result. I referenced ReadFileSync from node:js2c when making this change. https://github.com/bnoordhuis made the same suggestion based on the PR #49691. Fixes: #57800 PR-URL: #57811 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Added a unit test for testing the memory usage of readFileSync. Test is looking specifically for the the issue caused by failing to free the filepath buffer in fs::ReadFileUtf8(), but it will also catch other significant memory leaks in readFileSync() as well. Refs: #57800 PR-URL: #57811 Fixes: #57800 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Always call uv_fs_req_cleanup after calling uv_fs_open instead of just when uv_fs_open returns a negative result. I referenced ReadFileSync from node:js2c when making this change. https://github.com/bnoordhuis made the same suggestion based on the PR #49691. Fixes: #57800 PR-URL: #57811 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Added a unit test for testing the memory usage of readFileSync. Test is looking specifically for the the issue caused by failing to free the filepath buffer in fs::ReadFileUtf8(), but it will also catch other significant memory leaks in readFileSync() as well. Refs: #57800 PR-URL: #57811 Fixes: #57800 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Always call uv_fs_req_cleanup after calling uv_fs_open instead of just when uv_fs_open returns a negative result. I referenced ReadFileSync from node:js2c when making this change. https://github.com/bnoordhuis made the same suggestion based on the PR #49691. Fixes: #57800 PR-URL: #57811 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Added a unit test for testing the memory usage of readFileSync. Test is looking specifically for the the issue caused by failing to free the filepath buffer in fs::ReadFileUtf8(), but it will also catch other significant memory leaks in readFileSync() as well. Refs: #57800 PR-URL: #57811 Fixes: #57800 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Always call uv_fs_req_cleanup after calling uv_fs_open instead of just when uv_fs_open returns a negative result. I referenced ReadFileSync from node:js2c when making this change. https://github.com/bnoordhuis made the same suggestion based on the PR #49691. Fixes: #57800 PR-URL: #57811 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Added a unit test for testing the memory usage of readFileSync. Test is looking specifically for the the issue caused by failing to free the filepath buffer in fs::ReadFileUtf8(), but it will also catch other significant memory leaks in readFileSync() as well. Refs: #57800 PR-URL: #57811 Fixes: #57800 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
Version
v22.14.0
Platform
Subsystem
node::fs
What steps will reproduce the bug?
Open a file with ReadFileUtf8() using a path (not a file handle).
How often does it reproduce? Is there a required condition?
Every time, including loading JavaScript files for execution.
What is the expected behavior? Why is that the expected behavior?
Calling
readFileSync()
with an encoding of UTF-8 should not permanently increase the amount of rss memory being used by node. It's (hopefully) the expected behavior because memory leaks are bad.What do you see instead?
Calling will cause Node's memory usage to go up indefinitely.
This was discovered when a process using
node:worker_threads
would continuously increase it's memory consumption until it was forcibly closed. JavaScript debug tools showed that the managed heap's memory usage was constant, but the rss memory usage kept rising. Debugging the node runtime showed that loading modules was calling the underlyingReadFileUtf8
method. The process that was showing this memory leak was not caching workers and was re-loading all code files each time a worker thread was created.Minimum code example:
Memory usage in task manager:

Output from
process.memoryUsage()
:Additional information
This is my best guess at the bug. I'm like 85% sure of what's going, but this is the first time I'm looking at the NodeJS codebase.
Allocation:
void ReadFileUtf8(const FunctionCallbackInfo<Value>& args)
callsuv_fs_open
which callsfs__capture_path
.fs__capture_path
allocates a UTF16 buffer and assigns it touv_fs_s.file.pathw
.uv_fs_s.file
is a union type with the definition:union { WCHAR* pathw; int fd; }
Corruption
uv_file file;
is allocated and assigned the return value fromuv_fs_open
.ReadFileUtf8
callsuv_fs_read
passing in the file handlefile
.uv_fs_read
overwrites the least significant half ofuv_fs_s.file.pathw
with this callreq->file.fd = fd;
.uv_fs_s.file.pathw
is lost.The text was updated successfully, but these errors were encountered: