Unverified Commit 804b9a0b authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Enable Effects to be evicted (#94173)

### What?
Enable Effects to be evicted.

Currently `EffectInstance` transitively holds a
`ReadRef<PersistentFileContent>` which means eviction cannot not free
the memory for the outputs. Because the `ReadRefs` are captured by the
task producing the `Effect` we cannot just drop them (otherwise we could
need to somehow re-execute the write to recover them). So instead we
need a new 'effect processing phase'

Here is our new state machine:
 * Phase 1: `emit` effects.
- At this point we capture the content as a `ResolvedVc` and the hash as
a value.
- In principle this means the hash and content can get out of sync, but
only within an eventual consistency race which callers should be
handling.
  * Phase 2: `take_effects`.
- This will call `capture()` on all the effects which will conditionally
read the content cell if the hash doesn't match the `EffectStateStorage`
  * Phase 3: `Effects::apply`.
- This will apply the effects for all effects whose hashes don't match
storage.
- At this point we may discover that a hash in storage changed between
`capture` and `apply` which will trigger a special `Retry` error and
`invalidate` `Phase 2`.

To facilitate the `Retry` logic a
`read_strongly_consistent_and_apply_effects` helper function handles it.
Which allows for a simple circuit breaker and some verbose
warnings/errors when we retry too many times.

Specifically:
- Split the `Effect` trait into `Effect` (emit-side, returns a
`Captured` payload) and `CapturedEffect` (apply-side). `
- Switch `Effects` to `cell = "new"`. Every producer re-execution
allocates a fresh cell value; the prior cell (and its captured payload)
drops on overwrite, cascade-releasing upstream
`ReadRef<PersistedFileContent>` strong counts. The captured payload also
drops naturally when the cell is evicted.
- Push the per-key state machine (`Unapplied` / `InProgress` / `Applied
{ value_hash, result }`, `EventGuard` panic recovery, `Event` listener
coordination) down into `EffectStateStorage::run_apply`.
`Effects::apply` becomes a thin aggregator over `dyn_apply()` calls.
- New `ApplyOutcome<E> { Failed(E), Retry }`. `CapturedEffect::apply`
returns this; `Retry` signals "capture elided content but storage
diverged before apply" — `Effects::apply` collects across the batch,
fires the producer's invalidator once, and surfaces
`EffectsError::Retry`.
- New `EffectStateStorage::matches_applied(key, hash)`.
`WriteEffect::capture` / `WriteLinkEffect::capture` consult this and
skip the `ReadRef<PersistedFileContent>` / `ReadRef<LinkContent>`
materialization when storage already holds `Applied { matching, Ok(())
}` — avoiding the disk-read + decompression hit on producer re-runs that
don't actually change output.
- 10 integration tests in
`turbopack/crates/turbo-tasks-backend/tests/effects.rs` cover the new
lifecycle: duplicate apply, sibling stomp re-apply, repeated apply with
unchanged state dedupe, `cell = "new"` producing distinct cells per
producer run, capture-skip on storage match, and the
capture-skip-then-stomp race that exercises the `Retry` path.

### Why?

A memory audit showed `EffectInstance` cells accumulating the majority
of the ram in an eviction session

#### Capture-time storage check (the perf story)

`Effect::capture` may consult `EffectStateStorage::matches_applied` and
elide the content materialization when storage already records the
target hash. `CapturedWriteEffect.content` becomes
`Option<ReadRef<PersistedFileContent>>` — `None` means "capture observed
`Applied { matching }`; no `body` to pass to `run_apply`". The
apply-side state machine then either dedup-hits (storage still matches →
cached `Ok`) or enters the no-body branch and returns
`ApplyOutcome::Retry`.

Reading shared mutable `EffectStateStorage` from inside a turbo-tasks
task is normally suspect (the state isn't a tracked input). It is sound
here because the apply-time re-check fires the producer's invalidator on
mismatch, turning the otherwise-untracked read into an
explicitly-tracked one via the `Retry` pathway. The producer reruns,
`capture` sees the new storage state, materializes content, and the next
`apply` succeeds.

#### `ApplyOutcome::Retry` and the invalidator

`Effects::apply` collects `Retry` signals across the parallel batch via
`AtomicBool`. `Failed(e)` errors fail-fast. After the batch, if any
`Retry` fired (and no `Failed`), the producer's invalidator runs once
and `EffectsError::Retry` propagates. Callers re-read the operation; the
producer reruns; fresh `capture` either materializes content (storage
diverged) or dedup-hits cleanly.

#### `turbo-tasks-fs`

`WriteEffect::capture` / `WriteLinkEffect::capture` call
`matches_applied` first and conditionally `.await?` the content
`ResolvedVc`. `CapturedWriteEffect::apply` /
`CapturedWriteLinkEffect::apply` dispatch through `run_apply(key, hash,
body)`, where `body` is `Some(closure)` iff content is `Some`.
`apply_inner` takes the unwrapped `&ReadRef<…>` as a parameter so the
option-unwrap is at the dispatch boundary (type-enforced).

### Risk: infinite retry loop

**Failure mode.** Two sibling producers A and B contend on the same key
with different hashes (e.g. two routes that both want to write
`chunks/foo.js` with different content). Each `Retry` from A's apply
invalidates A's producer; each `Retry` from B's apply invalidates B's
producer. In the worst case, A and B perpetually re-capture, race to
apply, stomp each other's storage state, and re-trigger Retry —
invalidating themselves on each cycle.

**Why we believe it terminates in practice.** A `Retry` only fires when
(a) capture observed `Applied { matching }` and elided content, and (b)
by apply time storage was stomped to a different hash. Each `Retry`
invalidates only its own producer (no cross-producer invalidation), so
without external input churn the system reaches a fixed point —
whichever producer applies last wins, and subsequent re-runs of the
loser's producer materialize content (because storage no longer matches)
and apply cleanly. The pathological case requires a real, ongoing race
fundamentally triggered by external invalidations (file edits).


<!-- NEXT_JS_LLM_PR -->
parent a51f3556
...@@ -9847,6 +9847,7 @@ name = "turbo-tasks-backend" ...@@ -9847,6 +9847,7 @@ name = "turbo-tasks-backend"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait",
"auto-hash-map", "auto-hash-map",
"bincode 2.0.1", "bincode 2.0.1",
"bitfield", "bitfield",
...@@ -9869,6 +9870,7 @@ dependencies = [ ...@@ -9869,6 +9870,7 @@ dependencies = [
"serde_path_to_error", "serde_path_to_error",
"smallvec", "smallvec",
"tempfile", "tempfile",
"thiserror 1.0.69",
"thread_local", "thread_local",
"tokio", "tokio",
"tracing", "tracing",
...@@ -9948,6 +9950,7 @@ name = "turbo-tasks-fs" ...@@ -9948,6 +9950,7 @@ name = "turbo-tasks-fs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait",
"auto-hash-map", "auto-hash-map",
"bincode 2.0.1", "bincode 2.0.1",
"bitflags 1.3.2", "bitflags 1.3.2",
......
...@@ -13,7 +13,8 @@ use next_api::{ ...@@ -13,7 +13,8 @@ use next_api::{
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Effects, ReadConsistency, ReadRef, ResolvedVc, TransientInstance, TurboTasks, Vc, take_effects, Effects, ReadConsistency, ReadRef, ResolvedVc, TransientInstance, TurboTasks, Vc,
read_strongly_consistent_and_apply_effects, take_effects,
}; };
use turbo_tasks_backend::TurboTasksBackend; use turbo_tasks_backend::TurboTasksBackend;
use turbo_tasks_malloc::TurboMalloc; use turbo_tasks_malloc::TurboMalloc;
...@@ -275,13 +276,9 @@ async fn endpoint_write_to_disk_with_apply( ...@@ -275,13 +276,9 @@ async fn endpoint_write_to_disk_with_apply(
} }
let op = inner_operation_with_effects(endpoint); let op = inner_operation_with_effects(endpoint);
let WithEffects { let read = read_strongly_consistent_and_apply_effects(op, |v| &v.effects).await?;
output_paths,
effects,
} = &*op.read_strongly_consistent().await?;
effects.apply().await?;
Ok(output_paths.clone()) Ok(read.output_paths.clone())
} }
async fn hmr( async fn hmr(
......
...@@ -14,7 +14,9 @@ use next_api::{ ...@@ -14,7 +14,9 @@ use next_api::{
}; };
use tracing::Instrument; use tracing::Instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{Completion, Effects, OperationVc, ReadRef, Vc}; use turbo_tasks::{
Completion, Effects, OperationVc, ReadRef, Vc, read_strongly_consistent_and_apply_effects,
};
use turbopack_core::issue::{IssueFilter, PlainIssue}; use turbopack_core::issue::{IssueFilter, PlainIssue};
use crate::next_api::utils::{ use crate::next_api::utils::{
...@@ -156,14 +158,14 @@ pub async fn endpoint_write_to_disk( ...@@ -156,14 +158,14 @@ pub async fn endpoint_write_to_disk(
.run(async move { .run(async move {
let written_entrypoint_with_issues_op = let written_entrypoint_with_issues_op =
get_written_endpoint_with_issues_operation(endpoint_op); get_written_endpoint_with_issues_operation(endpoint_op);
let read = read_strongly_consistent_and_apply_effects(
written_entrypoint_with_issues_op,
|v| &v.effects,
)
.await?;
let WrittenEndpointWithIssues { let WrittenEndpointWithIssues {
written, written, issues, ..
issues, } = &*read;
effects,
} = &*written_entrypoint_with_issues_op
.read_strongly_consistent()
.await?;
effects.apply().await?;
Ok((written.clone(), issues.clone())) Ok((written.clone(), issues.clone()))
}) })
...@@ -190,8 +192,9 @@ pub fn endpoint_server_changed_subscribe( ...@@ -190,8 +192,9 @@ pub fn endpoint_server_changed_subscribe(
move || { move || {
async move { async move {
let issues_and_diags_op = subscribe_issues_and_diags_operation(endpoint, issues); let issues_and_diags_op = subscribe_issues_and_diags_operation(endpoint, issues);
let result = issues_and_diags_op.read_strongly_consistent().await?; let result =
result.effects.apply().await?; read_strongly_consistent_and_apply_effects(issues_and_diags_op, |v| &v.effects)
.await?;
Ok(result) Ok(result)
} }
.instrument(tracing::info_span!("server changes subscription")) .instrument(tracing::info_span!("server changes subscription"))
......
...@@ -51,7 +51,7 @@ use turbo_tasks::{ ...@@ -51,7 +51,7 @@ use turbo_tasks::{
TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi, UpdateInfo, Vc, TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi, UpdateInfo, Vc,
mark_top_level_task, mark_top_level_task,
message_queue::{CompilationEvent, Severity}, message_queue::{CompilationEvent, Severity},
take_effects, read_strongly_consistent_and_apply_effects, take_effects,
trace::TraceRawVcs, trace::TraceRawVcs,
unmark_top_level_task_may_leak_eventually_consistent_state, unmark_top_level_task_may_leak_eventually_consistent_state,
}; };
...@@ -1403,17 +1403,16 @@ pub async fn project_write_all_entrypoints_to_disk( ...@@ -1403,17 +1403,16 @@ pub async fn project_write_all_entrypoints_to_disk(
first_phase, first_phase,
); );
// Read and compile the files let read =
read_strongly_consistent_and_apply_effects(entrypoints_with_issues_op, |v| {
&v.effects
})
.await?;
let AllWrittenEntrypointsWithIssues { let AllWrittenEntrypointsWithIssues {
entrypoints, entrypoints,
issues, issues,
effects, ..
} = &*entrypoints_with_issues_op } = &*read;
.read_strongly_consistent()
.await?;
// Apply phase side effects. Asset emission is performed once at the end.
effects.apply().await?;
Ok(( Ok((
entrypoints.clone(), entrypoints.clone(),
...@@ -1468,16 +1467,16 @@ pub async fn project_write_all_entrypoints_to_disk( ...@@ -1468,16 +1467,16 @@ pub async fn project_write_all_entrypoints_to_disk(
EntrypointsWritePhase::Deferred, EntrypointsWritePhase::Deferred,
); );
let read =
read_strongly_consistent_and_apply_effects(entrypoints_with_issues_op, |v| {
&v.effects
})
.await?;
let AllWrittenEntrypointsWithIssues { let AllWrittenEntrypointsWithIssues {
entrypoints, entrypoints,
issues, issues,
effects, ..
} = &*entrypoints_with_issues_op } = &*read;
.read_strongly_consistent()
.await?;
// Apply phase side effects. Asset emission is performed once at the end.
effects.apply().await?;
Ok(( Ok((
entrypoints.clone(), entrypoints.clone(),
...@@ -1500,17 +1499,16 @@ pub async fn project_write_all_entrypoints_to_disk( ...@@ -1500,17 +1499,16 @@ pub async fn project_write_all_entrypoints_to_disk(
app_dir_only, app_dir_only,
has_deferred_entrypoints, has_deferred_entrypoints,
); );
let OperationResult { issues, effects } = let read =
&*emit_result_op.read_strongly_consistent().await?; read_strongly_consistent_and_apply_effects(emit_result_op, |v| &v.effects).await?;
let OperationResult { issues, .. } = &*read;
effects.apply().await?; Ok(issues.clone())
Ok(issues.iter().cloned().collect::<Vec<_>>())
}) })
.or_else(|e| ctx.throw_turbopack_internal_result(&e.into())) .or_else(|e| ctx.throw_turbopack_internal_result(&e.into()))
.await?; .await?;
issues.extend(emit_issues); issues.extend(emit_issues.iter().cloned());
Ok(TurbopackResult { Ok(TurbopackResult {
result: if let Some(entrypoints) = entrypoints { result: if let Some(entrypoints) = entrypoints {
...@@ -1742,15 +1740,16 @@ pub fn project_entrypoints_subscribe( ...@@ -1742,15 +1740,16 @@ pub fn project_entrypoints_subscribe(
move || { move || {
async move { async move {
let entrypoints_with_issues_op = get_entrypoints_with_issues_operation(container); let entrypoints_with_issues_op = get_entrypoints_with_issues_operation(container);
let read =
read_strongly_consistent_and_apply_effects(entrypoints_with_issues_op, |v| {
&v.effects
})
.await?;
let EntrypointsWithIssues { let EntrypointsWithIssues {
entrypoints, entrypoints,
issues, issues,
effects, ..
} = &*entrypoints_with_issues_op } = &*read;
.read_strongly_consistent()
.await?;
effects.apply().await?;
Ok((entrypoints.clone(), issues.clone())) Ok((entrypoints.clone(), issues.clone()))
} }
.instrument(tracing::info_span!("entrypoints subscription")) .instrument(tracing::info_span!("entrypoints subscription"))
...@@ -1862,17 +1861,14 @@ pub fn project_hmr_events( ...@@ -1862,17 +1861,14 @@ pub fn project_hmr_events(
state, state,
hmr_target, hmr_target,
); );
let update = update_op.read_strongly_consistent().await?;
let HmrUpdateWithIssues {
update,
issues,
effects,
} = &*update;
// HACK(bgw): Remove this mark call // HACK(bgw): Remove this mark call
mark_top_level_task(); mark_top_level_task();
effects.apply().await?; let read =
read_strongly_consistent_and_apply_effects(update_op, |v| &v.effects)
.await?;
// 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 HmrUpdateWithIssues { update, issues, .. } = &*read;
match &**update { match &**update {
Update::Missing | Update::None => {} Update::Missing | Update::None => {}
Update::Total(TotalUpdate { to }) => { Update::Total(TotalUpdate { to }) => {
...@@ -1984,14 +1980,16 @@ pub fn project_hmr_chunk_names_subscribe( ...@@ -1984,14 +1980,16 @@ pub fn project_hmr_chunk_names_subscribe(
move || async move { move || async move {
let hmr_chunk_names_with_issues_op = let hmr_chunk_names_with_issues_op =
get_hmr_chunk_names_with_issues_operation(container, hmr_target); get_hmr_chunk_names_with_issues_operation(container, hmr_target);
let read =
read_strongly_consistent_and_apply_effects(hmr_chunk_names_with_issues_op, |v| {
&v.effects
})
.await?;
let HmrChunkNamesWithIssues { let HmrChunkNamesWithIssues {
chunk_names, chunk_names,
issues, issues,
effects, ..
} = &*hmr_chunk_names_with_issues_op } = &*read;
.read_strongly_consistent()
.await?;
effects.apply().await?;
Ok((chunk_names.clone(), issues.clone())) Ok((chunk_names.clone(), issues.clone()))
}, },
...@@ -2450,11 +2448,10 @@ pub async fn project_write_analyze_data( ...@@ -2450,11 +2448,10 @@ pub async fn project_write_analyze_data(
.turbo_tasks() .turbo_tasks()
.run_once(async move { .run_once(async move {
let analyze_data_op = write_analyze_data_with_issues_operation(container, app_dir_only); let analyze_data_op = write_analyze_data_with_issues_operation(container, app_dir_only);
let WriteAnalyzeResult { issues, effects } =
&*analyze_data_op.read_strongly_consistent().await?;
// Write the files to disk // Write the files to disk
effects.apply().await?; let read =
read_strongly_consistent_and_apply_effects(analyze_data_op, |v| &v.effects).await?;
let WriteAnalyzeResult { issues, .. } = &*read;
Ok(issues.clone()) Ok(issues.clone())
}) })
.await .await
......
...@@ -64,12 +64,14 @@ turbo-tasks-malloc = { workspace = true, default-features = false } ...@@ -64,12 +64,14 @@ turbo-tasks-malloc = { workspace = true, default-features = false }
thread_local = { workspace = true } thread_local = { workspace = true }
[dev-dependencies] [dev-dependencies]
async-trait = { workspace = true }
criterion = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] }
futures = { workspace = true } futures = { workspace = true }
indoc = { workspace = true } indoc = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
tempfile = { workspace = true } tempfile = { workspace = true }
triomphe = { workspace = true } triomphe = { workspace = true }
thiserror = { workspace = true }
turbo-tasks-malloc = { workspace = true , features = ["custom_allocator"]} turbo-tasks-malloc = { workspace = true , features = ["custom_allocator"]}
rstest = { workspace = true } rstest = { workspace = true }
turbo-tasks-testing = { workspace = true } turbo-tasks-testing = { workspace = true }
......
This diff is collapsed.
...@@ -24,6 +24,7 @@ workspace = true ...@@ -24,6 +24,7 @@ workspace = true
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
async-trait = { workspace = true }
auto-hash-map = { workspace = true } auto-hash-map = { workspace = true }
bincode = { workspace = true } bincode = { workspace = true }
bitflags = "1.3.2" bitflags = "1.3.2"
......
This diff is collapsed.
...@@ -232,7 +232,10 @@ pub mod tests { ...@@ -232,7 +232,10 @@ pub mod tests {
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Completion, Effects, OperationVc, ReadRef, Vc, take_effects}; use turbo_tasks::{
Completion, Effects, OperationVc, ReadRef, Vc, read_strongly_consistent_and_apply_effects,
take_effects,
};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use crate::{ use crate::{
...@@ -560,11 +563,11 @@ pub mod tests { ...@@ -560,11 +563,11 @@ pub mod tests {
.await?; .await?;
// Delete a file that we shouldn't be tracking // Delete a file that we shouldn't be tracking
extract_effects_operation(delete(root.join("dir/sub/.vim/.gitignore")?)) read_strongly_consistent_and_apply_effects(
.read_strongly_consistent() extract_effects_operation(delete(root.join("dir/sub/.vim/.gitignore")?)),
.await? |e| e,
.apply() )
.await?; .await?;
let read_dir2 = track_star_star_glob(dir.clone()) let read_dir2 = track_star_star_glob(dir.clone())
.read_strongly_consistent() .read_strongly_consistent()
...@@ -572,11 +575,11 @@ pub mod tests { ...@@ -572,11 +575,11 @@ pub mod tests {
assert!(ReadRef::ptr_eq(&read_dir, &read_dir2)); assert!(ReadRef::ptr_eq(&read_dir, &read_dir2));
// Delete a file that we should be tracking // Delete a file that we should be tracking
extract_effects_operation(delete(root.join("dir/foo")?)) read_strongly_consistent_and_apply_effects(
.read_strongly_consistent() extract_effects_operation(delete(root.join("dir/foo")?)),
.await? |e| e,
.apply() )
.await?; .await?;
let read_dir2 = track_star_star_glob(dir.clone()) let read_dir2 = track_star_star_glob(dir.clone())
.read_strongly_consistent() .read_strongly_consistent()
...@@ -585,11 +588,14 @@ pub mod tests { ...@@ -585,11 +588,14 @@ pub mod tests {
assert!(!ReadRef::ptr_eq(&read_dir, &read_dir2)); assert!(!ReadRef::ptr_eq(&read_dir, &read_dir2));
// Modify a symlink target file // Modify a symlink target file
extract_effects_operation(write(root.join("link_target.js")?, rcstr!("new_contents"))) read_strongly_consistent_and_apply_effects(
.read_strongly_consistent() extract_effects_operation(write(
.await? root.join("link_target.js")?,
.apply() rcstr!("new_contents"),
.await?; )),
|e| e,
)
.await?;
let read_dir3 = track_star_star_glob(dir.clone()) let read_dir3 = track_star_star_glob(dir.clone())
.read_strongly_consistent() .read_strongly_consistent()
.await?; .await?;
......
...@@ -15,8 +15,8 @@ use rustc_hash::FxHashSet; ...@@ -15,8 +15,8 @@ use rustc_hash::FxHashSet;
use tokio::time::sleep; use tokio::time::sleep;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Effects, NonLocalValue, OperationVc, ResolvedVc, TransientInstance, Vc, take_effects, Effects, NonLocalValue, OperationVc, ResolvedVc, TransientInstance, Vc,
trace::TraceRawVcs, read_strongly_consistent_and_apply_effects, take_effects, trace::TraceRawVcs,
}; };
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
...@@ -142,7 +142,7 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> { ...@@ -142,7 +142,7 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
let symlink_is_directory = let symlink_is_directory =
symlink_mode.map(|m| m.to_link_type().contains(LinkType::DIRECTORY)); symlink_mode.map(|m| m.to_link_type().contains(LinkType::DIRECTORY));
let effects = extract_effects_operation(read_or_write_all_paths_operation( let effects_op = extract_effects_operation(read_or_write_all_paths_operation(
invalidations.clone(), invalidations.clone(),
project_root.clone(), project_root.clone(),
args.depth, args.depth,
...@@ -150,11 +150,9 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> { ...@@ -150,11 +150,9 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
symlink_count, symlink_count,
symlink_is_directory, symlink_is_directory,
track_writes, track_writes,
)) ));
.read_strongly_consistent()
.await?;
if track_writes { if track_writes {
effects.apply().await?; read_strongly_consistent_and_apply_effects(effects_op, |e| e).await?;
let (total, mismatched) = verify_written_files( let (total, mismatched) = verify_written_files(
&fs_root, &fs_root,
args.depth, args.depth,
...@@ -169,6 +167,8 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> { ...@@ -169,6 +167,8 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
} }
} }
} else { } else {
// Still drive the computation (and propagate errors) without applying effects.
effects_op.read_strongly_consistent().await?;
let invalidations = invalidations.0.lock().unwrap(); let invalidations = invalidations.0.lock().unwrap();
println!("read all {} files", invalidations.len()); println!("read all {} files", invalidations.len());
} }
...@@ -228,7 +228,7 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> { ...@@ -228,7 +228,7 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
// there's no way to know when we've received all the pending events from the operating // there's no way to know when we've received all the pending events from the operating
// system, so just sleep and pray // system, so just sleep and pray
sleep(Duration::from_millis(args.notify_timeout_ms)).await; sleep(Duration::from_millis(args.notify_timeout_ms)).await;
let effects = extract_effects_operation(read_or_write_all_paths_operation( let effects_op = extract_effects_operation(read_or_write_all_paths_operation(
invalidations.clone(), invalidations.clone(),
project_root.clone(), project_root.clone(),
args.depth, args.depth,
...@@ -236,16 +236,14 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> { ...@@ -236,16 +236,14 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
symlink_count, symlink_count,
symlink_is_directory, symlink_is_directory,
track_writes, track_writes,
)) ));
.read_strongly_consistent()
.await?;
let symlink_info = if args.symlinks.is_some() { let symlink_info = if args.symlinks.is_some() {
" and symlinks" " and symlinks"
} else { } else {
"" ""
}; };
if track_writes { if track_writes {
effects.apply().await?; read_strongly_consistent_and_apply_effects(effects_op, |e| e).await?;
let (total, mismatched) = verify_written_files( let (total, mismatched) = verify_written_files(
&fs_root, &fs_root,
args.depth, args.depth,
...@@ -268,6 +266,8 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> { ...@@ -268,6 +266,8 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
} }
} }
} else { } else {
// Still drive the computation (and propagate errors) without applying effects.
effects_op.read_strongly_consistent().await?;
let mut invalidations = invalidations.0.lock().unwrap(); let mut invalidations = invalidations.0.lock().unwrap();
println!( println!(
"modified {} files{}. found {} invalidations", "modified {} files{}. found {} invalidations",
......
...@@ -8,7 +8,10 @@ const PROGRESS_INTERVAL: Duration = Duration::from_secs(1); ...@@ -8,7 +8,10 @@ const PROGRESS_INTERVAL: Duration = Duration::from_secs(1);
use clap::Args; use clap::Args;
use rand::{RngExt, SeedableRng}; use rand::{RngExt, SeedableRng};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Effects, OperationVc, ResolvedVc, TryJoinIterExt, Vc, take_effects}; use turbo_tasks::{
Effects, OperationVc, ResolvedVc, TryJoinIterExt, Vc,
read_strongly_consistent_and_apply_effects, take_effects,
};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, LinkContent, LinkType}; use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, LinkContent, LinkType};
...@@ -82,14 +85,14 @@ pub async fn run(args: SymlinkStress) -> anyhow::Result<()> { ...@@ -82,14 +85,14 @@ pub async fn run(args: SymlinkStress) -> anyhow::Result<()> {
println!("creating {symlink_count} initial symlinks..."); println!("creating {symlink_count} initial symlinks...");
extract_effects_operation(create_initial_symlinks_operation( read_strongly_consistent_and_apply_effects(
symlinks_path.clone(), extract_effects_operation(create_initial_symlinks_operation(
symlink_count, symlinks_path.clone(),
initial_target, symlink_count,
)) initial_target,
.read_strongly_consistent() )),
.await? |e| e,
.apply() )
.await?; .await?;
println!( println!(
...@@ -120,13 +123,13 @@ pub async fn run(args: SymlinkStress) -> anyhow::Result<()> { ...@@ -120,13 +123,13 @@ pub async fn run(args: SymlinkStress) -> anyhow::Result<()> {
.collect(); .collect();
// Execute writes in parallel via turbo-tasks // Execute writes in parallel via turbo-tasks
extract_effects_operation(write_symlinks_batch_operation( read_strongly_consistent_and_apply_effects(
symlinks_path.clone(), extract_effects_operation(write_symlinks_batch_operation(
updates, symlinks_path.clone(),
)) updates,
.read_strongly_consistent() )),
.await? |e| e,
.apply() )
.await?; .await?;
total_writes += parallelism as u64; total_writes += parallelism as u64;
......
This diff is collapsed.
...@@ -83,7 +83,11 @@ pub use crate::{ ...@@ -83,7 +83,11 @@ pub use crate::{
dyn_task_inputs::{ dyn_task_inputs::{
DynTaskInputs, DynTaskInputsStorage, HeapDynTaskInputsStorage, StackDynTaskInputsStorage, DynTaskInputs, DynTaskInputsStorage, HeapDynTaskInputsStorage, StackDynTaskInputsStorage,
}, },
effect::{Effect, EffectError, EffectStateStorage, Effects, emit_effect, take_effects}, effect::{
ApplyError, CapturedEffect, Effect, EffectError, EffectExt, EffectStateStorage, Effects,
EffectsError, read_strongly_consistent_and_apply_effects,
resolve_strongly_consistent_and_take_and_apply_effects, take_effects,
},
error::PrettyPrintError, error::PrettyPrintError,
id::{ id::{
ExecutionId, FunctionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId, ExecutionId, FunctionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId,
......
...@@ -162,6 +162,7 @@ impl_task_input! { ...@@ -162,6 +162,7 @@ impl_task_input! {
u32, u32,
i32, i32,
u64, u64,
u128,
usize, usize,
RcStr, RcStr,
TaskId, TaskId,
......
...@@ -11,7 +11,7 @@ use tracing::Instrument; ...@@ -11,7 +11,7 @@ use tracing::Instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
Effects, OperationVc, ResolvedVc, TransientInstance, TryJoinIterExt, TurboTasks, Vc, Effects, OperationVc, ResolvedVc, TransientInstance, TryJoinIterExt, TurboTasks, Vc,
take_effects, read_strongly_consistent_and_apply_effects, take_effects,
}; };
use turbo_tasks_backend::{ use turbo_tasks_backend::{
BackendOptions, GitVersionInfo, StartupCacheState, StorageMode, TurboTasksBackend, BackendOptions, GitVersionInfo, StartupCacheState, StorageMode, TurboTasksBackend,
...@@ -158,10 +158,7 @@ impl TurbopackBuildBuilder { ...@@ -158,10 +158,7 @@ impl TurbopackBuildBuilder {
self.scope_hoist, self.scope_hoist,
)); ));
// Await the result to propagate any errors and capture effects. read_strongly_consistent_and_apply_effects(wrapper_op, |e| e).await?;
let effects = wrapper_op.read_strongly_consistent().await?;
effects.apply().await?;
let issue_reporter: Vc<Box<dyn IssueReporter>> = let issue_reporter: Vc<Box<dyn IssueReporter>> =
Vc::upcast(ConsoleUi::new(TransientInstance::new(LogOptions { Vc::upcast(ConsoleUi::new(TransientInstance::new(LogOptions {
......
...@@ -12,7 +12,7 @@ use hyper::{ ...@@ -12,7 +12,7 @@ use hyper::{
use mime::Mime; use mime::Mime;
use turbo_tasks::{ use turbo_tasks::{
CollectiblesSource, Effects, OperationVc, ReadRef, ResolvedVc, TransientInstance, Vc, CollectiblesSource, Effects, OperationVc, ReadRef, ResolvedVc, TransientInstance, Vc,
take_effects, util::SharedError, read_strongly_consistent_and_apply_effects, take_effects, util::SharedError,
}; };
use turbo_tasks_bytes::Bytes; use turbo_tasks_bytes::Bytes;
use turbo_tasks_fs::FileContent; use turbo_tasks_fs::FileContent;
...@@ -106,12 +106,12 @@ pub async fn process_request_with_content_source( ...@@ -106,12 +106,12 @@ pub async fn process_request_with_content_source(
let request = http_request_to_source_request(request).await?; let request = http_request_to_source_request(request).await?;
let wrapper_op = let wrapper_op =
get_from_source_with_collectibles_operation(source, TransientInstance::new(request)); get_from_source_with_collectibles_operation(source, TransientInstance::new(request));
let read = read_strongly_consistent_and_apply_effects(wrapper_op, |v| &v.effects).await?;
let GetFromSourceResultWithCollectibles { let GetFromSourceResultWithCollectibles {
result, result,
effects,
content_source_side_effects, content_source_side_effects,
} = &*wrapper_op.read_strongly_consistent().await?; ..
effects.apply().await?; } = &*read;
handle_issues( handle_issues(
wrapper_op, wrapper_op,
issue_reporter, issue_reporter,
......
...@@ -30,8 +30,9 @@ use socket2::{Domain, Protocol, Socket, Type}; ...@@ -30,8 +30,9 @@ use socket2::{Domain, Protocol, Socket, Type};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tracing::{Instrument, Level, Span, event, info_span}; use tracing::{Instrument, Level, Span, event, info_span};
use turbo_tasks::{ use turbo_tasks::{
Effects, NonLocalValue, OperationVc, PrettyPrintError, TurboTasksApi, Vc, run_once_with_reason, Effects, NonLocalValue, OperationVc, PrettyPrintError, TurboTasksApi, Vc,
take_effects, trace::TraceRawVcs, util::FormatDuration, read_strongly_consistent_and_apply_effects, run_once_with_reason, take_effects,
trace::TraceRawVcs, util::FormatDuration,
}; };
use turbopack_core::issue::{IssueReporter, IssueSeverity, handle_issues}; use turbopack_core::issue::{IssueReporter, IssueSeverity, handle_issues};
...@@ -223,9 +224,12 @@ impl DevServerBuilder { ...@@ -223,9 +224,12 @@ impl DevServerBuilder {
let path = uri.path().to_string(); let path = uri.path().to_string();
let source_with_issues_op = let source_with_issues_op =
get_source_with_issues_operation(source_provider.get_source()); get_source_with_issues_operation(source_provider.get_source());
let ContentSourceWithIssues { source_op, effects } = let read = read_strongly_consistent_and_apply_effects(
&*source_with_issues_op.read_strongly_consistent().await?; source_with_issues_op,
effects.apply().await?; |v| &v.effects,
)
.await?;
let ContentSourceWithIssues { source_op, .. } = &*read;
handle_issues( handle_issues(
source_with_issues_op, source_with_issues_op,
issue_reporter, issue_reporter,
......
use std::{iter, process::ExitStatus, sync::Arc, time::Duration}; use std::{iter, process::ExitStatus, time::Duration};
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use async_trait::async_trait; use async_trait::async_trait;
...@@ -10,8 +10,8 @@ use serde_json::Value as JsonValue; ...@@ -10,8 +10,8 @@ use serde_json::Value as JsonValue;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Completion, FxIndexMap, OperationVc, PrettyPrintError, ResolvedVc, TryJoinIterExt, Completion, FxIndexMap, OperationVc, PrettyPrintError, ResolvedVc, TryJoinIterExt,
ValueToString, Vc, duration_span, fxindexmap, mark_top_level_task, ValueToString, Vc, duration_span, fxindexmap, parallel::available_parallelism,
parallel::available_parallelism, take_effects, trace::TraceRawVcs, resolve_strongly_consistent_and_take_and_apply_effects, trace::TraceRawVcs,
}; };
use turbo_tasks_env::{EnvMap, ProcessEnv}; use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::{File, FileContent, FileSystemPath, to_sys_path}; use turbo_tasks_fs::{File, FileContent, FileSystemPath, to_sys_path};
...@@ -179,7 +179,7 @@ async fn emit_evaluate_pool_assets_operation( ...@@ -179,7 +179,7 @@ async fn emit_evaluate_pool_assets_operation(
Ok(EmittedEvaluatePoolAssets { Ok(EmittedEvaluatePoolAssets {
bootstrap: bootstrap.to_resolved().await?, bootstrap: bootstrap.to_resolved().await?,
output_root, output_root,
entrypoint: entrypoint.clone(), entrypoint,
} }
.cell()) .cell())
} }
...@@ -191,17 +191,16 @@ async fn create_evaluate_pool_assets_operation( ...@@ -191,17 +191,16 @@ async fn create_evaluate_pool_assets_operation(
module_graph: ResolvedVc<ModuleGraph>, module_graph: ResolvedVc<ModuleGraph>,
) -> Result<Vc<EmittedEvaluatePoolAssets>> { ) -> Result<Vc<EmittedEvaluatePoolAssets>> {
let operation = emit_evaluate_pool_assets_operation(entries, chunking_context, module_graph); let operation = emit_evaluate_pool_assets_operation(entries, chunking_context, module_graph);
let assets = operation.resolve().strongly_consistent().await?; // Apply the effects here (inside this producing task) via the bounded-retry helper, draining
let effects = Arc::new(take_effects(operation).await?); // them from the nested emit operation. Returning the serializable `EmittedEvaluatePoolAssets`
// (rather than a `serialization = "skip"` wrapper carrying the effects) keeps this task's
// HACK: `Effects::apply` normally panics if not called at the top-level. We want to apply most // output restorable from the persistent cache, so a warm restart does not re-run the
// effects outside of turbo-task functions to avoid re-executing effects during invalidations. // effect-producing tasks.
// //
// That's not possible here because we lazily create the pool, so instead, use // HACK: applying effects from inside a task means they may get re-applied if this task is
// `mark_top_level_task` to avoid the debug assertion. The consequence is that these effects // invalidated. That's acceptable because the pool is created lazily; we can't move the
// might get evaluated more than once if this function is invalidated. // apply to a true top-level task without eagerly reading the operation.
mark_top_level_task(); let assets = resolve_strongly_consistent_and_take_and_apply_effects(operation).await?;
effects.apply().await?;
Ok(*assets) Ok(*assets)
} }
...@@ -228,6 +227,8 @@ pub async fn get_evaluate_pool( ...@@ -228,6 +227,8 @@ pub async fn get_evaluate_pool(
env_var_tracking: EnvVarTracking, env_var_tracking: EnvVarTracking,
) -> Result<Vc<EvaluatePool>> { ) -> Result<Vc<EvaluatePool>> {
let assets_op = create_evaluate_pool_assets_operation(entries, chunking_context, module_graph); let assets_op = create_evaluate_pool_assets_operation(entries, chunking_context, module_graph);
// Effects are applied inside `create_evaluate_pool_assets_operation`; a plain strongly
// consistent read suffices here.
let assets = assets_op.read_strongly_consistent().await?; let assets = assets_op.read_strongly_consistent().await?;
let EmittedEvaluatePoolAssets { let EmittedEvaluatePoolAssets {
......
...@@ -15,7 +15,8 @@ use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt ...@@ -15,7 +15,8 @@ use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Completion, Effects, NonLocalValue, OperationVc, ReadRef, ResolvedVc, TurboTasks, Vc, Completion, Effects, NonLocalValue, OperationVc, ReadRef, ResolvedVc, TurboTasks, Vc,
debug::ValueDebugFormat, fxindexmap, take_effects, trace::TraceRawVcs, debug::ValueDebugFormat, fxindexmap, read_strongly_consistent_and_apply_effects, take_effects,
trace::TraceRawVcs,
}; };
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_env::CommandLineProcessEnv; use turbo_tasks_env::CommandLineProcessEnv;
...@@ -249,11 +250,9 @@ async fn run(resource: PathBuf, snapshot_mode: IssueSnapshotMode) -> Result<JsRe ...@@ -249,11 +250,9 @@ async fn run(resource: PathBuf, snapshot_mode: IssueSnapshotMode) -> Result<JsRe
} }
tt.run_once(async move { tt.run_once(async move {
let op = run_inner_operation_with_effects(resource.to_str().unwrap().into(), snapshot_mode);
let result_with_effects = let result_with_effects =
run_inner_operation_with_effects(resource.to_str().unwrap().into(), snapshot_mode) read_strongly_consistent_and_apply_effects(op, |v| &v.effects).await?;
.read_strongly_consistent()
.await?;
result_with_effects.effects.apply().await?;
Ok((*result_with_effects.result).clone()) Ok((*result_with_effects.result).clone())
}) })
......
...@@ -11,7 +11,10 @@ use rustc_hash::FxHashSet; ...@@ -11,7 +11,10 @@ use rustc_hash::FxHashSet;
use serde::Deserialize; use serde::Deserialize;
use serde_json::json; use serde_json::json;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Effects, OperationVc, ResolvedVc, TurboTasks, Vc, take_effects, turbofmt}; use turbo_tasks::{
Effects, OperationVc, ResolvedVc, TurboTasks, Vc, read_strongly_consistent_and_apply_effects,
take_effects, turbofmt,
};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_env::DotenvProcessEnv; use turbo_tasks_env::DotenvProcessEnv;
use turbo_tasks_fs::{ use turbo_tasks_fs::{
...@@ -249,11 +252,11 @@ async fn run(resource: PathBuf) -> Result<()> { ...@@ -249,11 +252,11 @@ async fn run(resource: PathBuf) -> Result<()> {
Ok(take_effects(op).await?.cell()) Ok(take_effects(op).await?.cell())
} }
extract_effects(inner_operation(resource.to_str().unwrap().into())) read_strongly_consistent_and_apply_effects(
.read_strongly_consistent() extract_effects(inner_operation(resource.to_str().unwrap().into())),
.await? |e| e,
.apply() )
.await?; .await?;
Ok(()) Ok(())
}) })
......
...@@ -3,7 +3,9 @@ use std::{fs, path::PathBuf}; ...@@ -3,7 +3,9 @@ use std::{fs, path::PathBuf};
use criterion::{Bencher, BenchmarkId, Criterion}; use criterion::{Bencher, BenchmarkId, Criterion};
use regex::Regex; use regex::Regex;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Effects, OperationVc, TurboTasks, Vc, take_effects}; use turbo_tasks::{
Effects, OperationVc, TurboTasks, Vc, read_strongly_consistent_and_apply_effects, take_effects,
};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, NullFileSystem}; use turbo_tasks_fs::{DiskFileSystem, FileSystem, NullFileSystem};
use turbopack::{ use turbopack::{
...@@ -129,11 +131,11 @@ fn bench_emit(b: &mut Bencher, bench_input: &BenchInput) { ...@@ -129,11 +131,11 @@ fn bench_emit(b: &mut Bencher, bench_input: &BenchInput) {
.to_resolved() .to_resolved()
.await?; .await?;
extract_effects_operation(emit_assets_into_dir_operation(assets, output_dir)) read_strongly_consistent_and_apply_effects(
.read_strongly_consistent() extract_effects_operation(emit_assets_into_dir_operation(assets, output_dir)),
.await? |e| e,
.apply() )
.await?; .await?;
Ok(()) Ok(())
}) })
......
...@@ -28,8 +28,8 @@ use serde::Serialize; ...@@ -28,8 +28,8 @@ use serde::Serialize;
use tokio::{process::Command, time::timeout}; use tokio::{process::Command, time::timeout};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Effects, ResolvedVc, TurboTasks, ValueToString, Vc, backend::Backend, take_effects, Effects, ResolvedVc, TurboTasks, ValueToString, Vc, backend::Backend,
trace::TraceRawVcs, read_strongly_consistent_and_apply_effects, take_effects, trace::TraceRawVcs,
}; };
use turbo_tasks_backend::TurboTasksBackend; use turbo_tasks_backend::TurboTasksBackend;
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath}; use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath};
...@@ -489,14 +489,13 @@ fn node_file_trace<B: Backend + 'static>( ...@@ -489,14 +489,13 @@ fn node_file_trace<B: Backend + 'static>(
let directory = directory.clone(); let directory = directory.clone();
let task = async move { let task = async move {
let before_start = Instant::now(); let before_start = Instant::now();
let trace_result = node_file_trace_operation( let trace_op = node_file_trace_operation(
package_root.clone(), package_root.clone(),
input.clone(), input.clone(),
directory.clone(), directory.clone(),
) );
.read_strongly_consistent() let trace_result =
.await?; read_strongly_consistent_and_apply_effects(trace_op, |v| &v.effects).await?;
trace_result.effects.apply().await?;
let rebased = trace_result.rebased; let rebased = trace_result.rebased;
let duration = before_start.elapsed(); let duration = before_start.elapsed();
......
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