mirror of
https://github.com/nodejs/node.git
synced 2026-12-05 17:28:46 -05:00
When a process exits cleanly, i.e. because the event loop ends up
without things to wait for, the Node.js objects that are left on
the heap should be:
1. weak, i.e. ready for garbage collection once no longer
referenced, or
2. detached, i.e. scheduled for destruction once no longer
referenced, or
3. an unrefed libuv handle, i.e. does not keep the event loop
alive, or
4. an inactive libuv handle (essentially the same here)
There are a few exceptions to this rule, but generally,
if there are C++-backed Node.js objects on the heap
that do not fall into the above categories, we may be looking
at a potential memory leak. Most likely, the cause is a missing
`MakeWeak()` call on the corresponding object.
In order to avoid this kind of problem, we check the list
of BaseObjects for these criteria. In this commit, we only do so
when explicitly instructed to or when in debug mode
(where --verify-base-objects is always-on).
In particular, this avoids the kinds of memory leak issues
that were fixed in the PRs referenced below.
Refs: https://github.com/nodejs/node/pull/35488
Refs: https://github.com/nodejs/node/pull/35487
Refs: https://github.com/nodejs/node/pull/35481
PR-URL: https://github.com/nodejs/node/pull/35490
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
116 lines
3.6 KiB
C++
116 lines
3.6 KiB
C++
#ifndef SRC_MODULE_WRAP_H_
|
|
#define SRC_MODULE_WRAP_H_
|
|
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#include <unordered_map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include "base_object.h"
|
|
|
|
namespace node {
|
|
|
|
class Environment;
|
|
|
|
namespace contextify {
|
|
class ContextifyContext;
|
|
}
|
|
|
|
namespace loader {
|
|
|
|
enum ScriptType : int {
|
|
kScript,
|
|
kModule,
|
|
kFunction,
|
|
};
|
|
|
|
enum HostDefinedOptions : int {
|
|
kType = 8,
|
|
kID = 9,
|
|
kLength = 10,
|
|
};
|
|
|
|
class ModuleWrap : public BaseObject {
|
|
public:
|
|
enum InternalFields {
|
|
kModuleWrapBaseField = BaseObject::kInternalFieldCount,
|
|
kURLSlot,
|
|
kSyntheticEvaluationStepsSlot,
|
|
kContextObjectSlot, // Object whose creation context is the target Context
|
|
kInternalFieldCount
|
|
};
|
|
|
|
static void Initialize(v8::Local<v8::Object> target,
|
|
v8::Local<v8::Value> unused,
|
|
v8::Local<v8::Context> context,
|
|
void* priv);
|
|
static void HostInitializeImportMetaObjectCallback(
|
|
v8::Local<v8::Context> context,
|
|
v8::Local<v8::Module> module,
|
|
v8::Local<v8::Object> meta);
|
|
|
|
void MemoryInfo(MemoryTracker* tracker) const override {
|
|
tracker->TrackField("resolve_cache", resolve_cache_);
|
|
}
|
|
|
|
inline uint32_t id() { return id_; }
|
|
v8::Local<v8::Context> context() const;
|
|
static ModuleWrap* GetFromID(node::Environment*, uint32_t id);
|
|
|
|
SET_MEMORY_INFO_NAME(ModuleWrap)
|
|
SET_SELF_SIZE(ModuleWrap)
|
|
|
|
bool IsNotIndicativeOfMemoryLeakAtExit() const override {
|
|
// XXX: The garbage collection rules for ModuleWrap are *super* unclear.
|
|
// Do these objects ever get GC'd? Are we just okay with leaking them?
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
ModuleWrap(Environment* env,
|
|
v8::Local<v8::Object> object,
|
|
v8::Local<v8::Module> module,
|
|
v8::Local<v8::String> url);
|
|
~ModuleWrap() override;
|
|
|
|
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void Link(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void Instantiate(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void Evaluate(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void GetNamespace(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void GetStatus(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void GetError(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void GetStaticDependencySpecifiers(
|
|
const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void SetImportModuleDynamicallyCallback(
|
|
const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void SetInitializeImportMetaObjectCallback(
|
|
const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static v8::MaybeLocal<v8::Value> SyntheticModuleEvaluationStepsCallback(
|
|
v8::Local<v8::Context> context, v8::Local<v8::Module> module);
|
|
static void SetSyntheticExport(
|
|
const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static v8::MaybeLocal<v8::Module> ResolveCallback(
|
|
v8::Local<v8::Context> context,
|
|
v8::Local<v8::String> specifier,
|
|
v8::Local<v8::Module> referrer);
|
|
static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>);
|
|
|
|
v8::Global<v8::Module> module_;
|
|
std::unordered_map<std::string, v8::Global<v8::Promise>> resolve_cache_;
|
|
contextify::ContextifyContext* contextify_context_ = nullptr;
|
|
bool synthetic_ = false;
|
|
bool linked_ = false;
|
|
uint32_t id_;
|
|
};
|
|
|
|
} // namespace loader
|
|
} // namespace node
|
|
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
#endif // SRC_MODULE_WRAP_H_
|