Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion src/runtime/bake/DevServer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ pub enum TestingBatchEvents {
/// a message saying that new files have been seen. Once DevServer receives
/// that signal, or times out, it will "release" this batch.
Enabled(TestingBatch),
/// The harness released this batch while a bundle was already in flight.
/// `finalize_bundle_cleanup` releases it once no bundle is running.
Comment thread
robobun marked this conversation as resolved.
Resolved
ReleaseAfterBundle(TestingBatch),
}

/// There is only ever one bundle executing at the same time, since all bundles
Expand Down Expand Up @@ -1151,7 +1154,9 @@ impl Drop for DevServer {
}
}

if let TestingBatchEvents::Enabled(batch) = &mut self.testing_batch_events {
if let TestingBatchEvents::Enabled(batch) | TestingBatchEvents::ReleaseAfterBundle(batch) =
&mut self.testing_batch_events
{
drop(std::mem::replace(
&mut batch.entry_points,
EntryPointList::empty(),
Expand Down Expand Up @@ -3778,6 +3783,22 @@ fn finalize_bundle_cleanup(dev: &mut DevServer, bv2: &mut BundleV2, had_sent_hmr

dev.start_next_bundle_if_present();

// A batch released while this bundle was in flight waits for a moment with
// no bundle running. `start_next_bundle_if_present` may have started one,
// in which case the next cleanup releases the batch.
Comment thread
robobun marked this conversation as resolved.
Resolved
if matches!(
dev.testing_batch_events,
TestingBatchEvents::ReleaseAfterBundle(_)
) && dev.current_bundle.is_none()
{
let TestingBatchEvents::ReleaseAfterBundle(batch) =
core::mem::replace(&mut dev.testing_batch_events, TestingBatchEvents::Disabled)
else {
unreachable!()
};
dev.release_testing_batch(batch);
}

// Unref the ref added in `start_async_bundle`
if let Some(server) = dev.server.as_mut() {
server.on_static_request_complete();
Expand Down Expand Up @@ -4873,6 +4894,24 @@ pub(super) fn finalize_bundle(
}

impl DevServer {
/// Start a bundle for the files a testing batch collected, or tell the
/// harness the batch was empty. The caller must have no bundle in flight.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) fn release_testing_batch(&mut self, batch: TestingBatch) {
debug_assert!(self.current_bundle.is_none());
if batch.entry_points.set.count() == 0 {
self.publish(
HmrTopic::TestingWatchSynchronization,
&[MessageId::TestingWatchSynchronization.char(), 2],
Opcode::BINARY,
);
return;
}

self.start_async_bundle(batch.entry_points, true, Instant::now())
// bun.handleOom(err) — Rust aborts on OOM by default
.expect("OOM");
}

fn start_next_bundle_if_present(&mut self) {
debug_assert!(self.magic == Magic::Valid);
// Clear the current bundle
Expand Down
39 changes: 21 additions & 18 deletions src/runtime/bake/dev_server/hmr_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ impl HmrSocket {
}
x if x == IncomingMessageId::SetUrl as u8 => {
let pattern = &msg[1..];
// `pattern` is peer bytes; `FrameworkRouter::match_slow` requires
// an absolute path (`debug_assert!(path[0] == b'/')`).
Comment thread
robobun marked this conversation as resolved.
Resolved
if pattern.first() != Some(&b'/') {
return ws.close();
}
// SAFETY: JS-thread only; sole `&mut DevServer` for this scope.
let dev = unsafe { self.dev() };
let maybe_rbi = dev.route_to_bundle_index_slow(pattern);
Expand Down Expand Up @@ -204,36 +209,34 @@ impl HmrSocket {
);
}
}
super::TestingBatchEvents::EnableAfterBundle => {
// do not expose a websocket event that panics a release build
debug_assert!(false);
super::TestingBatchEvents::EnableAfterBundle
| super::TestingBatchEvents::ReleaseAfterBundle(_) => {
// A duplicate `H` before the pending batch activates or
// releases is a peer protocol violation; never assert on
// websocket input.
Comment thread
robobun marked this conversation as resolved.
Resolved
ws.close();
}
super::TestingBatchEvents::Enabled(_event_const) => {
Comment thread
robobun marked this conversation as resolved.
Resolved
// Replace-and-extract to satisfy borrowck.
let super::TestingBatchEvents::Enabled(mut event) = core::mem::replace(
let super::TestingBatchEvents::Enabled(batch) = core::mem::replace(
&mut dev.testing_batch_events,
super::TestingBatchEvents::Disabled,
) else {
unreachable!()
};
let _ = &mut event;

if event.entry_points.set.count() == 0 {
dev.publish(
HmrTopic::TestingWatchSynchronization,
&[MessageId::TestingWatchSynchronization.char(), 2],
bun_uws::Opcode::BINARY,
);
// A request for a route that is not bundled yet starts a
// bundle without consulting the batch, and
// `start_async_bundle` requires that no bundle is in
// flight. Hold the batch and release it once that bundle
// finishes.
Comment thread
robobun marked this conversation as resolved.
Resolved
if dev.current_bundle.is_some() {
dev.testing_batch_events =
super::TestingBatchEvents::ReleaseAfterBundle(batch);
return;
}

let timer = std::time::Instant::now();
dev.start_async_bundle(event.entry_points, true, timer)
// bun.handleOom(err) — Rust aborts on OOM by default
.expect("OOM");

// `event.entry_points.deinit(allocator)` → Drop handles this
dev.release_testing_batch(batch);
}
}
}
Expand Down Expand Up @@ -307,7 +310,7 @@ impl HmrSocket {
}
if field.contains(HmrTopic::MemoryVisualizer.as_bit()) {
dev.emit_memory_visualizer_events -= 1;
if dev.emit_incremental_visualizer_events == 0
if dev.emit_memory_visualizer_events == 0
&& dev.memory_visualizer_timer.state == EventLoopTimerState::ACTIVE
{
// Note (jsc/runtime crate cycle): `vm.timer` is `()` on the low-tier
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/bake/dev_server/memory_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub(crate) fn memory_cost_detailed(dev: &DevServer) -> MemoryCost {
// .testing_batch_events
match &dev.testing_batch_events {
TestingBatchEvents::Disabled => {}
TestingBatchEvents::Enabled(batch) => {
TestingBatchEvents::Enabled(batch) | TestingBatchEvents::ReleaseAfterBundle(batch) => {
other_bytes += memory_cost_array_hash_map(&batch.entry_points.set);
}
TestingBatchEvents::EnableAfterBundle => {}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/bake/dev_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ impl HotReloadEvent {

match &mut dev_ref.testing_batch_events {
TestingBatchEvents::Disabled => {}
TestingBatchEvents::Enabled(ev) => {
TestingBatchEvents::Enabled(ev) | TestingBatchEvents::ReleaseAfterBundle(ev) => {
bun_core::handle_oom(ev.append(&entry_points));
dev_ref.publish(
HmrTopic::TestingWatchSynchronization,
Expand Down
Loading
Loading