Skip to content

Commit

Permalink
stamp: resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyannyacha committed Nov 23, 2024
1 parent 45fb979 commit 399fd97
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 76 deletions.
96 changes: 27 additions & 69 deletions crates/base/src/deno_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use crate::inspector_server::Inspector;
use crate::rt_worker::supervisor::{CPUUsage, CPUUsageMetrics};
use crate::rt_worker::worker::DuplexStreamEntry;
use crate::server::ServerFlags;
use crate::utils::json;
use crate::utils::path::find_up;
use crate::server::ServerFlags;
use crate::utils::units::{bytes_to_display, mib_to_bytes, percentage_value};

use anyhow::{anyhow, bail, Error};
use arc_swap::ArcSwapOption;
use base_mem_check::{MemCheckState, WorkerHeapStatistics};
use base_rt::DenoRuntimeDropToken;
use base_rt::{get_current_cpu_time_ns, BlockingScopeCPUUsage};
use arc_swap::ArcSwapOption;
use cooked_waker::{IntoWaker, WakeRef};
use ctor::ctor;
use deno_core::error::{AnyError, JsError};
use deno_core::url::Url;
use deno_core::v8::{self, GCCallbackFlags, GCType, HeapStatistics, Isolate};
use deno_core::{
located_script_name, serde_json, JsRuntime, ModuleCodeString, ModuleId, ModuleLoader, OpState,
located_script_name, serde_json, JsRuntime, ModuleCodeString, ModuleId, ModuleLoader,
ModuleSpecifier, OpState, PollEventLoopOptions, ResolutionKind, RuntimeOptions,
};
use deno_http::DefaultHttpPropertyExtractor;
Expand All @@ -26,8 +26,7 @@ use deno_tls::rustls::RootCertStore;
use deno_tls::RootCertStoreProvider;
use futures_util::future::poll_fn;
use futures_util::task::AtomicWaker;
use futures_util::Future;
use log::{error, trace};
use log::error;
use once_cell::sync::{Lazy, OnceCell};
use sb_core::http::sb_core_http;
use sb_core::http_start::sb_core_http_start;
Expand Down Expand Up @@ -147,7 +146,6 @@ fn get_error_class_name(e: &AnyError) -> &'static str {
struct MemCheck {
exceeded_token: CancellationToken,
limit: Option<usize>,

waker: Arc<AtomicWaker>,
state: Arc<RwLock<MemCheckState>>,
}
Expand Down Expand Up @@ -202,6 +200,7 @@ impl MemCheck {

fn is_exceeded(&self) -> bool {
self.exceeded_token.is_cancelled()
}
}

pub trait GetRuntimeContext {
Expand Down Expand Up @@ -609,7 +608,7 @@ where

allocator.set_waker(mem_check.waker.clone());

mem_check.limit = Some(memory_limit);
mem_check.limit = Some(memory_limit_bytes);
create_params = Some(
v8::CreateParams::default()
.heap_limits(mib_to_bytes(0) as usize, memory_limit_bytes)
Expand All @@ -632,7 +631,7 @@ where
..Default::default()
};

let mut js_runtime = JsRuntime::new(runtime_options);
let mut js_runtime = ManuallyDrop::new(JsRuntime::new(runtime_options));

let dispatch_fns = {
let context = js_runtime.main_context();
Expand Down Expand Up @@ -903,8 +902,10 @@ where
self.js_runtime.v8_isolate().enter();

if inspector.is_some() {
let is_terminated = self.is_terminated.clone();
let mut this = scopeguard::guard_on_unwind(&mut *self, |this| {
this.js_runtime.v8_isolate().exit();
is_terminated.raise();
});

{
Expand All @@ -922,6 +923,7 @@ where

if this.termination_request_token.is_cancelled() {
this.js_runtime.v8_isolate().exit();
is_terminated.raise();
return (Ok(()), 0i64);
}
}
Expand Down Expand Up @@ -983,9 +985,10 @@ where

if let Err(err) = with_cpu_metrics_guard(
current_thread_id,
this.js_runtime.op_state(),
&maybe_cpu_usage_metrics_tx,
&mut accumulated_cpu_time_ns,
|| MaybeDenoRuntime::DenoRuntime(&mut this).dispatch_load_event(),
|| MaybeDenoRuntime::DenoRuntime(*this).dispatch_load_event(),
) {
return (Err(err), get_accumulated_cpu_time_ms!());
}
Expand All @@ -1003,13 +1006,12 @@ where
let mut this = self.get_v8_tls_guard();
let _ = with_cpu_metrics_guard(
current_thread_id,
this.js_runtime.op_state(),
&maybe_cpu_usage_metrics_tx,
&mut accumulated_cpu_time_ns,
|| MaybeDenoRuntime::DenoRuntime(&mut this).dispatch_unload_event(),
) {
return (Err(err), get_accumulated_cpu_time_ms!());
}

);

// TODO(Nyannyacha): Here we also need to trigger the event for node platform (i.e; exit)

return (
Expand All @@ -1022,6 +1024,7 @@ where

if let Err(err) = with_cpu_metrics_guard(
current_thread_id,
this.js_runtime.op_state(),
&maybe_cpu_usage_metrics_tx,
&mut accumulated_cpu_time_ns,
|| MaybeDenoRuntime::DenoRuntime(&mut this).dispatch_unload_event(),
Expand All @@ -1048,7 +1051,7 @@ where
let beforeunload_cpu_threshold = self.beforeunload_cpu_threshold.clone();
let beforeunload_mem_threshold = self.beforeunload_mem_threshold.clone();

let mem_check_state = is_user_worker.then(|| self.mem_check_state.clone());
let mem_check_state = is_user_worker.then(|| self.mem_check.clone());
let mut poll_sem = None::<PollSemaphore>;

poll_fn(move |cx| {
Expand Down Expand Up @@ -1123,6 +1126,9 @@ where

if is_user_worker {
let mem_state = mem_check_state.as_ref().unwrap();
let total_malloced_bytes = mem_state.check(js_runtime.v8_isolate().as_mut());

mem_state.waker.register(waker);

if let Some(threshold_ms) = beforeunload_cpu_threshold.load().as_deref().copied() {
let threshold_ns = (threshold_ms as i128) * 1_000_000;
Expand Down Expand Up @@ -1155,9 +1161,6 @@ where
}
}
}

mem_state.check(js_runtime.v8_isolate().as_mut());
mem_state.waker.register(waker);
}

// NOTE(Nyannyacha): If tasks are empty or V8 is not evaluating the
Expand Down Expand Up @@ -1240,7 +1243,7 @@ where
let _ = handle.request_interrupt(interrupt_fn, std::ptr::null_mut());
};

drop(rt::SUPERVISOR_RT.spawn({
drop(base_rt::SUPERVISOR_RT.spawn({
let cancel_task_token = cancel_task_token.clone();

async move {
Expand Down Expand Up @@ -1294,12 +1297,15 @@ impl<'s> Scope<'s> {
}
}

pub enum MaybeDenoRuntime<'l> {
DenoRuntime(&'l mut DenoRuntime),
pub enum MaybeDenoRuntime<'l, RuntimeContext> {
DenoRuntime(&'l mut DenoRuntime<RuntimeContext>),
Isolate(&'l mut v8::Isolate),
}

impl<'l> MaybeDenoRuntime<'l> {
impl<'l, RuntimeContext> MaybeDenoRuntime<'l, RuntimeContext>
where
RuntimeContext: GetRuntimeContext,
{
fn scope(&mut self) -> Scope<'_> {
let op_state = self.op_state();
let op_state_ref = op_state.borrow();
Expand Down Expand Up @@ -1424,10 +1430,6 @@ impl<'l> MaybeDenoRuntime<'l> {
}
}

fn get_current_cpu_time_ns() -> Result<i64, Error> {
get_thread_time().context("can't get current thread time")
}

pub fn import_meta_resolve_callback(
loader: &dyn ModuleLoader,
specifier: String,
Expand Down Expand Up @@ -1911,50 +1913,6 @@ mod test {
std::mem::drop(main_mod_ev);
}

async fn create_runtime(
path: Option<&str>,
env_vars: Option<HashMap<String, String>>,
user_conf: Option<WorkerRuntimeOpts>,
static_patterns: Vec<String>,
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
) -> DenoRuntime {
let (worker_pool_tx, _) = mpsc::unbounded_channel::<UserWorkerMsgs>();

DenoRuntime::new(
WorkerContextInitOpts {
service_path: path
.map(PathBuf::from)
.unwrap_or(PathBuf::from("./test_cases/main")),

no_module_cache: false,
import_map_path: None,
env_vars: env_vars.unwrap_or_default(),
events_rx: None,
timing: None,
maybe_eszip: None,
maybe_entrypoint: None,
maybe_decorator: None,
maybe_module_code: None,
conf: {
if let Some(uc) = user_conf {
uc
} else {
WorkerRuntimeOpts::MainWorker(MainWorkerRuntimeOpts {
worker_pool_tx,
shared_metric_src: None,
event_worker_metric_src: None,
})
}
},
static_patterns,
maybe_jsx_import_source_config,
},
None,
)
.await
.unwrap()
}

// Main Runtime should have access to `EdgeRuntime`
#[tokio::test]
#[serial]
Expand Down
2 changes: 1 addition & 1 deletion crates/base/src/rt_worker/supervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ extern "C" fn v8_handle_wall_clock_beforeunload(
isolate: &mut v8::Isolate,
_data: *mut std::ffi::c_void,
) {
if let Err(err) = MaybeDenoRuntime::Isolate(isolate)
if let Err(err) = MaybeDenoRuntime::<()>::Isolate(isolate)
.dispatch_beforeunload_event(WillTerminateReason::WallClock)
{
warn!(
Expand Down
4 changes: 2 additions & 2 deletions crates/base/src/rt_worker/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use sb_workers::context::{UserWorkerMsgs, WorkerContextInitOpts, WorkerExit, Wor
use std::any::Any;
use std::future::{pending, Future};
use std::pin::Pin;
use std::time::Duration;
use std::sync::Arc;
use std::time::Duration;
use tokio::io;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::sync::oneshot::{self, Receiver, Sender};
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Worker {
termination_request_token.cancel();

let data_ptr_mut = Box::into_raw(Box::new(
supervisor::IsolateInterruptData {
supervisor::V8HandleTerminationData {
should_terminate: true,
isolate_memory_usage_tx: None,
},
Expand Down
3 changes: 1 addition & 2 deletions crates/base/src/rt_worker/worker_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ pub async fn create_worker<Opt: Into<CreateWorkerArgs>>(
init_opts.into();

let worker_kind = worker_init_opts.conf.to_worker_kind();
let request_idle_timeout = flags.request_idle_timeout_ms;
let exit = WorkerExit::default();
let mut worker = Worker::new(&worker_init_opts)?;

Expand All @@ -629,7 +628,7 @@ pub async fn create_worker<Opt: Into<CreateWorkerArgs>>(
exit.clone(),
maybe_termination_token.clone(),
inspector,
flags,
flags.clone(),
);

// create an async task waiting for requests for worker
Expand Down
1 change: 0 additions & 1 deletion crates/base/src/rt_worker/worker_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ impl WorkerPool {
user_workers: HashMap::new(),
active_workers: HashMap::new(),
maybe_inspector: inspector,
flags: Arc::new(flags),
worker_pool_msgs_tx,
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/sb_core/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ function processRejectionHandled(promise, reason) {
}
}

globalThis.bootstrapSBEdge = opts => {
globalThis.bootstrapSBEdge = (opts, extraCtx) => {
globalThis_ = globalThis;

// We should delete this after initialization,
// Deleting it during bootstrapping can backfire
delete globalThis.__bootstrap;
Expand Down

0 comments on commit 399fd97

Please sign in to comment.