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

fix(next): read server HMR pull from one coherent operation

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 00161ce1
...@@ -7,7 +7,8 @@ use anyhow::Result; ...@@ -7,7 +7,8 @@ use anyhow::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, ReadRef, ResolvedVc, TransientInstance, TryJoinIterExt,
Vc,
debug::ValueDebugFormat, debug::ValueDebugFormat,
message_queue::{CompilationEvent, Severity}, message_queue::{CompilationEvent, Severity},
turbo_tasks, turbo_tasks,
...@@ -22,9 +23,7 @@ use turbopack_ecmascript::chunk_list::{ ...@@ -22,9 +23,7 @@ use turbopack_ecmascript::chunk_list::{
update::{ChunkListUpdate, ChunkUpdate, EcmascriptUpdateInstruction}, update::{ChunkListUpdate, ChunkUpdate, EcmascriptUpdateInstruction},
version::ChunkListVersion, version::ChunkListVersion,
}; };
use turbopack_nodejs::ecmascript::node::entry::chunk_list_content::{ use turbopack_nodejs::ecmascript::node::entry::chunk_list_content::EcmascriptBuildNodeChunkListContent;
EcmascriptBuildNodeChunkListContent, compute_update_from_version_operation,
};
#[derive(Clone, PartialEq, Eq, ValueDebugFormat, NonLocalValue)] #[derive(Clone, PartialEq, Eq, ValueDebugFormat, NonLocalValue)]
pub struct ServerHmrChunkList { pub struct ServerHmrChunkList {
...@@ -136,7 +135,7 @@ impl ChunkListUpdateBuilder { ...@@ -136,7 +135,7 @@ impl ChunkListUpdateBuilder {
} }
/// An update plus the baseline for the next pull. /// An update plus the baseline for the next pull.
#[derive(Debug)] #[derive(Debug, PartialEq, Eq, ValueDebugFormat, NonLocalValue)]
pub enum ServerHmrUpdate { pub enum ServerHmrUpdate {
/// No runtime update and the graph is equivalent. However, `to` may still advance the pull /// No runtime update and the graph is equivalent. However, `to` may still advance the pull
/// version. /// version.
...@@ -152,6 +151,32 @@ pub enum ServerHmrUpdate { ...@@ -152,6 +151,32 @@ pub enum ServerHmrUpdate {
}, },
} }
/// The baseline of one pull, split per chunk list.
///
/// It is created once per pull, outside of Turbo Tasks, and passed to the pull operation as a
/// [`TransientInstance`]. The per-chunk-list [`TransientInstance`]s key the diff tasks, so they
/// stay the same while the pull operation re-executes to settle, and they are never shared with
/// another pull.
#[derive(NonLocalValue)]
pub struct ServerHmrPullBaseline {
version: ReadRef<ServerHmrChunkListVersion>,
chunk_list_versions: FxIndexMap<RcStr, TransientInstance<ReadRef<ChunkListVersion>>>,
}
impl ServerHmrPullBaseline {
pub fn new(version: ReadRef<ServerHmrChunkListVersion>) -> Self {
let chunk_list_versions = version
.versions_by_chunk_list_path
.iter()
.map(|(path, version)| (path.clone(), TransientInstance::new(version.clone())))
.collect();
Self {
version,
chunk_list_versions,
}
}
}
struct DiffResult { struct DiffResult {
chunk_updates: Vec<ReadRef<Update>>, chunk_updates: Vec<ReadRef<Update>>,
membership: ChunkListMembershipChange, membership: ChunkListMembershipChange,
...@@ -199,15 +224,21 @@ impl CompilationEvent for ServerHmrChunkListDiffEvent { ...@@ -199,15 +224,21 @@ impl CompilationEvent for ServerHmrChunkListDiffEvent {
} }
} }
/// Diffs `chunk_lists` against the pull baseline `from`.
///
/// Must be called from the task that also produced `chunk_lists` (or that connected their
/// producer): the diff only reads the chunk-list contents, so a strongly consistent read covers
/// their producers only when they are part of the same call tree.
async fn diff_chunks_against( async fn diff_chunks_against(
chunk_lists: &[ServerHmrChunkList], chunk_lists: &[ServerHmrChunkList],
from: &ServerHmrChunkListVersion, from: &ServerHmrPullBaseline,
) -> Result<DiffResult> { ) -> Result<DiffResult> {
let current_chunk_list_paths = chunk_lists let current_chunk_list_paths = chunk_lists
.iter() .iter()
.map(|chunk_list| &chunk_list.relative_path) .map(|chunk_list| &chunk_list.relative_path)
.collect::<FxIndexSet<_>>(); .collect::<FxIndexSet<_>>();
let has_removed_chunk_lists = from let has_removed_chunk_lists = from
.version
.versions_by_chunk_list_path .versions_by_chunk_list_path
.keys() .keys()
.any(|path| !current_chunk_list_paths.contains(path)); .any(|path| !current_chunk_list_paths.contains(path));
...@@ -224,22 +255,14 @@ async fn diff_chunks_against( ...@@ -224,22 +255,14 @@ async fn diff_chunks_against(
chunk_list_path: relative_path.clone(), chunk_list_path: relative_path.clone(),
})); }));
} }
let Some(prev) = from.versions_by_chunk_list_path.get(relative_path).cloned() let Some(prev) = from.chunk_list_versions.get(relative_path).cloned() else {
else {
has_new_chunk_lists = true; has_new_chunk_lists = true;
return None; return None;
}; };
Some((*versioned_content, prev)) Some((*versioned_content, prev))
}, },
) )
.map(|(content, prev)| async move { .map(|(content, prev)| async move { content.compute_update_from_version(prev).await })
compute_update_from_version_operation(
content,
turbo_tasks::TransientInstance::new(prev),
)
.read_strongly_consistent()
.await
})
.try_join() .try_join()
.await?; .await?;
Ok(DiffResult { Ok(DiffResult {
...@@ -302,10 +325,17 @@ fn classify_server_hmr_update<'a>( ...@@ -302,10 +325,17 @@ fn classify_server_hmr_update<'a>(
} }
} }
/// Kept outside Turbo Tasks so old pull baselines cannot reactivate. /// Computes the update of a pull from its chunk lists, the pull baseline `from` and the version
/// `to` of `chunk_lists`.
///
/// Must be called from the pull operation that also produced `chunk_lists` and `to`, so that the
/// update, `to` and the applied effects all come from the same settled state (see
/// `diff_chunks_against`). That operation is keyed by the pull's [`TransientInstance`], so its
/// diff tasks become inactive once the pull's top-level task has finished and can't be re-executed
/// for an old baseline.
pub async fn compute_server_hmr_update( pub async fn compute_server_hmr_update(
chunk_lists: &[ServerHmrChunkList], chunk_lists: &[ServerHmrChunkList],
from: Option<&ServerHmrChunkListVersion>, from: Option<&ServerHmrPullBaseline>,
to: ReadRef<ServerHmrChunkListVersion>, to: ReadRef<ServerHmrChunkListVersion>,
) -> Result<ServerHmrUpdate> { ) -> Result<ServerHmrUpdate> {
if chunk_lists.is_empty() { if chunk_lists.is_empty() {
......
...@@ -20,7 +20,8 @@ use napi::{ ...@@ -20,7 +20,8 @@ use napi::{
use napi_derive::napi; use napi_derive::napi;
use next_api::{ use next_api::{
aggregate_hmr::{ aggregate_hmr::{
ServerHmrChunkListVersion, ServerHmrChunkLists, ServerHmrUpdate, compute_server_hmr_update, ServerHmrChunkListVersion, ServerHmrChunkLists, ServerHmrPullBaseline, ServerHmrUpdate,
compute_server_hmr_update,
}, },
entrypoints::Entrypoints, entrypoints::Entrypoints,
next_server_nft::next_server_nft_assets, next_server_nft::next_server_nft_assets,
...@@ -1975,7 +1976,9 @@ async fn project_server_hmr_snapshot_operation( ...@@ -1975,7 +1976,9 @@ async fn project_server_hmr_snapshot_operation(
.cell()) .cell())
} }
/// Snapshot only; diffing here would keep old baselines active. /// Snapshot only, keyed by `(project, entry_paths)` and shared by all pulls of these entries.
/// Diffing happens in [`server_hmr_update_with_effects_operation`], which is keyed per pull, so
/// that this shared operation never keeps an old pull baseline active.
#[tracing::instrument(level = "info", name = "server hmr snapshot", skip_all)] #[tracing::instrument(level = "info", name = "server hmr snapshot", skip_all)]
#[turbo_tasks::function(operation, root)] #[turbo_tasks::function(operation, root)]
async fn server_hmr_snapshot_with_effects_operation( async fn server_hmr_snapshot_with_effects_operation(
...@@ -2003,6 +2006,50 @@ async fn server_hmr_snapshot_with_effects_operation( ...@@ -2003,6 +2006,50 @@ async fn server_hmr_snapshot_with_effects_operation(
pub struct ServerHmrVersion(ReadRef<ServerHmrChunkListVersion>); pub struct ServerHmrVersion(ReadRef<ServerHmrChunkListVersion>);
#[turbo_tasks::value(serialization = "skip")]
struct ServerHmrUpdateWithEffects {
update: ServerHmrUpdate,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
effects: Arc<Effects>,
}
/// One server HMR pull: the snapshot (with its effects and issues) and the diff against the
/// pull's baseline, in a single call tree.
///
/// The diff only reads the chunk-list contents of the snapshot. Connecting the snapshot here puts
/// their producers into the call tree of this operation, so its strongly consistent read waits
/// until they settled, and the update, `to`, the issues and the effects all describe the same
/// state.
///
/// `from` is a new [`TransientInstance`] per pull, so this operation and its diff tasks are never
/// shared with another pull and become inactive once the pull's top-level task has finished; an
/// old baseline is never diffed again.
#[tracing::instrument(level = "info", name = "server hmr update", skip_all)]
#[turbo_tasks::function(operation, root)]
async fn server_hmr_update_with_effects_operation(
project: ResolvedVc<Project>,
entry_paths: Vec<RcStr>,
from: TransientInstance<Option<ServerHmrPullBaseline>>,
) -> Result<Vc<ServerHmrUpdateWithEffects>> {
let snapshot = server_hmr_snapshot_with_effects_operation(project, entry_paths)
.connect()
.await?;
let ServerHmrSnapshotWithEffects {
chunk_lists,
version,
issues,
effects,
} = &*snapshot;
let update =
compute_server_hmr_update(chunk_lists.as_slice(), from.as_ref(), version.clone()).await?;
Ok(ServerHmrUpdateWithEffects {
update,
issues: issues.clone(),
effects: effects.clone(),
}
.cell())
}
#[napi(object, object_from_js = false)] #[napi(object, object_from_js = false)]
pub struct NapiServerHmrUpdate { pub struct NapiServerHmrUpdate {
#[napi(ts_type = "\"none\" | \"partial\" | \"restart\"")] #[napi(ts_type = "\"none\" | \"partial\" | \"restart\"")]
...@@ -2041,46 +2088,26 @@ pub async fn project_get_server_hmr_update( ...@@ -2041,46 +2088,26 @@ pub async fn project_get_server_hmr_update(
) -> napi::Result<TurbopackResult<NapiServerHmrUpdate>> { ) -> napi::Result<TurbopackResult<NapiServerHmrUpdate>> {
let container = project.container; let container = project.container;
let turbo_tasks = project.turbopack_ctx.turbo_tasks(); let turbo_tasks = project.turbopack_ctx.turbo_tasks();
let from = from.map(|from| from.0.clone()); let from = TransientInstance::new(from.map(|from| ServerHmrPullBaseline::new(from.0.clone())));
let (project, read) = turbo_tasks let read = turbo_tasks
.run(async move { .run(async move {
// HACK(bgw): Remove this unmark call // HACK(bgw): Remove this unmark call
unmark_top_level_task_may_leak_eventually_consistent_state(); unmark_top_level_task_may_leak_eventually_consistent_state();
let project = container.project().to_resolved().await?; let project = container.project().to_resolved().await?;
// HACK(bgw): Remove this mark call // HACK(bgw): Remove this mark call
mark_top_level_task(); mark_top_level_task();
let snapshot_op = server_hmr_snapshot_with_effects_operation(project, entry_paths); let update_op = server_hmr_update_with_effects_operation(project, entry_paths, from);
let read = read_strongly_consistent_and_apply_effects(update_op, |v| &v.effects).await
read_strongly_consistent_and_apply_effects(snapshot_op, |v| &v.effects).await?;
Ok((project, read))
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e.into()).to_string()))?;
// Diffing must not remain active after the pull completes.
let (update, issues) = turbo_tasks
.run(async move {
// The snapshot's chunk-list `Vc`s are only valid while the project is held.
let _project_keep_alive = project;
let ServerHmrSnapshotWithEffects {
chunk_lists,
version,
issues,
..
} = &*read;
let update =
compute_server_hmr_update(chunk_lists.as_slice(), from.as_deref(), version.clone())
.await?;
Ok::<_, anyhow::Error>((update, issues.clone()))
}) })
.await .await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e.into()).to_string()))?; .map_err(|e| napi::Error::from_reason(PrettyPrintError(&e.into()).to_string()))?;
Ok(TurbopackResult { Ok(TurbopackResult {
result: NapiServerHmrUpdate::new(&update) result: NapiServerHmrUpdate::new(&read.update)
.map_err(|error| napi::Error::from_reason(PrettyPrintError(&error).to_string()))?, .map_err(|error| napi::Error::from_reason(PrettyPrintError(&error).to_string()))?,
issues: issues issues: read
.issues
.iter() .iter()
.map(|issue| NapiIssue::from(&**issue)) .map(|issue| NapiIssue::from(&**issue))
.collect(), .collect(),
......
...@@ -157,11 +157,3 @@ impl VersionedContent for EcmascriptBuildNodeChunkListContent { ...@@ -157,11 +157,3 @@ impl VersionedContent for EcmascriptBuildNodeChunkListContent {
update_chunk_list(&this.chunks_contents, to_version, from_version).await update_chunk_list(&this.chunks_contents, to_version, from_version).await
} }
} }
#[turbo_tasks::function(operation, root)]
pub fn compute_update_from_version_operation(
content: ResolvedVc<EcmascriptBuildNodeChunkListContent>,
from: TransientInstance<ReadRef<ChunkListVersion>>,
) -> Vc<Update> {
content.compute_update_from_version(from)
}
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