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