Unverified Commit 3def5ff9 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

backport: fix(turbopack): Store persistence of wrapped task on RawVc::LocalOutput (#78488) (#78883)

This is a straightforward backport of
https://github.com/vercel/next.js/pull/78488. There were only a few
minor merge conficts.

This *mostly* impacted use of Turbopack with persistent caching (which
is canary-gated), but we saw some reports of it without persistent
caching, so this backports the fix.

Tested with

```
cargo check
cargo test -p turbo-tasks-backend
cargo test -p turbo-tasks-memory
```
parent d0b2f8a8
...@@ -91,8 +91,8 @@ impl UpdateOutputOperation { ...@@ -91,8 +91,8 @@ impl UpdateOutputOperation {
cell, cell,
}) })
} }
Ok(Ok(RawVc::LocalOutput(_, _))) => { Ok(Ok(RawVc::LocalOutput(..))) => {
panic!("LocalOutput must not be output of a task"); panic!("Non-local tasks must not return a local Vc");
} }
Ok(Err(err)) => { Ok(Err(err)) => {
task.insert(CachedDataItem::Error { task.insert(CachedDataItem::Error {
......
../../turbo-tasks-testing/tests/transient_vc.rs
\ No newline at end of file
../../turbo-tasks-testing/tests/transient_vc.rs
\ No newline at end of file
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
use anyhow::Result;
use turbo_tasks::{TaskInput, TransientValue, Vc};
use turbo_tasks_testing::{register, run_without_cache_check, Registration};
static REGISTRATION: Registration = register!();
#[tokio::test]
async fn test_transient_vc() -> Result<()> {
run_without_cache_check(&REGISTRATION, async {
test_transient_operation(TransientValue::new(123))
.read_strongly_consistent()
.await?;
Ok(())
})
.await
}
#[turbo_tasks::function(operation)]
async fn test_transient_operation(transient_arg: TransientValue<i32>) -> Result<()> {
let called_with_transient = has_transient_arg(transient_arg);
let called_with_persistent = has_persistent_arg(123);
assert!(called_with_transient.is_transient());
assert!(!called_with_persistent.is_transient());
assert!(has_vc_arg(called_with_transient).is_transient());
assert!(!has_vc_arg(called_with_persistent).is_transient());
let called_with_transient_resolved = called_with_transient.to_resolved().await?;
let called_with_persistent_resolved = called_with_persistent.to_resolved().await?;
assert!(called_with_transient_resolved.is_transient());
assert!(!called_with_persistent_resolved.is_transient());
assert!(has_vc_arg(*called_with_transient_resolved).is_transient());
assert!(!has_vc_arg(*called_with_persistent_resolved).is_transient());
Ok(())
}
#[turbo_tasks::function]
fn has_transient_arg(arg: TransientValue<i32>) -> Vc<i32> {
Vc::cell(*arg)
}
#[turbo_tasks::function]
fn has_persistent_arg(arg: i32) -> Vc<i32> {
Vc::cell(arg)
}
#[turbo_tasks::function]
async fn has_vc_arg(arg: Vc<i32>) -> Result<Vc<i32>> {
Ok(Vc::cell(*arg.await?))
}
...@@ -306,7 +306,7 @@ pub struct UpdateInfo { ...@@ -306,7 +306,7 @@ pub struct UpdateInfo {
placeholder_for_future_fields: (), placeholder_for_future_fields: (),
} }
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum TaskPersistence { pub enum TaskPersistence {
/// Tasks that may be persisted across sessions using serialization. /// Tasks that may be persisted across sessions using serialization.
Persistent, Persistent,
...@@ -796,7 +796,7 @@ impl<B: Backend + 'static> TurboTasks<B> { ...@@ -796,7 +796,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
#[cfg(not(feature = "tokio_tracing"))] #[cfg(not(feature = "tokio_tracing"))]
tokio::task::spawn(future); tokio::task::spawn(future);
RawVc::LocalOutput(parent_task_id, local_task_id) RawVc::LocalOutput(parent_task_id, persistence, local_task_id)
} }
fn begin_primary_job(&self) { fn begin_primary_job(&self) {
......
...@@ -11,8 +11,8 @@ use crate::{ ...@@ -11,8 +11,8 @@ use crate::{
id::LocalTaskId, id::LocalTaskId,
manager::{read_local_output, read_task_cell, read_task_output, with_turbo_tasks}, manager::{read_local_output, read_task_cell, read_task_output, with_turbo_tasks},
registry::{self, get_value_type}, registry::{self, get_value_type},
turbo_tasks, CollectiblesSource, ReadCellOptions, ReadConsistency, TaskId, TraitTypeId, turbo_tasks, CollectiblesSource, ReadCellOptions, ReadConsistency, TaskId, TaskPersistence,
ValueType, ValueTypeId, Vc, VcValueTrait, TraitTypeId, ValueType, ValueTypeId, Vc, VcValueTrait,
}; };
#[derive(Error, Debug)] #[derive(Error, Debug)]
...@@ -57,31 +57,34 @@ impl Display for CellId { ...@@ -57,31 +57,34 @@ impl Display for CellId {
pub enum RawVc { pub enum RawVc {
TaskOutput(TaskId), TaskOutput(TaskId),
TaskCell(TaskId, CellId), TaskCell(TaskId, CellId),
LocalOutput(TaskId, LocalTaskId), LocalOutput(TaskId, TaskPersistence, LocalTaskId),
} }
impl RawVc { impl RawVc {
pub(crate) fn is_resolved(&self) -> bool { pub(crate) fn is_resolved(&self) -> bool {
match self { match self {
RawVc::TaskOutput(_) => false, RawVc::TaskOutput(..) => false,
RawVc::TaskCell(_, _) => true, RawVc::TaskCell(..) => true,
RawVc::LocalOutput(_, _) => false, RawVc::LocalOutput(..) => false,
} }
} }
pub(crate) fn is_local(&self) -> bool { pub(crate) fn is_local(&self) -> bool {
match self { match self {
RawVc::TaskOutput(_) => false, RawVc::TaskOutput(..) => false,
RawVc::TaskCell(_, _) => false, RawVc::TaskCell(..) => false,
RawVc::LocalOutput(_, _) => true, RawVc::LocalOutput(..) => true,
} }
} }
/// Returns `true` if the task this `RawVc` reads from cannot be serialized and will not be
/// stored in the persistent cache.
///
/// See [`TaskPersistence`] for more details.
pub fn is_transient(&self) -> bool { pub fn is_transient(&self) -> bool {
match self { match self {
RawVc::TaskOutput(task) | RawVc::TaskCell(task, _) | RawVc::LocalOutput(task, _) => { RawVc::TaskOutput(task) | RawVc::TaskCell(task, ..) => task.is_transient(),
task.is_transient() RawVc::LocalOutput(_, persistence, ..) => *persistence == TaskPersistence::Transient,
}
} }
} }
...@@ -145,7 +148,7 @@ impl RawVc { ...@@ -145,7 +148,7 @@ impl RawVc {
return Err(ResolveTypeError::NoContent); return Err(ResolveTypeError::NoContent);
} }
} }
RawVc::LocalOutput(task_id, local_task_id) => { RawVc::LocalOutput(task_id, _persistence, local_task_id) => {
current = read_local_output(&*tt, task_id, local_task_id) current = read_local_output(&*tt, task_id, local_task_id)
.await .await
.map_err(|source| ResolveTypeError::TaskError { source })?; .map_err(|source| ResolveTypeError::TaskError { source })?;
...@@ -182,7 +185,7 @@ impl RawVc { ...@@ -182,7 +185,7 @@ impl RawVc {
consistency = ReadConsistency::Eventual; consistency = ReadConsistency::Eventual;
} }
RawVc::TaskCell(_, _) => return Ok(current), RawVc::TaskCell(_, _) => return Ok(current),
RawVc::LocalOutput(task_id, local_task_id) => { RawVc::LocalOutput(task_id, _persistence, local_task_id) => {
debug_assert_eq!(consistency, ReadConsistency::Eventual); debug_assert_eq!(consistency, ReadConsistency::Eventual);
current = read_local_output(&*tt, task_id, local_task_id).await?; current = read_local_output(&*tt, task_id, local_task_id).await?;
} }
...@@ -197,7 +200,7 @@ impl RawVc { ...@@ -197,7 +200,7 @@ impl RawVc {
let mut current = self; let mut current = self;
loop { loop {
match current { match current {
RawVc::LocalOutput(task_id, local_task_id) => { RawVc::LocalOutput(task_id, _persistence, local_task_id) => {
current = read_local_output(&*tt, task_id, local_task_id).await?; current = read_local_output(&*tt, task_id, local_task_id).await?;
} }
non_local => return Ok(non_local), non_local => return Ok(non_local),
...@@ -212,7 +215,7 @@ impl RawVc { ...@@ -212,7 +215,7 @@ impl RawVc {
pub fn get_task_id(&self) -> TaskId { pub fn get_task_id(&self) -> TaskId {
match self { match self {
RawVc::TaskOutput(t) | RawVc::TaskCell(t, _) | RawVc::LocalOutput(t, _) => *t, RawVc::TaskOutput(t) | RawVc::TaskCell(t, _) | RawVc::LocalOutput(t, ..) => *t,
} }
} }
...@@ -347,7 +350,7 @@ impl Future for ReadRawVcFuture { ...@@ -347,7 +350,7 @@ impl Future for ReadRawVcFuture {
Err(err) => return Poll::Ready(Err(err)), Err(err) => return Poll::Ready(Err(err)),
} }
} }
RawVc::LocalOutput(task_id, local_output_id) => { RawVc::LocalOutput(task_id, _persistence, local_output_id) => {
debug_assert_eq!(this.consistency, ReadConsistency::Eventual); debug_assert_eq!(this.consistency, ReadConsistency::Eventual);
let read_result = tt.try_read_local_output(task_id, local_output_id); let read_result = tt.try_read_local_output(task_id, local_output_id);
match read_result { match read_result {
......
...@@ -141,7 +141,7 @@ where ...@@ -141,7 +141,7 @@ where
} }
fn is_transient(&self) -> bool { fn is_transient(&self) -> bool {
self.node.get_task_id().is_transient() self.node.is_transient()
} }
async fn resolve_input(&self) -> Result<Self> { async fn resolve_input(&self) -> Result<Self> {
......
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