diff --git a/packages/next-swc/crates/napi/Cargo.toml b/packages/next-swc/crates/napi/Cargo.toml index ab386c6f3ad03f..30a8df56de3568 100644 --- a/packages/next-swc/crates/napi/Cargo.toml +++ b/packages/next-swc/crates/napi/Cargo.toml @@ -28,8 +28,7 @@ tracing = { version = "0.1.32", features = ["release_max_level_info"] } tracing-futures = "0.2.5" tracing-subscriber = "0.3.9" -# https://github.com/thoren-d/tracing-chrome/pull/10 -tracing-chrome = { git = "https://github.com/kwonoj/tracing-chrome", rev = "0dd9d6c9e0f74f43993b58560f4ac0103e058df8" } +tracing-chrome = "0.5.0" [build-dependencies] napi-build = "1" diff --git a/packages/next-swc/crates/napi/src/util.rs b/packages/next-swc/crates/napi/src/util.rs index dbb5509951c233..8476ff55d6e65a 100644 --- a/packages/next-swc/crates/napi/src/util.rs +++ b/packages/next-swc/crates/napi/src/util.rs @@ -29,7 +29,7 @@ DEALINGS IN THE SOFTWARE. use anyhow::{anyhow, Context, Error}; use napi::{CallContext, Env, JsBuffer, JsExternal, JsString, JsUndefined, JsUnknown, Status}; use serde::de::DeserializeOwned; -use std::{any::type_name, convert::TryFrom, path::PathBuf}; +use std::{any::type_name, cell::RefCell, convert::TryFrom, path::PathBuf}; use tracing_chrome::{ChromeLayerBuilder, FlushGuard}; use tracing_subscriber::{filter, prelude::*, util::SubscriberInitExt, Layer}; @@ -125,17 +125,22 @@ pub fn init_custom_trace_subscriber(cx: CallContext) -> napi::Result .try_init() .expect("Failed to register tracing subscriber"); - cx.env.create_external(guard, None) + let guard_cell = RefCell::new(Some(guard)); + cx.env.create_external(guard_cell, None) } /// Teardown currently running tracing subscriber to flush out remaining traces. /// This should be called when parent node.js process exits, otherwise generated -/// trace will missing traces in the buffer. +/// trace may drop traces in the buffer. #[js_function(1)] pub fn teardown_trace_subscriber(cx: CallContext) -> napi::Result { let guard_external = cx.get::(0)?; - let guard = &*cx.env.get_value_external::(&guard_external)?; + let guard_cell = &*cx + .env + .get_value_external::>>(&guard_external)?; - guard.close(); + if let Some(guard) = guard_cell.take() { + drop(guard); + } cx.env.get_undefined() }