Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
56 changes: 56 additions & 0 deletions src/workerd/api/js-readable-stream-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,62 @@ KJ_TEST("JsReadableStream tee carries the buffer and nullifies the original") {
});
}

// The isolate's external memory, including adjustments not yet applied.
int64_t getExternalMemory(jsg::Lock& js) {
js.getExternalMemoryTarget()->applyDeferredMemoryUpdate();
return js.v8Isolate->GetExternalMemory();
}

KJ_TEST("JsReadableStream reports its retransmit buffer as external memory, once") {
TestFixture testFixture(
{.autogateKeys = kj::arr(util::AutogateKey::BODY_BUFFER_EXTERNAL_MEMORY)});
testFixture.runInIoContext([](const TestFixture::Environment& env) {
auto& js = env.js;
constexpr size_t size = 1024 * 1024;
auto source = jsg::JsArrayBuffer::create(js, size);

auto before = getExternalMemory(js);
JsReadableStream stream(js, jsg::JsBufferSource(source).addRef(js));
auto created = getExternalMemory(js) - before;
KJ_EXPECT(created >= size, created);
// The stream itself only adds bookkeeping on top of the copied bytes.
KJ_EXPECT(created < size + 64 * 1024, created);

// Clones and tee branches share the one buffer, so they don't count it again.
auto clone = KJ_ASSERT_NONNULL(stream.tryClone(js));
auto tee = stream.tee(js);
auto shared = getExternalMemory(js) - before;
KJ_EXPECT(shared < size + 64 * 1024, shared);
});
}

KJ_TEST("JsReadableStream retransmit buffer releases its external memory") {
TestFixture testFixture(
{.autogateKeys = kj::arr(util::AutogateKey::BODY_BUFFER_EXTERNAL_MEMORY)});
testFixture.runInIoContext([](const TestFixture::Environment& env) {
auto& js = env.js;
constexpr size_t size = 1024 * 1024;

auto before = getExternalMemory(js);
auto buffer = kj::rc<JsReadableStream::Buffer>(js, kj::heapArray<kj::byte>(size));
KJ_EXPECT(getExternalMemory(js) - before == size);
buffer = nullptr;
KJ_EXPECT(getExternalMemory(js) == before);
});
}

KJ_TEST("JsReadableStream retransmit buffer reports no external memory with its autogate off") {
TestFixture testFixture({.ignoreAllAutogatesEnv = true});
testFixture.runInIoContext([](const TestFixture::Environment& env) {
auto& js = env.js;
constexpr size_t size = 1024 * 1024;

auto before = getExternalMemory(js);
auto buffer = kj::rc<JsReadableStream::Buffer>(js, kj::heapArray<kj::byte>(size));
KJ_EXPECT(getExternalMemory(js) == before);
});
}

KJ_TEST("JsReadableStream detach takes over the stream, leaving a disturbed husk") {
TestFixture testFixture;
testFixture.runInIoContext([](const TestFixture::Environment& env) -> kj::Promise<void> {
Expand Down
11 changes: 7 additions & 4 deletions src/workerd/api/js-readable-stream.c++
Original file line number Diff line number Diff line change
Expand Up @@ -756,17 +756,20 @@ kj::Promise<DeferredProxy<void>> pumpQueuedTsStream(jsg::Lock& js,
// ownership of that heap buffer without changing its address, so view remains valid.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdangling-field"
JsReadableStream::Buffer::Buffer(kj::Array<const kj::byte> data)
JsReadableStream::Buffer::Buffer(jsg::Lock& js, kj::Array<const kj::byte> data)
: owned(kj::mv(data)),
view(owned) {}
view(owned),
externalMemoryAdjustment(js.getExternalMemoryAdjustment(
util::Autogate::isEnabled(util::AutogateKey::BODY_BUFFER_EXTERNAL_MEMORY) ? owned.size()
: 0)) {}
#pragma clang diagnostic pop

// Blob contents live in a V8 ArrayBuffer. The streams built from this Buffer are read on the
// kj event loop, where the isolate's MPK-protected sandbox pages are unreadable, so take a
// kj-heap copy. It is shared by every stream derived from this Buffer -- rewinds and tee
// branches alike.
JsReadableStream::Buffer::Buffer(jsg::Lock& js, jsg::Ref<Blob> data)
: Buffer(kj::heapArray(data->getData(js))) {}
: Buffer(js, kj::heapArray(data->getData(js))) {}

namespace {

Expand Down Expand Up @@ -817,7 +820,7 @@ JsReadableStream::JsReadableStream(jsg::Lock&, jsg::JsRef<jsg::JsObject> obj)
}) {}

JsReadableStream::JsReadableStream(jsg::Lock& js, kj::Array<const kj::byte> data)
: impl(bufferBackedImpl(js, kj::rc<Buffer>(kj::mv(data)))) {}
: impl(bufferBackedImpl(js, kj::rc<Buffer>(js, kj::mv(data)))) {}

JsReadableStream::JsReadableStream(jsg::Lock& js, kj::String data)
: JsReadableStream(js, webstreams::stringToBytes(kj::mv(data))) {}
Expand Down
8 changes: 7 additions & 1 deletion src/workerd/api/js-readable-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ class JsReadableStream final {
kj::Array<const kj::byte> owned;
kj::ArrayPtr<const kj::byte> view;

explicit Buffer(kj::Array<const kj::byte> data);
// Reports `owned` to the isolate as external memory. V8 sees only a small wrapper for each
// Request or Response, so without this, bodies held by objects awaiting collection don't
// prompt a GC and aren't counted against the isolate's memory limit. Reports nothing unless
// the BODY_BUFFER_EXTERNAL_MEMORY autogate is enabled.
jsg::ExternalMemoryAdjustment externalMemoryAdjustment;

Buffer(jsg::Lock& js, kj::Array<const kj::byte> data);
Buffer(jsg::Lock& js, jsg::Ref<Blob> data);
};

Expand Down
6 changes: 5 additions & 1 deletion src/workerd/util/autogate.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ namespace workerd::util {
/* Worker::Isolate's constructor skips its MemoryPressureNotification(kModerate) call, which \
flushes the Liftoff code of every Wasm module in the process and empties the isolate \
group's memory pools. */ \
V(SKIP_ISOLATE_MEMORY_PRESSURE_HINT)
V(SKIP_ISOLATE_MEMORY_PRESSURE_HINT) \
/* Counts the bytes behind a Request or Response body built from in-memory data, such as a \
string or BufferSource, as the isolate's external memory, so that they prompt GCs and the \
isolate's memory limit applies to them. See JsReadableStream::Buffer. */ \
V(BODY_BUFFER_EXTERNAL_MEMORY)
// clang-format on
// --------------------------------------------------------------------------------------

Expand Down
Loading