Unverified Commit 0ab66e9a authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Fix server HMR entry map persistence

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 50d95098
use std::{ use std::{
fmt::Display, fmt::Display,
sync::{Arc, LazyLock, RwLock}, sync::{Arc, LazyLock},
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use serde::Serialize; use serde::Serialize;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, TryJoinIterExt, Vc, FxIndexMap, FxIndexSet, NonLocalValue, OperationValue, ReadRef, ResolvedVc, State,
TryJoinIterExt, Vc,
debug::ValueDebugFormat, debug::ValueDebugFormat,
message_queue::{CompilationEvent, Severity}, message_queue::{CompilationEvent, Severity},
trace::TraceRawVcs, trace::TraceRawVcs,
...@@ -75,33 +76,55 @@ impl ServerHmrChunkLists { ...@@ -75,33 +76,55 @@ impl ServerHmrChunkLists {
} }
} }
#[derive(Debug, Default)] #[derive(Clone, Default, TraceRawVcs, PartialEq, Eq, ValueDebugFormat, Debug, NonLocalValue)]
struct ServerHmrEntries(FxIndexMap<RcStr, ResolvedVc<ServerHmrChunkLists>>);
// HACK: This is safe for this session-only map because the referenced chunk lists are never
// serialized and the owning value is never evicted within the session.
unsafe impl OperationValue for ServerHmrEntries {}
/// Tracks the server HMR chunk lists owned by each route entry, keyed by its entry key.
///
/// Ownership is registered imperatively when an endpoint is written, so this is a session-scoped
/// value rather than task output: `serialization = "skip"` keeps a restored session from holding
/// references to the previous session's chunk lists, and `evict = "never"` keeps the registered
/// ownership alive for the whole session. Keeping this state here (instead of inline in
/// [`crate::project::Project`]) is what lets `Project` itself stay serializable and cacheable.
#[turbo_tasks::value(serialization = "skip", evict = "never")]
pub struct ServerHmrEntryMap { pub struct ServerHmrEntryMap {
entries: RwLock<FxIndexMap<RcStr, ResolvedVc<ServerHmrChunkLists>>>, entries: State<ServerHmrEntries>,
} }
impl PartialEq for ServerHmrEntryMap { impl ServerHmrEntryMap {
fn eq(&self, other: &Self) -> bool { // This must not be a `#[turbo_tasks::function]`: each project container owns one map.
std::ptr::eq(self, other) pub fn new() -> ResolvedVc<Self> {
Self {
entries: State::new(ServerHmrEntries::default()),
}
.resolved_cell()
} }
}
impl Eq for ServerHmrEntryMap {}
impl ServerHmrEntryMap { /// Records the chunk lists owned by `entry_key`, replacing any previous registration.
pub fn set(&self, entry_key: RcStr, chunk_lists: ResolvedVc<ServerHmrChunkLists>) { pub fn set(&self, entry_key: RcStr, chunk_lists: ResolvedVc<ServerHmrChunkLists>) {
self.entries self.entries.update_conditionally(|entries| {
.write() entries.0.insert(entry_key, chunk_lists) != Some(chunk_lists)
.expect("server HMR entry map lock poisoned") });
.insert(entry_key, chunk_lists);
} }
/// Reads the chunk lists owned by `entry_key`. Untracked: pulls are driven by the endpoint
/// write that registered them, not by task invalidation.
pub fn get(&self, entry_key: &str) -> Option<ResolvedVc<ServerHmrChunkLists>> { pub fn get(&self, entry_key: &str) -> Option<ResolvedVc<ServerHmrChunkLists>> {
self.entries self.entries.get_untracked().0.get(entry_key).copied()
.read() }
.expect("server HMR entry map lock poisoned")
.get(entry_key) /// Drops every registration, e.g. when project options change and the chunk lists they were
.copied() /// built from no longer describe the project.
pub fn clear(&self) {
self.entries.update_conditionally(|entries| {
let was_populated = !entries.0.is_empty();
entries.0.clear();
was_populated
});
} }
} }
......
...@@ -1785,16 +1785,24 @@ impl AppEndpoint { ...@@ -1785,16 +1785,24 @@ impl AppEndpoint {
NextRuntime::NodeJs => { NextRuntime::NodeJs => {
// For node, there will be exactly one asset in this // For node, there will be exactly one asset in this
let rsc_chunk = *app_entry_chunks_ref.first().unwrap(); let rsc_chunk = *app_entry_chunks_ref.first().unwrap();
let app_server_root = server_path.join("app")?; let server_hmr_chunks = if *project.server_hmr_enabled().await? {
let mut server_hmr_chunks = let app_server_root = server_path.join("app")?;
vec![ServerHmrChunkList::from_chunk_list(&app_server_root, rsc_chunk).await?]; let mut server_hmr_chunks = vec![
if let Some(ssr_hmr_chunk_list) = ssr_hmr_chunk_list { ServerHmrChunkList::from_chunk_list(&app_server_root, rsc_chunk).await?,
server_hmr_chunks.push( ];
ServerHmrChunkList::from_chunk_list(&app_server_root, ssr_hmr_chunk_list) if let Some(ssr_hmr_chunk_list) = ssr_hmr_chunk_list {
server_hmr_chunks.push(
ServerHmrChunkList::from_chunk_list(
&app_server_root,
ssr_hmr_chunk_list,
)
.await?, .await?,
); );
} }
let server_hmr_chunks = ServerHmrChunkLists::new(server_hmr_chunks).resolved_cell(); Some(ServerHmrChunkLists::new(server_hmr_chunks).resolved_cell())
} else {
None
};
if emit_manifests != EmitManifests::None { if emit_manifests != EmitManifests::None {
// create app paths manifest // create app paths manifest
...@@ -2185,7 +2193,7 @@ impl Endpoint for AppEndpoint { ...@@ -2185,7 +2193,7 @@ impl Endpoint for AppEndpoint {
let server_hmr_chunks = match *output.await? { let server_hmr_chunks = match *output.await? {
AppEndpointOutput::NodeJs { AppEndpointOutput::NodeJs {
server_hmr_chunks, .. server_hmr_chunks, ..
} => Some(server_hmr_chunks), } => server_hmr_chunks,
AppEndpointOutput::Edge { .. } => None, AppEndpointOutput::Edge { .. } => None,
}; };
...@@ -2346,7 +2354,7 @@ enum AppEndpointOutput { ...@@ -2346,7 +2354,7 @@ enum AppEndpointOutput {
rsc_chunk: ResolvedVc<Box<dyn OutputAsset>>, rsc_chunk: ResolvedVc<Box<dyn OutputAsset>>,
server_assets: ResolvedVc<OutputAssets>, server_assets: ResolvedVc<OutputAssets>,
client_assets: ResolvedVc<OutputAssets>, client_assets: ResolvedVc<OutputAssets>,
server_hmr_chunks: ResolvedVc<ServerHmrChunkLists>, server_hmr_chunks: Option<ResolvedVc<ServerHmrChunkLists>>,
}, },
Edge { Edge {
files: ResolvedVc<OutputAssets>, files: ResolvedVc<OutputAssets>,
......
use std::{path::Path, sync::Arc, time::Duration}; use std::{path::Path, time::Duration};
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use async_trait::async_trait; use async_trait::async_trait;
...@@ -437,13 +437,12 @@ pub struct Instrumentation { ...@@ -437,13 +437,12 @@ pub struct Instrumentation {
pub edge: ResolvedVc<Box<dyn Endpoint>>, pub edge: ResolvedVc<Box<dyn Endpoint>>,
} }
#[turbo_tasks::value(serialization = "skip", evict = "never")] #[turbo_tasks::value]
pub struct ProjectContainer { pub struct ProjectContainer {
name: RcStr, name: RcStr,
options_state: State<Option<ProjectOptions>>, options_state: State<Option<ProjectOptions>>,
versioned_content_map: Option<ResolvedVc<VersionedContentMap>>, versioned_content_map: Option<ResolvedVc<VersionedContentMap>>,
#[turbo_tasks(trace_ignore)] server_hmr_entry_map: ResolvedVc<ServerHmrEntryMap>,
server_hmr_entry_map: Arc<ServerHmrEntryMap>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -460,7 +459,7 @@ impl ProjectContainer { ...@@ -460,7 +459,7 @@ impl ProjectContainer {
None None
}, },
options_state: State::new(None), options_state: State::new(None),
server_hmr_entry_map: Arc::new(ServerHmrEntryMap::default()), server_hmr_entry_map: ServerHmrEntryMap::new(),
} }
.cell()) .cell())
} }
...@@ -734,6 +733,10 @@ impl ProjectContainer { ...@@ -734,6 +733,10 @@ impl ProjectContainer {
.as_str(), .as_str(),
); );
} }
// Registered entries hold chunk lists from the current project options. Drop them
// before invalidating the project so a config or environment update cannot expose
// stale ownership while the affected endpoints are rebuilt.
this.server_hmr_entry_map.await?.clear();
this.options_state.set(Some(new_options)); this.options_state.set(Some(new_options));
let project = project_operation(self) let project = project_operation(self)
.resolve() .resolve()
...@@ -841,7 +844,7 @@ impl ProjectContainer { ...@@ -841,7 +844,7 @@ impl ProjectContainer {
NextMode::Build.resolved_cell() NextMode::Build.resolved_cell()
}, },
versioned_content_map: self.versioned_content_map, versioned_content_map: self.versioned_content_map,
server_hmr_entry_map: self.server_hmr_entry_map.clone(), server_hmr_entry_map: self.server_hmr_entry_map,
build_id, build_id,
encryption_key, encryption_key,
preview_props, preview_props,
...@@ -885,7 +888,7 @@ impl ProjectContainer { ...@@ -885,7 +888,7 @@ impl ProjectContainer {
} }
#[derive(Clone)] #[derive(Clone)]
#[turbo_tasks::value(serialization = "skip", evict = "never")] #[turbo_tasks::value]
pub struct Project { pub struct Project {
/// An absolute root path (Windows or Unix path) from which all files must be nested under. /// An absolute root path (Windows or Unix path) from which all files must be nested under.
/// Trying to access a file outside this root will fail, so think of this as a chroot. /// Trying to access a file outside this root will fail, so think of this as a chroot.
...@@ -897,8 +900,7 @@ pub struct Project { ...@@ -897,8 +900,7 @@ pub struct Project {
/// E.g. `apps/my-app` /// E.g. `apps/my-app`
project_path: RcStr, project_path: RcStr,
#[turbo_tasks(trace_ignore)] server_hmr_entry_map: ResolvedVc<ServerHmrEntryMap>,
server_hmr_entry_map: Arc<ServerHmrEntryMap>,
/// A path where to emit the build outputs, relative to [`Project::project_path`], always a /// A path where to emit the build outputs, relative to [`Project::project_path`], always a
/// Unix path. Corresponds to next.config.js's `distDir`. /// Unix path. Corresponds to next.config.js's `distDir`.
...@@ -961,21 +963,28 @@ pub struct Project { ...@@ -961,21 +963,28 @@ pub struct Project {
} }
impl Project { impl Project {
pub fn register_server_hmr_entry( pub async fn register_server_hmr_entry(
&self, &self,
entry_key: RcStr, entry_key: RcStr,
chunk_lists: Option<ResolvedVc<ServerHmrChunkLists>>, chunk_lists: Option<ResolvedVc<ServerHmrChunkLists>>,
) { ) -> Result<()> {
self.server_hmr_entry_map.set( self.server_hmr_entry_map.await?.set(
entry_key, entry_key,
chunk_lists.unwrap_or_else(|| ServerHmrChunkLists::new(vec![]).resolved_cell()), chunk_lists.unwrap_or_else(|| ServerHmrChunkLists::new(vec![]).resolved_cell()),
); );
Ok(())
} }
pub fn server_hmr_chunk_lists(&self, entry_key: &str) -> ResolvedVc<ServerHmrChunkLists> { pub async fn server_hmr_chunk_lists(
self.server_hmr_entry_map &self,
entry_key: &str,
) -> Result<ReadRef<ServerHmrChunkLists>> {
let chunk_lists = self
.server_hmr_entry_map
.await?
.get(entry_key) .get(entry_key)
.unwrap_or_else(|| ServerHmrChunkLists::new(vec![]).resolved_cell()) .unwrap_or_else(|| ServerHmrChunkLists::new(vec![]).resolved_cell());
Ok(chunk_lists.await?)
} }
} }
...@@ -1237,6 +1246,11 @@ impl Project { ...@@ -1237,6 +1246,11 @@ impl Project {
Ok(Vc::cell(*self.mode.await? == NextMode::Development)) Ok(Vc::cell(*self.mode.await? == NextMode::Development))
} }
#[turbo_tasks::function]
pub(super) fn server_hmr_enabled(&self) -> Vc<bool> {
Vc::cell(self.server_hmr)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn encryption_key(&self) -> Vc<RcStr> { pub(super) fn encryption_key(&self) -> Vc<RcStr> {
Vc::cell(self.encryption_key.clone()) Vc::cell(self.encryption_key.clone())
......
...@@ -240,7 +240,8 @@ pub async fn endpoint_write_to_disk( ...@@ -240,7 +240,8 @@ pub async fn endpoint_write_to_disk(
if let Some(entry_key) = entry_key { if let Some(entry_key) = entry_key {
project project
.await? .await?
.register_server_hmr_entry(entry_key, server_hmr_chunks); .register_server_hmr_entry(entry_key, server_hmr_chunks)
.await?;
} }
Ok(*output_paths) Ok(*output_paths)
......
...@@ -1857,7 +1857,7 @@ async fn hmr_update_with_issues_operation( ...@@ -1857,7 +1857,7 @@ async fn hmr_update_with_issues_operation(
.cell()) .cell())
} }
#[turbo_tasks::value(serialization = "skip", evict = "never")] #[turbo_tasks::value(serialization = "skip")]
struct ServerHmrSnapshotWithEffects { struct ServerHmrSnapshotWithEffects {
chunk_lists: ReadRef<ServerHmrChunkLists>, chunk_lists: ReadRef<ServerHmrChunkLists>,
version: ReadRef<ServerHmrChunkListVersion>, version: ReadRef<ServerHmrChunkListVersion>,
...@@ -1865,7 +1865,7 @@ struct ServerHmrSnapshotWithEffects { ...@@ -1865,7 +1865,7 @@ struct ServerHmrSnapshotWithEffects {
effects: Arc<Effects>, effects: Arc<Effects>,
} }
#[turbo_tasks::value(serialization = "skip", evict = "never")] #[turbo_tasks::value(serialization = "skip")]
struct ServerHmrSnapshot { struct ServerHmrSnapshot {
chunk_lists: ReadRef<ServerHmrChunkLists>, chunk_lists: ReadRef<ServerHmrChunkLists>,
version: ReadRef<ServerHmrChunkListVersion>, version: ReadRef<ServerHmrChunkListVersion>,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment