Coverage Report

Created: 2025-08-28 09:57

/src/node/src/node_worker.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef SRC_NODE_WORKER_H_
2
#define SRC_NODE_WORKER_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <optional>
7
#include <unordered_map>
8
#include "node_exit_code.h"
9
#include "node_messaging.h"
10
#include "uv.h"
11
12
namespace node {
13
14
struct SnapshotData;
15
namespace worker {
16
17
class WorkerThreadData;
18
19
enum ResourceLimits {
20
  kMaxYoungGenerationSizeMb,
21
  kMaxOldGenerationSizeMb,
22
  kCodeRangeSizeMb,
23
  kStackSizeMb,
24
  kTotalResourceLimitCount
25
};
26
27
// A worker thread, as represented in its parent thread.
28
class Worker : public AsyncWrap {
29
 public:
30
  Worker(Environment* env,
31
         v8::Local<v8::Object> wrap,
32
         const std::string& url,
33
         const std::string& name,
34
         std::shared_ptr<PerIsolateOptions> per_isolate_opts,
35
         std::vector<std::string>&& exec_argv,
36
         std::shared_ptr<KVStore> env_vars,
37
         const SnapshotData* snapshot_data);
38
  ~Worker() override;
39
40
  // Run the worker. This is only called from the worker thread.
41
  void Run();
42
43
  // Forcibly exit the thread with a specified exit code. This may be called
44
  // from any thread. `error_code` and `error_message` can be used to create
45
  // a custom `'error'` event before emitting `'exit'`.
46
  void Exit(ExitCode code,
47
            const char* error_code = nullptr,
48
            const char* error_message = nullptr);
49
50
  // Wait for the worker thread to stop (in a blocking manner).
51
  void JoinThread();
52
53
  template <typename Fn>
54
  inline bool RequestInterrupt(Fn&& cb);
55
56
  SET_NO_MEMORY_INFO()
57
  SET_MEMORY_INFO_NAME(Worker)
58
  SET_SELF_SIZE(Worker)
59
  bool IsNotIndicativeOfMemoryLeakAtExit() const override;
60
61
  bool is_stopped() const;
62
0
  const SnapshotData* snapshot_data() const { return snapshot_data_; }
63
64
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
65
  static void CloneParentEnvVars(
66
      const v8::FunctionCallbackInfo<v8::Value>& args);
67
  static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args);
68
  static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
69
  static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
70
  static void HasRef(const v8::FunctionCallbackInfo<v8::Value>& args);
71
  static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
72
  static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
73
  static void GetResourceLimits(
74
      const v8::FunctionCallbackInfo<v8::Value>& args);
75
  v8::Local<v8::Float64Array> GetResourceLimits(v8::Isolate* isolate) const;
76
  static void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args);
77
  static void LoopIdleTime(const v8::FunctionCallbackInfo<v8::Value>& args);
78
  static void LoopStartTime(const v8::FunctionCallbackInfo<v8::Value>& args);
79
80
 private:
81
  bool CreateEnvMessagePort(Environment* env);
82
  static size_t NearHeapLimit(void* data, size_t current_heap_limit,
83
                              size_t initial_heap_limit);
84
85
  std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
86
  std::vector<std::string> exec_argv_;
87
  std::vector<std::string> argv_;
88
89
  MultiIsolatePlatform* platform_;
90
  v8::Isolate* isolate_ = nullptr;
91
  std::optional<uv_thread_t> tid_;  // Set while the thread is running
92
93
  std::unique_ptr<InspectorParentHandle> inspector_parent_handle_;
94
95
  // This mutex protects access to all variables listed below it.
96
  mutable Mutex mutex_;
97
98
  const char* custom_error_ = nullptr;
99
  std::string custom_error_str_;
100
  ExitCode exit_code_ = ExitCode::kNoFailure;
101
  ThreadId thread_id_;
102
  uintptr_t stack_base_ = 0;
103
  // Optional name used for debugging in inspector and trace events.
104
  std::string name_;
105
106
  // Custom resource constraints:
107
  double resource_limits_[kTotalResourceLimitCount];
108
  void UpdateResourceConstraints(v8::ResourceConstraints* constraints);
109
110
  // Full size of the thread's stack.
111
  size_t stack_size_ = 4 * 1024 * 1024;
112
  // Stack buffer size that is not available to the JS engine.
113
  static constexpr size_t kStackBufferSize = 192 * 1024;
114
115
  std::unique_ptr<MessagePortData> child_port_data_;
116
  std::shared_ptr<KVStore> env_vars_;
117
  EmbedderPreloadCallback embedder_preload_;
118
119
  // A raw flag that is used by creator and worker threads to
120
  // sync up on pre-mature termination of worker  - while in the
121
  // warmup phase.  Once the worker is fully warmed up, use the
122
  // async handle of the worker's Environment for the same purpose.
123
  bool stopped_ = true;
124
125
  bool has_ref_ = true;
126
  uint64_t environment_flags_ = EnvironmentFlags::kNoFlags;
127
128
  // The real Environment of the worker object. It has a lesser
129
  // lifespan than the worker object itself - comes to life
130
  // when the worker thread creates a new Environment, and gets
131
  // destroyed alongwith the worker thread.
132
  Environment* env_ = nullptr;
133
134
  const SnapshotData* snapshot_data_ = nullptr;
135
  friend class WorkerThreadData;
136
};
137
138
template <typename Fn>
139
0
bool Worker::RequestInterrupt(Fn&& cb) {
140
0
  Mutex::ScopedLock lock(mutex_);
141
0
  if (env_ == nullptr) return false;
142
0
  env_->RequestInterrupt(std::move(cb));
143
0
  return true;
144
0
}
Unexecuted instantiation: node_report.cc:bool node::worker::Worker::RequestInterrupt<node::report::WriteNodeReport(v8::Isolate*, node::Environment*, char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> >&, v8::Local<v8::Value>, bool, bool)::$_0::operator()(node::worker::Worker*) const::{lambda(node::Environment*)#1}>(node::report::WriteNodeReport(v8::Isolate*, node::Environment*, char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> >&, v8::Local<v8::Value>, bool, bool)::$_0::operator()(node::worker::Worker*) const::{lambda(node::Environment*)#1}&&)
Unexecuted instantiation: node_worker.cc:bool node::worker::Worker::RequestInterrupt<node::worker::Worker::TakeHeapSnapshot(v8::FunctionCallbackInfo<v8::Value> const&)::$_0>(node::worker::Worker::TakeHeapSnapshot(v8::FunctionCallbackInfo<v8::Value> const&)::$_0&&)
145
146
}  // namespace worker
147
}  // namespace node
148
149
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
150
151
152
#endif  // SRC_NODE_WORKER_H_