Skip to content

Commit a1bd16c

Browse files
author
Guy Bedford
authored
feat: add out-of-memory callback with stderr log (#805)
1 parent 56f5214 commit a1bd16c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

runtime/fastly/builtins/fastly.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ bool DEBUG_LOGGING_ENABLED = false;
2525

2626
api::Engine *ENGINE;
2727

28+
static void oom_callback(JSContext *cx, void *data) {
29+
fprintf(stderr, "Critical Error: out of memory\n");
30+
fflush(stderr);
31+
}
32+
2833
} // namespace
2934

3035
bool debug_logging_enabled() { return DEBUG_LOGGING_ENABLED; }
@@ -357,6 +362,8 @@ bool install(api::Engine *engine) {
357362
bool ENABLE_EXPERIMENTAL_HIGH_RESOLUTION_TIME_METHODS =
358363
std::string(std::getenv("ENABLE_EXPERIMENTAL_HIGH_RESOLUTION_TIME_METHODS")) == "1";
359364

365+
JS::SetOutOfMemoryCallback(engine->cx(), oom_callback, nullptr);
366+
360367
JS::RootedObject fastly(engine->cx(), JS_NewPlainObject(engine->cx()));
361368
if (!fastly) {
362369
return false;

runtime/js-compute-runtime/js-compute-runtime.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static void rejection_tracker(JSContext *cx, bool mutedErrors, JS::HandleObject
9797
// Note: we unconditionally print these, since they almost always indicate
9898
// serious bugs.
9999
fprintf(stderr, "Adding an unhandled rejected promise to the promise "
100-
"rejection tracker failed");
100+
"rejection tracker failed\n");
101101
}
102102
return;
103103
}
@@ -107,12 +107,17 @@ static void rejection_tracker(JSContext *cx, bool mutedErrors, JS::HandleObject
107107
// Note: we unconditionally print these, since they almost always indicate
108108
// serious bugs.
109109
fprintf(stderr, "Removing an handled rejected promise from the promise "
110-
"rejection tracker failed");
110+
"rejection tracker failed\n");
111111
}
112112
}
113113
}
114114
}
115115

116+
static void oom_callback(JSContext *cx, void *data) {
117+
fprintf(stderr, "Critical Error: out of memory\n");
118+
fflush(stderr);
119+
}
120+
116121
bool init_js() {
117122
JS_Init();
118123

@@ -151,6 +156,7 @@ bool init_js() {
151156
}
152157

153158
JS::SetPromiseRejectionTrackerCallback(cx, rejection_tracker);
159+
JS::SetOutOfMemoryCallback(cx, oom_callback, nullptr);
154160

155161
CONTEXT = cx;
156162
GLOBAL.init(cx, global);

0 commit comments

Comments
 (0)