Skip to content

Commit b6f41b0

Browse files
TartanLlamazkat
andauthored
feat: Bail out of reusable sandbox if request fails (#1453)
Co-authored-by: Kat Marchán <kzm@zkat.tech>
1 parent be0867b commit b6f41b0

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

runtime/fastly/handler.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool install(api::Engine *engine) {
2626
return true;
2727
}
2828

29-
void handle_incoming(host_api::Request req) {
29+
bool handle_incoming(host_api::Request req) {
3030
builtins::web::performance::Performance::timeOrigin.emplace(
3131
std::chrono::high_resolution_clock::now());
3232

@@ -47,7 +47,7 @@ void handle_incoming(host_api::Request req) {
4747
RootedObject fetch_event(ENGINE->cx(), FetchEvent::create(ENGINE->cx()));
4848
if (!FetchEvent::init_request(ENGINE->cx(), fetch_event, req.req, req.body)) {
4949
ENGINE->dump_pending_exception("initialization of FetchEvent");
50-
return;
50+
return false;
5151
}
5252

5353
if (ENGINE->debug_logging_enabled()) {
@@ -66,27 +66,32 @@ void handle_incoming(host_api::Request req) {
6666

6767
if (JS_IsExceptionPending(ENGINE->cx())) {
6868
ENGINE->dump_pending_exception("evaluating code");
69+
return false;
6970
} else if (!success) {
7071
if (ENGINE->has_pending_async_tasks()) {
7172
fprintf(stderr, "Warning: JS event loop terminated with async tasks pending. "
7273
"Use FetchEvent#waitUntil to extend the service's lifetime "
7374
"if needed.\n");
75+
return false;
7476
} else {
7577
fprintf(stderr, "Warning: JS event loop terminated without completing the request.\n");
78+
return false;
7679
}
7780
}
7881

79-
if (ENGINE->debug_logging_enabled() && ENGINE->has_pending_async_tasks()) {
80-
fprintf(stderr, "Warming: JS event loop terminated with async tasks pending. "
81-
"Use FetchEvent#waitUntil to extend the service's lifetime "
82-
"if needed.\n");
83-
return;
82+
if (ENGINE->has_pending_async_tasks()) {
83+
if (ENGINE->debug_logging_enabled()) {
84+
fprintf(stderr, "Warning: JS event loop terminated with async tasks pending. "
85+
"Use FetchEvent#waitUntil to extend the service's lifetime "
86+
"if needed.\n");
87+
}
88+
return false;
8489
}
8590

8691
// Respond with status `500` if no response was ever sent.
8792
if (!FetchEvent::response_started(fetch_event)) {
8893
FetchEvent::respondWithError(ENGINE->cx(), fetch_event);
89-
return;
94+
return false;
9095
}
9196

9297
if (ENGINE->debug_logging_enabled()) {
@@ -95,7 +100,7 @@ void handle_incoming(host_api::Request req) {
95100
printf("Done. Total request processing time: %fms. Total compute time: %fms\n", diff / 1000,
96101
total_compute / 1000);
97102
}
98-
return;
103+
return true;
99104
}
100105

101106
} // namespace fastly::runtime
@@ -121,7 +126,16 @@ int main(int argc, const char *argv[]) {
121126
std::size_t requests_handled = 0;
122127
const auto start_time = std::chrono::high_resolution_clock::now();
123128
while (true) {
124-
fastly::runtime::handle_incoming(req.unwrap());
129+
bool success = fastly::runtime::handle_incoming(req.unwrap());
130+
131+
if (!success) {
132+
if (ENGINE->debug_logging_enabled()) {
133+
printf("Request handling not successful, exiting process.\n");
134+
fflush(stdout);
135+
}
136+
return -1;
137+
}
138+
125139
requests_handled++;
126140

127141
// Check if we should exit based on configured max requests

0 commit comments

Comments
 (0)