fix: backport Node nested microtask fix (#20303)

This commit is contained in:
Jeremy Apthorp 2019-09-25 17:38:50 -04:00 committed by GitHub
parent 0653e31767
commit 593f1774e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 19 deletions

View file

@ -91,10 +91,18 @@ void NodeStreamLoader::NotifyComplete(int result) {
}
void NodeStreamLoader::ReadMore() {
if (is_reading_) {
// Calling read() can trigger the "readable" event again, making this
// function re-entrant. If we're already reading, we don't want to start
// a nested read, so short-circuit.
return;
}
is_reading_ = true;
auto weak = weak_factory_.GetWeakPtr();
// buffer = emitter.read()
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
isolate_, emitter_.Get(isolate_), "read", 0, nullptr, {0, 0});
DCHECK(weak) << "We shouldn't have been destroyed when calling read()";
// If there is no buffer read, wait until |readable| is emitted again.
v8::Local<v8::Value> buffer;
@ -110,13 +118,12 @@ void NodeStreamLoader::ReadMore() {
// Write buffer to mojo pipe asyncronously.
is_reading_ = false;
is_writing_ = true;
producer_->Write(
std::make_unique<mojo::StringDataSource>(
base::StringPiece(node::Buffer::Data(buffer),
node::Buffer::Length(buffer)),
mojo::StringDataSource::AsyncWritingMode::
STRING_STAYS_VALID_UNTIL_COMPLETION),
base::BindOnce(&NodeStreamLoader::DidWrite, weak_factory_.GetWeakPtr()));
producer_->Write(std::make_unique<mojo::StringDataSource>(
base::StringPiece(node::Buffer::Data(buffer),
node::Buffer::Length(buffer)),
mojo::StringDataSource::AsyncWritingMode::
STRING_STAYS_VALID_UNTIL_COMPLETION),
base::BindOnce(&NodeStreamLoader::DidWrite, weak));
}
void NodeStreamLoader::DidWrite(MojoResult result) {