From 2d4d2307bc4fcfc94a0b8ce74b5caf210dc916ea Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Tue, 12 Dec 2023 19:54:21 +0000 Subject: [PATCH] refactor: set v8 flags before initialize v8 platform After the v8 platform had initialized, it was no longer able to set the v8 flags, so it is necessary to adjust the invocation order of the set v8 flag function to avoid v8's static assertion. (cherry picked from commit bd1975276df6f3ded90a23c21e5729ebe50a2dba) --- crates/base/src/commands.rs | 20 ++++++++++++++++++++ crates/base/src/deno_runtime.rs | 16 ---------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/crates/base/src/commands.rs b/crates/base/src/commands.rs index 87f92bd75..406c4e638 100644 --- a/crates/base/src/commands.rs +++ b/crates/base/src/commands.rs @@ -1,6 +1,7 @@ use crate::server::{Server, ServerCodes, WorkerEntrypoints}; use anyhow::Error; use deno_core::JsRuntime; +use log::error; use tokio::sync::mpsc::Sender; #[allow(clippy::too_many_arguments)] @@ -14,6 +15,8 @@ pub async fn start_server( callback_tx: Option>, entrypoints: WorkerEntrypoints, ) -> Result<(), Error> { + set_v8_flags(); + // NOTE(denoland/deno/20495): Due to the new PKU (Memory Protection Keys) // feature introduced in V8 11.6, We need to initialize the V8 platform on // the main thread that spawns V8 isolates. @@ -32,3 +35,20 @@ pub async fn start_server( .await?; server.listen().await } + +fn set_v8_flags() { + let v8_flags = std::env::var("V8_FLAGS").unwrap_or("".to_string()); + let mut vec = vec![""]; + + if v8_flags.is_empty() { + return; + } + + vec.append(&mut v8_flags.split(' ').collect()); + + let ignored = deno_core::v8_set_flags(vec.iter().map(|v| v.to_string()).collect()); + + if *ignored.as_slice() != [""] { + error!("v8 flags unrecognized {:?}", ignored); + } +} diff --git a/crates/base/src/deno_runtime.rs b/crates/base/src/deno_runtime.rs index 8368fc72f..2b828ece4 100644 --- a/crates/base/src/deno_runtime.rs +++ b/crates/base/src/deno_runtime.rs @@ -58,20 +58,6 @@ fn get_error_class_name(e: &AnyError) -> &'static str { sb_core::errors_rt::get_error_class_name(e).unwrap_or("Error") } -fn set_v8_flags() { - let v8_flags = std::env::var("V8_FLAGS").unwrap_or("".to_string()); - let mut vec = vec!["IGNORED"]; - if v8_flags.is_empty() { - return; - } - - vec.append(&mut v8_flags.split(' ').collect()); - error!( - "v8 flags unrecognized {:?}", - deno_core::v8_set_flags(vec.iter().map(|v| v.to_string()).collect()) - ); -} - pub struct DenoRuntime { pub js_runtime: JsRuntime, pub env_vars: HashMap, // TODO: does this need to be pub? @@ -95,8 +81,6 @@ impl DenoRuntime { maybe_module_code, } = opts; - set_v8_flags(); - let user_agent = "supabase-edge-runtime".to_string(); let base_dir_path = std::env::current_dir().map(|p| p.join(&service_path))?; let base_url = Url::from_directory_path(&base_dir_path).unwrap();