Skip to content

feat: add out-of-memory callback with stderr log #805

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

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions runtime/fastly/builtins/fastly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ bool DEBUG_LOGGING_ENABLED = false;

api::Engine *ENGINE;

static void oom_callback(JSContext *cx, void *data) {
fprintf(stderr, "Critical Error: out of memory\n");
fflush(stderr);
}

} // namespace

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

JS::SetOutOfMemoryCallback(engine->cx(), oom_callback, nullptr);

JS::RootedObject fastly(engine->cx(), JS_NewPlainObject(engine->cx()));
if (!fastly) {
return false;
Expand Down
10 changes: 8 additions & 2 deletions runtime/js-compute-runtime/js-compute-runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static void rejection_tracker(JSContext *cx, bool mutedErrors, JS::HandleObject
// Note: we unconditionally print these, since they almost always indicate
// serious bugs.
fprintf(stderr, "Adding an unhandled rejected promise to the promise "
"rejection tracker failed");
"rejection tracker failed\n");
}
return;
}
Expand All @@ -107,12 +107,17 @@ static void rejection_tracker(JSContext *cx, bool mutedErrors, JS::HandleObject
// Note: we unconditionally print these, since they almost always indicate
// serious bugs.
fprintf(stderr, "Removing an handled rejected promise from the promise "
"rejection tracker failed");
"rejection tracker failed\n");
}
}
}
}

static void oom_callback(JSContext *cx, void *data) {
fprintf(stderr, "Critical Error: out of memory\n");
fflush(stderr);
}

bool init_js() {
JS_Init();

Expand Down Expand Up @@ -151,6 +156,7 @@ bool init_js() {
}

JS::SetPromiseRejectionTrackerCallback(cx, rejection_tracker);
JS::SetOutOfMemoryCallback(cx, oom_callback, nullptr);

CONTEXT = cx;
GLOBAL.init(cx, global);
Expand Down
Loading