(fix): STOP_SENDING
Some checks failed
CI / checks (push) Has been cancelled

This commit is contained in:
Alois 2026-07-04 13:33:13 +02:00
commit 0bb3f607c7
2 changed files with 64 additions and 43 deletions

1
example/Cargo.lock generated
View file

@ -902,7 +902,6 @@ dependencies = [
"mtp-crypto", "mtp-crypto",
"mtp-files", "mtp-files",
"mtp-host", "mtp-host",
"mtp-transport",
"mtp-type-map", "mtp-type-map",
] ]

View file

@ -10,20 +10,25 @@ use crate::frame::parse_frame_value;
const CLOSE_FRAME_LEN: u32 = u32::MAX; const CLOSE_FRAME_LEN: u32 = u32::MAX;
/// Inspect a JS error value for a WebTransport stream error and log the /// Inspect a JS error value for a WebTransport **stream-level** error and, if
/// `streamErrorCode` carried by STOP_SENDING / RESET_STREAM. /// present, log the `streamErrorCode` carried by STOP_SENDING / RESET_STREAM.
/// ///
/// The browser's WebTransport API rejects write/close/read promises with a
/// `WebTransportError` whose `source` is `"stream"` and whose
/// `streamErrorCode` is the application error code from the peer's
/// STOP_SENDING (for send streams) or RESET_STREAM (for receive streams).
/// Per draft-ietf-webtrans-http3-15 §4.4, a WebTransport application MUST /// Per draft-ietf-webtrans-http3-15 §4.4, a WebTransport application MUST
/// provide an error code for those operations, so it is always present on /// provide an error code for those operations. The browser surfaces these as
/// stream-level errors. /// `WebTransportError` with `source = "stream"` and a numeric `streamErrorCode`.
fn log_webtransport_error(error: &JsValue, context: &str) { ///
/// Session-level errors (`source = "session"`) are normal connection
/// closures and are **not** logged here — they propagate to `on_error`
/// in the receive loop like any other transport error.
fn log_stream_error_code(error: &JsValue, context: &str) {
let source = js_sys::Reflect::get(error, &JsValue::from_str("source")) let source = js_sys::Reflect::get(error, &JsValue::from_str("source"))
.ok() .ok()
.and_then(|v| v.as_string()); .and_then(|v| v.as_string());
if source.as_deref() != Some("stream") {
return;
}
let stream_error_code = js_sys::Reflect::get(error, &JsValue::from_str("streamErrorCode")) let stream_error_code = js_sys::Reflect::get(error, &JsValue::from_str("streamErrorCode"))
.ok() .ok()
.and_then(|v| v.as_f64()); .and_then(|v| v.as_f64());
@ -36,15 +41,12 @@ fn log_webtransport_error(error: &JsValue, context: &str) {
}) })
.unwrap_or_else(|| format!("{:?}", error)); .unwrap_or_else(|| format!("{:?}", error));
let formatted = match (&source, stream_error_code) { let formatted = match stream_error_code {
(Some(src), Some(code)) => format!( Some(code) => format!(
"[WasmTransport] {context}: WebTransportError source={src} \ "[WasmTransport] {context}: STOP_SENDING/RESET_STREAM streamErrorCode={code} \
streamErrorCode={code} ({message})" ({message})"
), ),
(Some(src), None) => { None => format!("[WasmTransport] {context}: stream error ({message})"),
format!("[WasmTransport] {context}: WebTransportError source={src} ({message})")
}
(None, _) => format!("[WasmTransport] {context}: {message}"),
}; };
if let Ok(console) = js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("console")) { if let Ok(console) = js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("console")) {
@ -76,6 +78,18 @@ fn resolve_stream_readable(recv_stream: &JsValue) -> Result<JsValue, JsValue> {
} }
} }
/// Release a `WritableStreamDefaultWriter`'s lock on its stream. Called after
/// `writer.close()` (or on write failure) so the runtime does not interpret an
/// abandoned locked writer as an abort, which would surface as STOP_SENDING to
/// the peer. Errors are ignored — `releaseLock` is best-effort cleanup.
fn release_writer_lock(writer: &JsValue) {
if let Ok(release) = js_sys::Reflect::get(writer, &JsValue::from_str("releaseLock"))
.and_then(|f| f.dyn_into::<js_sys::Function>().map_err(Into::into))
{
let _ = release.call0(writer);
}
}
/// Outcome of reading the next framed message from the incoming stream(s). /// Outcome of reading the next framed message from the incoming stream(s).
enum FrameOutcome { enum FrameOutcome {
/// A complete application frame. /// A complete application frame.
@ -216,7 +230,8 @@ impl WasmTransport {
.call1(&writer_val, &chunk) .call1(&writer_val, &chunk)
.map_err(|e| js_error(&format!("write failed: {:?}", e)))?; .map_err(|e| js_error(&format!("write failed: {:?}", e)))?;
if let Err(e) = JsFuture::from(write_promise.unchecked_into::<js_sys::Promise>()).await { if let Err(e) = JsFuture::from(write_promise.unchecked_into::<js_sys::Promise>()).await {
log_webtransport_error(&e, "send_frame write"); log_stream_error_code(&e, "send_frame write");
release_writer_lock(&writer_val);
return Err(e); return Err(e);
} }
@ -228,10 +243,17 @@ impl WasmTransport {
.call0(&writer_val) .call0(&writer_val)
.map_err(|e| js_error(&format!("close failed: {:?}", e)))?; .map_err(|e| js_error(&format!("close failed: {:?}", e)))?;
if let Err(e) = JsFuture::from(close_promise.unchecked_into::<js_sys::Promise>()).await { if let Err(e) = JsFuture::from(close_promise.unchecked_into::<js_sys::Promise>()).await {
log_webtransport_error(&e, "send_frame close"); // The write already succeeded; a STOP_SENDING on close just means
return Err(e); // the peer stopped reading before we could send FIN. The data is in
// flight, so this is not a send failure — log and return success.
log_stream_error_code(&e, "send_frame close");
} }
// Always release the writer's lock on the WritableStream. Abandoning a
// locked writer (e.g. via drop) can be interpreted by the runtime as an
// abort, which may surface as STOP_SENDING to the peer.
release_writer_lock(&writer_val);
Ok(()) Ok(())
} }
@ -263,17 +285,17 @@ impl WasmTransport {
.map_err(|_| js_error("missing read"))? .map_err(|_| js_error("missing read"))?
.dyn_into::<js_sys::Function>() .dyn_into::<js_sys::Function>()
.map_err(|_| js_error("read not a function"))?; .map_err(|_| js_error("read not a function"))?;
let result = JsFuture::from( let promise = read_fn
read_fn
.call0(&streams_reader) .call0(&streams_reader)
.map_err(|_| js_error("read call failed"))? .map_err(|_| js_error("read call failed"))?
.unchecked_into::<js_sys::Promise>(), .unchecked_into::<js_sys::Promise>();
) let result = match JsFuture::from(promise).await {
.await Ok(r) => r,
.map_err(|e| { Err(e) => {
log_webtransport_error(&e, "open_next_stream accept"); log_stream_error_code(&e, "open_next_stream accept");
js_error(&format!("accept stream failed: {:?}", e)) return Err(js_error(&format!("accept stream failed: {:?}", e)));
})?; }
};
let done = js_sys::Reflect::get(&result, &JsValue::from_str("done")) let done = js_sys::Reflect::get(&result, &JsValue::from_str("done"))
.ok() .ok()
@ -308,17 +330,17 @@ impl WasmTransport {
.map_err(|_| js_error("missing read"))? .map_err(|_| js_error("missing read"))?
.dyn_into::<js_sys::Function>() .dyn_into::<js_sys::Function>()
.map_err(|_| js_error("read not a function"))?; .map_err(|_| js_error("read not a function"))?;
let result = JsFuture::from( let promise = read_fn
read_fn
.call0(&reader) .call0(&reader)
.map_err(|_| js_error("read call failed"))? .map_err(|_| js_error("read call failed"))?
.unchecked_into::<js_sys::Promise>(), .unchecked_into::<js_sys::Promise>();
) let result = match JsFuture::from(promise).await {
.await Ok(r) => r,
.map_err(|e| { Err(e) => {
log_webtransport_error(&e, "read_chunk"); log_stream_error_code(&e, "read_chunk");
js_error(&format!("read failed: {:?}", e)) return Err(js_error(&format!("read failed: {:?}", e)));
})?; }
};
let done = js_sys::Reflect::get(&result, &JsValue::from_str("done")) let done = js_sys::Reflect::get(&result, &JsValue::from_str("done"))
.ok() .ok()