From db0ff4e5f88f4685d9e01acbd2ba48d577019d83 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 10 Sep 2026 21:10:53 +0800 Subject: [PATCH] fix(streams): disturb source synchronously when piping starts --- .../tests/fixtures/runtime/pipe_disturbed.js | 150 +++++++++--------- moli-core/tests/web_apis.rs | 5 +- .../tests/web_apis/indexed_db_transaction.rs | 2 +- moli-core/tests/web_apis/pipe_disturbed.rs | 58 +------ .../src/context_bootstrap/streams.rs | 3 - 5 files changed, 81 insertions(+), 137 deletions(-) diff --git a/moli-core/tests/fixtures/runtime/pipe_disturbed.js b/moli-core/tests/fixtures/runtime/pipe_disturbed.js index 097f7023aba..93cb5f7603f 100644 --- a/moli-core/tests/fixtures/runtime/pipe_disturbed.js +++ b/moli-core/tests/fixtures/runtime/pipe_disturbed.js @@ -3,82 +3,86 @@ async function runPipeDisturbedProbe(scenario) { const check = (condition, label) => { if (!condition) errors.push(label); }; - for (const byteStream of [false, true]) { - for (const method of ['pipeTo', 'pipeThrough']) { - const cases = scenario === 'start' - ? ['backpressure', 'pre-aborted', 'source-closed', 'source-errored', 'sink-closed', 'sink-errored'] - : ['source-locked', 'sink-locked', 'invalid-sink', 'invalid-signal', 'throwing-options']; - for (const state of cases) { - const label = `${byteStream ? 'bytes' : 'default'}/${method}/${state}`; - const reason = new Error(label); - let sourceController, sinkController, pulls = 0, writes = 0; - const source = new ReadableStream({ - ...(byteStream ? {type: 'bytes'} : {}), - start(controller) { sourceController = controller; }, - pull() { ++pulls; } - }, {highWaterMark: 0}); - const body = new Response(source); - check(body.body === source, `${label}: body observes the tested source`); - const sink = new WritableStream({ - start(controller) { sinkController = controller; }, - write() { ++writes; }, - abort() { check(body.bodyUsed, `${label}: abort observes disturbed source`); } - }, {highWaterMark: 0}); - const output = new ReadableStream({}, {highWaterMark: 0}); - const outputBody = new Response(output); - const abort = new AbortController(); - let options = {signal: abort.signal, preventCancel: true}; - let destination = sink, reader, writer; - if (state === 'source-closed') sourceController.close(); - if (state === 'source-errored') sourceController.error(reason); - if (state === 'sink-closed') await sink.close(); - if (state === 'sink-errored') sinkController.error(reason); - if (state === 'pre-aborted') abort.abort(reason); - if (state === 'source-locked') reader = source.getReader(); - if (state === 'sink-locked') writer = sink.getWriter(); - if (state === 'invalid-sink') destination = {}; - if (state === 'invalid-signal') options = {signal: {}}; - if (state === 'throwing-options') options = {get preventCancel() {throw reason;}}; - check(!body.bodyUsed, `${label}: initially undisturbed`); - let completion, thrown; - try { - if (method === 'pipeTo') { - completion = source.pipeTo(destination, options); - completion.catch(() => {}); + for (const ownerKind of ['Response', 'Request']) { + for (const byteStream of [false, true]) { + for (const method of ['pipeTo', 'pipeThrough']) { + const cases = scenario === 'start' + ? ['backpressure', 'pre-aborted', 'source-closed', 'source-errored', 'sink-closed', 'sink-errored'] + : ['source-locked', 'sink-locked', 'invalid-sink', 'invalid-signal', 'throwing-options']; + for (const state of cases) { + const label = `${ownerKind}/${byteStream ? 'bytes' : 'default'}/${method}/${state}`; + const reason = new Error(label); + let sourceController, sinkController, pulls = 0, writes = 0; + const source = new ReadableStream({ + ...(byteStream ? {type: 'bytes'} : {}), + start(controller) { sourceController = controller; }, + pull() { ++pulls; } + }, {highWaterMark: 0}); + const body = ownerKind === 'Response' + ? new Response(source) + : new Request('https://example.test/', {method: 'POST', body: source, duplex: 'half'}); + check(body.body === source, `${label}: body observes the tested source`); + const sink = new WritableStream({ + start(controller) { sinkController = controller; }, + write() { ++writes; }, + abort() { check(body.bodyUsed, `${label}: abort observes disturbed source`); } + }, {highWaterMark: 0}); + const output = new ReadableStream({}, {highWaterMark: 0}); + const outputBody = new Response(output); + const abort = new AbortController(); + let options = {signal: abort.signal, preventCancel: true}; + let destination = sink, reader, writer; + if (state === 'source-closed') sourceController.close(); + if (state === 'source-errored') sourceController.error(reason); + if (state === 'sink-closed') await sink.close(); + if (state === 'sink-errored') sinkController.error(reason); + if (state === 'pre-aborted') abort.abort(reason); + if (state === 'source-locked') reader = source.getReader(); + if (state === 'sink-locked') writer = sink.getWriter(); + if (state === 'invalid-sink') destination = {}; + if (state === 'invalid-signal') options = {signal: {}}; + if (state === 'throwing-options') options = {get preventCancel() {throw reason;}}; + check(!body.bodyUsed, `${label}: initially undisturbed`); + let completion, thrown; + try { + if (method === 'pipeTo') { + completion = source.pipeTo(destination, options); + completion.catch(() => {}); + } else { + const result = source.pipeThrough({readable: output, writable: destination}, options); + check(result === output, `${label}: returns output stream`); + } + } catch (error) { + thrown = error; + } + if (scenario === 'start') { + check(thrown === undefined, `${label}: accepted pipe does not throw`); + check(body.bodyUsed, `${label}: synchronously disturbed without reading`); + check(pulls === 0 && writes === 0, `${label}: no pull or write before return`); + check(!outputBody.bodyUsed, `${label}: output remains undisturbed`); + abort.abort(reason); + if (completion) await completion.catch(() => {}); + for (let turn = 0; (source.locked || sink.locked) && turn < 30; ++turn) { + await new Promise(resolve => setTimeout(resolve, 0)); + } + check(!source.locked && !sink.locked, `${label}: pipe releases locks`); + check(body.bodyUsed, `${label}: remains disturbed after shutdown`); } else { - const result = source.pipeThrough({readable: output, writable: destination}, options); - check(result === output, `${label}: returns output stream`); + if (completion) { + check(thrown === undefined, `${label}: pipeTo returns a rejection`); + thrown = await completion.then(() => undefined, error => error); + } + check(state === 'throwing-options' ? thrown === reason : thrown instanceof TypeError, + `${label}: validation rejects with original error`); + reader?.releaseLock(); + writer?.releaseLock(); + check(!body.bodyUsed, `${label}: rejected pipe leaves source undisturbed`); + check(!source.locked && !sink.locked, `${label}: rejected pipe acquires no locks`); + check(pulls === 0 && writes === 0, `${label}: rejected pipe does not read or write`); } - } catch (error) { - thrown = error; + // Avoid invoking the observation above during test cleanup. + await Promise.allSettled([source.cancel(), output.cancel()]); } - if (scenario === 'start') { - check(thrown === undefined, `${label}: accepted pipe does not throw`); - check(body.bodyUsed, `${label}: synchronously disturbed without reading`); - check(pulls === 0 && writes === 0, `${label}: no pull or write before return`); - check(!outputBody.bodyUsed, `${label}: output remains undisturbed`); - abort.abort(reason); - if (completion) await completion.catch(() => {}); - for (let turn = 0; (source.locked || sink.locked) && turn < 30; ++turn) { - await new Promise(resolve => setTimeout(resolve, 0)); - } - check(!source.locked && !sink.locked, `${label}: pipe releases locks`); - check(body.bodyUsed, `${label}: remains disturbed after shutdown`); - } else { - if (completion) { - check(thrown === undefined, `${label}: pipeTo returns a rejection`); - thrown = await completion.then(() => undefined, error => error); - } - check(state === 'throwing-options' ? thrown === reason : thrown instanceof TypeError, - `${label}: validation rejects with original error`); - reader?.releaseLock(); - writer?.releaseLock(); - check(!body.bodyUsed, `${label}: rejected pipe leaves source undisturbed`); - check(!source.locked && !sink.locked, `${label}: rejected pipe acquires no locks`); - check(pulls === 0 && writes === 0, `${label}: rejected pipe does not read or write`); - } - // Avoid invoking the observation above during test cleanup. - await Promise.allSettled([source.cancel(), output.cancel()]); } } } diff --git a/moli-core/tests/web_apis.rs b/moli-core/tests/web_apis.rs index e74be10cbbb..cfe21447df0 100644 --- a/moli-core/tests/web_apis.rs +++ b/moli-core/tests/web_apis.rs @@ -13,9 +13,6 @@ mod font_queries; #[path = "web_apis/indexed_db_transaction.rs"] mod indexed_db_transaction; -#[path = "web_apis/pipe_disturbed.rs"] -mod pipe_disturbed; - use moli_test_support as support; use anyhow::Result; @@ -36,6 +33,8 @@ mod body_state; mod callback_cleanup; #[path = "web_apis/event_dispatch.rs"] mod event_dispatch; +#[path = "web_apis/pipe_disturbed.rs"] +mod pipe_disturbed; #[path = "web_apis/request_init.rs"] mod request_init; #[path = "web_apis/request_stream.rs"] diff --git a/moli-core/tests/web_apis/indexed_db_transaction.rs b/moli-core/tests/web_apis/indexed_db_transaction.rs index f2c8d8f7996..d3cd5bae602 100644 --- a/moli-core/tests/web_apis/indexed_db_transaction.rs +++ b/moli-core/tests/web_apis/indexed_db_transaction.rs @@ -1,4 +1,4 @@ -use super::pipe_disturbed::run_probe; +use super::event_dispatch::run_probe; use super::*; async fn assert_multi_entry_keys(scenario: &str) -> Result<()> { diff --git a/moli-core/tests/web_apis/pipe_disturbed.rs b/moli-core/tests/web_apis/pipe_disturbed.rs index 4f08b9779a1..e3996e56be2 100644 --- a/moli-core/tests/web_apis/pipe_disturbed.rs +++ b/moli-core/tests/web_apis/pipe_disturbed.rs @@ -11,7 +11,7 @@ async fn assert_pipe_disturbed(scenario: &str) -> Result<()> { for target in ["window", "child", "worker"] { let observed = tokio::time::timeout( Duration::from_secs(20), - run_probe(&browser, &server, target, &source), + super::event_dispatch::run_probe(&browser, &server, target, &source), ) .await??; assert_eq!( @@ -33,59 +33,3 @@ async fn piping_disturbs_fetch_bodies_before_reads_and_abort_handling() -> Resul async fn rejected_piping_preserves_undisturbed_fetch_bodies() -> Result<()> { assert_pipe_disturbed("reject").await } - -fn markup_url(server: &FixtureServer, markup: &str) -> String { - let mut url = url::Url::parse(&server.url("/compat/child-dynamic-markup-document")).unwrap(); - url.query_pairs_mut().append_pair("markup", markup); - url.into() -} - -pub(super) async fn run_probe( - browser: &Browser, - server: &FixtureServer, - target: &str, - source: &str, -) -> Result { - let markup = if target == "worker" { - let source = serde_json::to_string(&format!( - "self.finish = value => postMessage(value);\n{source}" - ))?; - format!( - r#""# - ) - } else { - format!( - "" - ) - }; - let url = markup_url(server, &markup); - let url = if target == "child" { - markup_url( - server, - &format!( - "", - url.replace('&', "&") - ), - ) - } else { - url - }; - let mut page = browser.fetch(&url).await?; - let expression = if target == "child" { - "document.getElementById('target').contentWindow.done.then(JSON.stringify)" - } else { - "done.then(JSON.stringify)" - }; - let value = page - .evaluate_runtime_expression_with_await_async(expression, true) - .await?; - Ok(serde_json::from_str( - value["value"].as_str().expect("callback probe result"), - )?) -} diff --git a/moli-renderer-v8/src/context_bootstrap/streams.rs b/moli-renderer-v8/src/context_bootstrap/streams.rs index 9c76d50acfe..9d5eec2b86d 100644 --- a/moli-renderer-v8/src/context_bootstrap/streams.rs +++ b/moli-renderer-v8/src/context_bootstrap/streams.rs @@ -60,9 +60,6 @@ pub(crate) fn proxy_fetch_body_stream<'s>( initialize_transform_stream_object(scope, transform, None, None, 1.0, None, 0.0, None); let readable = stream_slot_object(scope, transform, TRANSFORM_STREAM_READABLE_SLOT)?; let writable = stream_slot_object(scope, transform, TRANSFORM_STREAM_WRITABLE_SLOT)?; - // A proxy disturbs its input immediately, even when backpressure delays - // the first source read. - super::stream_adapter::disturb_readable_stream(scope, stream); let pipe = readable::start_internal_readable_stream_pipe_to(scope, stream, writable)?; suppress_promise_unhandled_rejection(scope, pipe); Some(readable)