Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 90 additions & 13 deletions src/runtime/socket/UpgradedDuplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ pub(crate) struct UpgradedDuplex {
/// Replayed by [`Self::drain_pending`] after the staged bytes, preserving
/// the original data-then-EOF order.
pub pending_end: Cell<bool>,
/// The transport delivered EOF (its 'end' event fired). Teardown payloads
/// (close_notify) are dropped after this; see [`Self::call_write_or_end`].
pub transport_eof: Cell<bool>,
/// [`Self::pause_stream`] paused `origin` and no [`Self::resume_stream`]
/// followed. [`Self::on_close`] resumes such a transport so it can still
/// drain to its own EOF once the engine is gone.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub reads_paused: Cell<bool>,
}

bun_event_loop::impl_timer_owner!(UpgradedDuplex; from_timer_ptr => event_loop_timer);
Expand Down Expand Up @@ -209,6 +210,11 @@ impl UpgradedDuplex {
js_wrapper.ensure_still_alive();

(this.handlers.on_close)(this.handlers.ctx);
// A transport left paused would never read its peer's EOF and close.
// `teardown` neuters the thunks, so whatever it still delivers is dropped.
Comment thread
robobun marked this conversation as resolved.
Resolved
if this.reads_paused.get() {
this.resume_stream();
}
// closes the underlying duplex
this.call_write_or_end(None, false);

Expand All @@ -217,6 +223,57 @@ impl UpgradedDuplex {
js_wrapper.ensure_still_alive();
}

/// node's `JSStreamSocket.readStop()` / `readStart()`: the transport only
/// emits 'data' while the TLS socket wants more. A chunk already handed to
/// the engine is still decrypted and delivered in full.
/// https://github.com/nodejs/node/blob/v26.3.0/lib/internal/js_stream_socket.js#L117-L125
///
/// A pause before `start_tls` ran is left alone, like a socket that is
/// still connecting: `on_open` forgets the owner's paused flag, and the
/// handshake needs the reads.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[uws_callback(export = "UpgradedDuplex__pause_stream")]
pub(crate) fn pause_stream(&self) -> bool {
if self.wrapper_ref().is_none() || !self.call_origin("pause") {
return false;
}
self.reads_paused.set(true);
true
Comment thread
robobun marked this conversation as resolved.
Resolved
}

#[uws_callback(export = "UpgradedDuplex__resume_stream")]
pub(crate) fn resume_stream(&self) -> bool {
if !self.call_origin("resume") {
return false;
}
self.reads_paused.set(false);
true
}

/// Calls `origin[name]()`. False when there is no JS duplex to talk to
/// (see [`Self::call_write_or_end`]) or the call threw (routed to `on_error`).
Comment thread
robobun marked this conversation as resolved.
Resolved
fn call_origin(&self, name: &str) -> bool {
let duplex = self.origin.get();
if duplex.is_empty() {
return false;
}
let Some(global) = self.global else {
return false;
};
let method = match duplex.get(&global, name) {
Ok(Some(f)) if f.is_callable() => f,
Ok(_) => return false,
Err(err) => {
(self.handlers.on_error)(self.handlers.ctx, global.take_error(err));
return false;
}
};
if let Err(err) = method.call(&global, duplex, &[]) {
(self.handlers.on_error)(self.handlers.ctx, global.take_error(err));
return false;
}
true
}

fn call_write_or_end(&self, data: Option<&[u8]>, msg_more: bool) {
// No JS duplex to talk to: the zeroed placeholder, or the owning
// socket's finalizer abandoned it (`abandon_js_side`).
Expand All @@ -234,13 +291,20 @@ impl UpgradedDuplex {
let teardown = data.is_none() || self.wrapper_ref().is_some_and(|w| w.is_shutdown());
if teardown {
// A teardown payload (close_notify) after the transport's readable
// side ended has no reader behind it: node writes nothing there,
// and a transport that forwards into an auto-ended net.Socket
// throws writeAfterFIN (EPIPE). The trailing end() is not a write
// and still goes through the writableEnded probe below, so a
// half-open transport sees our FIN.
if data.is_some() && self.transport_eof.get() {
return;
// side got its EOF has no reader behind it: node writes nothing
// there, and a transport that forwards into an auto-ended
// net.Socket throws writeAfterFIN (EPIPE). The trailing end() is
// not a write and still goes through the writableEnded probe
// below, so a half-open transport sees our FIN.
Comment thread
robobun marked this conversation as resolved.
Resolved
if data.is_some() {
match Self::readable_got_eof(duplex, &global) {
Ok(false) => {}
Ok(true) => return,
Err(err) => {
(self.handlers.on_error)(self.handlers.ctx, global.take_error(err));
return;
}
}
}
match duplex.get(&global, "writableEnded") {
Ok(Some(ended)) if ended.to_boolean() => return,
Expand Down Expand Up @@ -276,6 +340,20 @@ impl UpgradedDuplex {
}
}

/// `duplex._readableState.ended`. The 'end' event comes too late to tell:
/// a paused transport holds it back until the unread bytes are consumed.
Comment thread
robobun marked this conversation as resolved.
Resolved
fn readable_got_eof(duplex: JSValue, global: &JSGlobalObject) -> JsResult<bool> {
let Some(state) = duplex.get(global, "_readableState")? else {
return Ok(false);
};
if !state.is_object() {
return Ok(false);
}
Ok(state
.get(global, "ended")?
.is_some_and(|ended| ended.to_boolean()))
}

fn internal_write(this: *mut Self, encoded_data: &[u8]) {
// SAFETY: see handler note above.
unsafe { &*this }.write_encrypted(encoded_data);
Expand Down Expand Up @@ -408,7 +486,7 @@ impl UpgradedDuplex {
current_timeout: Cell::new(0),
pending_data: JsCell::new(Vec::new()),
pending_end: Cell::new(false),
transport_eof: Cell::new(false),
reads_paused: Cell::new(false),
}
}

Expand Down Expand Up @@ -679,7 +757,7 @@ impl UpgradedDuplex {
self.ssl_error.set(CertError::default());
self.pending_data.set(Vec::new());
self.pending_end.set(false);
self.transport_eof.set(false);
self.reads_paused.set(false);
}
}

Expand Down Expand Up @@ -738,7 +816,6 @@ fn on_end(_global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> {
// SAFETY: see host-fn note above.
let this = unsafe { &*self_ptr.cast::<UpgradedDuplex>() };

this.transport_eof.set(true);
if this.wrapper_ref().is_some() {
(this.handlers.on_end)(this.handlers.ctx);
} else {
Expand Down
10 changes: 10 additions & 0 deletions src/uws_sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ unsafe extern "C" {
safe fn UpgradedDuplex__shutdown_read(this: &mut UpgradedDuplex);
safe fn UpgradedDuplex__close(this: &mut UpgradedDuplex);
safe fn UpgradedDuplex__abandon_js_side(this: &mut UpgradedDuplex);
safe fn UpgradedDuplex__pause_stream(this: &mut UpgradedDuplex) -> bool;
safe fn UpgradedDuplex__resume_stream(this: &mut UpgradedDuplex) -> bool;
}
impl UpgradedDuplex {
#[inline]
Expand Down Expand Up @@ -282,6 +284,14 @@ impl UpgradedDuplex {
pub(crate) fn abandon_js_side(&mut self) {
UpgradedDuplex__abandon_js_side(self)
}
#[inline]
pub(crate) fn pause_stream(&mut self) -> bool {
UpgradedDuplex__pause_stream(self)
}
#[inline]
pub(crate) fn resume_stream(&mut self) -> bool {
UpgradedDuplex__resume_stream(self)
}
}

// ── WindowsNamedPipe (cycle-break shim) ─────────────────────────────────────
Expand Down
4 changes: 2 additions & 2 deletions src/uws_sys/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl<const IS_SSL: bool> NewSocketHandler<IS_SSL> {
connected s => if s.is_established() { s.pause(); true } else { false },
connecting _c => false,
detached => true,
duplex _d => false, // TODO: pause/resume upgraded duplex
duplex d => d.pause_stream(),
pipe p => p.pause_stream(),
)
}
Expand All @@ -533,7 +533,7 @@ impl<const IS_SSL: bool> NewSocketHandler<IS_SSL> {
connected s => if s.is_established() { s.resume(); true } else { false },
connecting _c => false,
detached => true,
duplex _d => false, // TODO: pause/resume upgraded duplex
duplex d => d.resume_stream(),
pipe p => p.resume_stream(),
)
}
Expand Down
Loading
Loading