@@ -28,6 +28,7 @@ namespace {
2828kj::Promise<sandbox::ExecutionInfo> RunSandbox (
2929 const sandbox::ExecutionOptions& exec_options,
3030 kj::LowLevelAsyncIoProvider* async_io_provider, uint32_t frontend_id,
31+ const std::set<uint32_t >& canceled_frontends,
3132 std::unordered_map<uint32_t , std::set<int >>* running) {
3233 KJ_LOG (INFO , " Starting sandbox" );
3334 int options_pipe[2 ];
@@ -71,27 +72,34 @@ kj::Promise<sandbox::ExecutionInfo> RunSandbox(
7172 return out->write (ex_opts.get (), sizeof (exec_options))
7273 .attach (std::move (out), std::move (ex_opts))
7374 .then ([fd = options_pipe[1 ], outcome_pipe, pid, in = std::move (in),
74- running, frontend_id]() mutable {
75+ running, frontend_id, &canceled_frontends ]() mutable {
7576 close (fd);
7677 auto promise = in->readAllBytes ();
7778 return promise.attach (std::move (in))
78- .then ([pid, fd = outcome_pipe[0 ], running,
79+ .then ([pid, fd = outcome_pipe[0 ], running, &canceled_frontends,
7980 frontend_id](const kj::Array<kj::byte>& data) {
8081 close (fd);
82+ if (canceled_frontends.count (frontend_id)) {
83+ sandbox::ExecutionInfo info;
84+ info.killed_external = true ;
85+ return info;
86+ }
87+ int ret = 0 ;
88+ int err = waitpid (pid, &ret, 0 );
89+ (*running)[frontend_id].erase (pid);
90+ KJ_ASSERT (err != -1 , strerror (errno));
91+ KJ_ASSERT (ret == 0 , " Sandbox failed" );
92+ KJ_ASSERT (data.size () >= sizeof (size_t ));
8193 size_t error_sz =
8294 *reinterpret_cast <const size_t *>(data.begin ()); // NOLINT
95+ KJ_ASSERT (data.size () >= sizeof (size_t ) + error_sz);
8396 const char * msg =
8497 reinterpret_cast <const char *>(data.begin ()) + // NOLINT
8598 sizeof (size_t );
8699 KJ_ASSERT (!error_sz,
87100 std::string (msg, data.size () - sizeof (size_t )));
88101 sandbox::ExecutionInfo outcome;
89102 memcpy (&outcome, msg, sizeof (outcome));
90- int ret = 0 ;
91- int err = waitpid (pid, &ret, 0 );
92- (*running)[frontend_id].erase (pid);
93- KJ_ASSERT (err != -1 , strerror (errno));
94- KJ_ASSERT (ret == 0 , " Sandbox failed" );
95103 return outcome;
96104 });
97105 });
@@ -374,7 +382,7 @@ kj::Promise<void> Executor::Execute(capnproto::Request::Reader request_,
374382 info_.add (RunSandbox (
375383 exec_options_v[i],
376384 &manager_->Client ().getLowLevelIoProvider (),
377- frontend_id, &running_));
385+ frontend_id, canceled_evaluations_, &running_));
378386 }
379387 return kj::joinPromises (info_.releaseAsArray ());
380388 }))
@@ -456,11 +464,13 @@ kj::Promise<void> Executor::Execute(capnproto::Request::Reader request_,
456464 }
457465 },
458466 [fail](kj::Exception exc) mutable {
467+ KJ_LOG (WARNING , " Execution failed: " , exc.getDescription ());
459468 fail (exc.getDescription ());
460469 })
461470 .eagerlyEvaluate (nullptr );
462471 },
463472 [this ](kj::Exception exc) -> kj::Promise<void > {
473+ KJ_LOG (WARNING , " Execution canceled: " , exc.getDescription ());
464474 manager_->CancelPending ();
465475 return exc;
466476 });
@@ -470,6 +480,7 @@ kj::Promise<void> Executor::cancelRequest(CancelRequestContext context) {
470480 uint32_t evaluation_id = context.getParams ().getEvaluationId ();
471481 KJ_LOG (INFO ,
472482 " Cancelling evaluations of frontend " + std::to_string (evaluation_id));
483+ canceled_evaluations_.insert (evaluation_id);
473484 for (int pid : running_[evaluation_id]) {
474485 kill (pid, SIGINT );
475486 }
0 commit comments