From ce489360777e0a664ef60037c78d909122dda13d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 18 Jul 2018 02:20:51 +0200 Subject: [PATCH] src: plug trace file file descriptor leak Close existing file descriptors before overriding the field with new ones. Also, use `nullptr` as the loop for these synchronous operations, since they do not run on the same thread as the `uv_run()` call for the previously used loop. PR-URL: https://github.com/nodejs/node/pull/21867 Reviewed-By: James M Snell Reviewed-By: Eugene Ostroukhov Reviewed-By: Ali Ijaz Sheikh --- src/tracing/node_trace_writer.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/tracing/node_trace_writer.cc b/src/tracing/node_trace_writer.cc index 2fdb92972330b5..2fb7c12a82dace 100644 --- a/src/tracing/node_trace_writer.cc +++ b/src/tracing/node_trace_writer.cc @@ -53,7 +53,7 @@ NodeTraceWriter::~NodeTraceWriter() { uv_fs_t req; int err; if (fd_ != -1) { - err = uv_fs_close(tracing_loop_, &req, fd_, nullptr); + err = uv_fs_close(nullptr, &req, fd_, nullptr); CHECK_EQ(err, 0); uv_fs_req_cleanup(&req); } @@ -84,7 +84,12 @@ void NodeTraceWriter::OpenNewFileForStreaming() { replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid())); replace_substring(&filepath, "${rotation}", std::to_string(file_num_)); - fd_ = uv_fs_open(tracing_loop_, &req, filepath.c_str(), + if (fd_ != -1) { + CHECK_EQ(uv_fs_close(nullptr, &req, fd_, nullptr), 0); + uv_fs_req_cleanup(&req); + } + + fd_ = uv_fs_open(nullptr, &req, filepath.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644, nullptr); uv_fs_req_cleanup(&req); if (fd_ < 0) {