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::{
fmt::Display,
sync::{Arc, LazyLock, RwLock},
sync::{Arc, LazyLock},
};
use anyhow::{Context, Result};
use serde::Serialize;
use turbo_rcstr::RcStr;
use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, TryJoinIterExt, Vc,
FxIndexMap, FxIndexSet, NonLocalValue, OperationValue, ReadRef, ResolvedVc, State,
TryJoinIterExt, Vc,
debug::ValueDebugFormat,
message_queue::{CompilationEvent, Severity},
trace::TraceRawVcs,
......@@ -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 {
entries: RwLock<FxIndexMap<RcStr, ResolvedVc<ServerHmrChunkLists>>>,
entries: State<ServerHmrEntries>,
}
impl PartialEq for ServerHmrEntryMap {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
impl ServerHmrEntryMap {
// This must not be a `#[turbo_tasks::function]`: each project container owns one map.
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>) {
self.entries
.write()
.expect("server HMR entry map lock poisoned")
.insert(entry_key, chunk_lists);
self.entries.update_conditionally(|entries| {
entries.0.insert(entry_key, chunk_lists) != Some(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>> {
self.entries
.read()
.expect("server HMR entry map lock poisoned")
.get(entry_key)
.copied()
self.entries.get_untracked().0.get(entry_key).copied()
}
/// Drops every registration, e.g. when project options change and the chunk lists they were
/// 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 {
NextRuntime::NodeJs => {
// For node, there will be exactly one asset in this
let rsc_chunk = *app_entry_chunks_ref.first().unwrap();
let app_server_root = server_path.join("app")?;
let mut server_hmr_chunks =
vec![ServerHmrChunkList::from_chunk_list(&app_server_root, rsc_chunk).await?];
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)
let server_hmr_chunks = if *project.server_hmr_enabled().await? {
let app_server_root = server_path.join("app")?;
let mut server_hmr_chunks = vec![
ServerHmrChunkList::from_chunk_list(&app_server_root, rsc_chunk).await?,
];
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?,
);
}
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 {
// create app paths manifest
......@@ -2185,7 +2193,7 @@ impl Endpoint for AppEndpoint {
let server_hmr_chunks = match *output.await? {
AppEndpointOutput::NodeJs {
server_hmr_chunks, ..
} => Some(server_hmr_chunks),
} => server_hmr_chunks,
AppEndpointOutput::Edge { .. } => None,
};
......@@ -2346,7 +2354,7 @@ enum AppEndpointOutput {
rsc_chunk: ResolvedVc<Box<dyn OutputAsset>>,
server_assets: ResolvedVc<OutputAssets>,
client_assets: ResolvedVc<OutputAssets>,
server_hmr_chunks: ResolvedVc<ServerHmrChunkLists>,
server_hmr_chunks: Option<ResolvedVc<ServerHmrChunkLists>>,
},
Edge {
files: ResolvedVc<OutputAssets>,
......
use std::{path::Path, sync::Arc, time::Duration};
use std::{path::Path, time::Duration};
use anyhow::{Context, Result, bail};
use async_trait::async_trait;
......@@ -437,13 +437,12 @@ pub struct Instrumentation {
pub edge: ResolvedVc<Box<dyn Endpoint>>,
}
#[turbo_tasks::value(serialization = "skip", evict = "never")]
#[turbo_tasks::value]
pub struct ProjectContainer {
name: RcStr,
options_state: State<Option<ProjectOptions>>,
versioned_content_map: Option<ResolvedVc<VersionedContentMap>>,
#[turbo_tasks(trace_ignore)]
server_hmr_entry_map: Arc<ServerHmrEntryMap>,
server_hmr_entry_map: ResolvedVc<ServerHmrEntryMap>,
}
#[turbo_tasks::value_impl]
......@@ -460,7 +459,7 @@ impl ProjectContainer {
None
},
options_state: State::new(None),
server_hmr_entry_map: Arc::new(ServerHmrEntryMap::default()),
server_hmr_entry_map: ServerHmrEntryMap::new(),
}
.cell())
}
......@@ -734,6 +733,10 @@ impl ProjectContainer {
.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));
let project = project_operation(self)
.resolve()
......@@ -841,7 +844,7 @@ impl ProjectContainer {
NextMode::Build.resolved_cell()
},
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,
encryption_key,
preview_props,
......@@ -885,7 +888,7 @@ impl ProjectContainer {
}
#[derive(Clone)]
#[turbo_tasks::value(serialization = "skip", evict = "never")]
#[turbo_tasks::value]
pub struct Project {
/// 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.
......@@ -897,8 +900,7 @@ pub struct Project {
/// E.g. `apps/my-app`
project_path: RcStr,
#[turbo_tasks(trace_ignore)]
server_hmr_entry_map: Arc<ServerHmrEntryMap>,
server_hmr_entry_map: ResolvedVc<ServerHmrEntryMap>,
/// A path where to emit the build outputs, relative to [`Project::project_path`], always a
/// Unix path. Corresponds to next.config.js's `distDir`.
......@@ -961,21 +963,28 @@ pub struct Project {
}
impl Project {
pub fn register_server_hmr_entry(
pub async fn register_server_hmr_entry(
&self,
entry_key: RcStr,
chunk_lists: Option<ResolvedVc<ServerHmrChunkLists>>,
) {
self.server_hmr_entry_map.set(
) -> Result<()> {
self.server_hmr_entry_map.await?.set(
entry_key,
chunk_lists.unwrap_or_else(|| ServerHmrChunkLists::new(vec![]).resolved_cell()),
);
Ok(())
}
pub fn server_hmr_chunk_lists(&self, entry_key: &str) -> ResolvedVc<ServerHmrChunkLists> {
self.server_hmr_entry_map
pub async fn server_hmr_chunk_lists(
&self,
entry_key: &str,
) -> Result<ReadRef<ServerHmrChunkLists>> {
let chunk_lists = self
.server_hmr_entry_map
.await?
.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 {
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]
pub(super) fn encryption_key(&self) -> Vc<RcStr> {
Vc::cell(self.encryption_key.clone())
......
......@@ -240,7 +240,8 @@ pub async fn endpoint_write_to_disk(
if let Some(entry_key) = entry_key {
project
.await?
.register_server_hmr_entry(entry_key, server_hmr_chunks);
.register_server_hmr_entry(entry_key, server_hmr_chunks)
.await?;
}
Ok(*output_paths)
......
......@@ -1857,7 +1857,7 @@ async fn hmr_update_with_issues_operation(
.cell())
}
#[turbo_tasks::value(serialization = "skip", evict = "never")]
#[turbo_tasks::value(serialization = "skip")]
struct ServerHmrSnapshotWithEffects {
chunk_lists: ReadRef<ServerHmrChunkLists>,
version: ReadRef<ServerHmrChunkListVersion>,
......@@ -1865,7 +1865,7 @@ struct ServerHmrSnapshotWithEffects {
effects: Arc<Effects>,
}
#[turbo_tasks::value(serialization = "skip", evict = "never")]
#[turbo_tasks::value(serialization = "skip")]
struct ServerHmrSnapshot {
chunk_lists: ReadRef<ServerHmrChunkLists>,
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