Skip to content

Commit

Permalink
fix: race condition in NodeStreamLoader (#19869)
Browse files Browse the repository at this point in the history
* fix: race condition in NodeStreamLoader

* nit: add comments
  • Loading branch information
trop[bot] authored and codebytere committed Aug 21, 2019
1 parent 241b2a9 commit 36a5ba0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
19 changes: 15 additions & 4 deletions shell/browser/net/node_stream_loader.cc
Expand Up @@ -71,12 +71,18 @@ void NodeStreamLoader::Start(network::ResourceResponseHead head) {
base::BindRepeating(&NodeStreamLoader::NotifyComplete, weak, net::OK));
On("error", base::BindRepeating(&NodeStreamLoader::NotifyComplete, weak,
net::ERR_FAILED));
On("readable", base::BindRepeating(&NodeStreamLoader::ReadMore, weak));
On("readable", base::BindRepeating(&NodeStreamLoader::NotifyReadable, weak));
}

void NodeStreamLoader::NotifyReadable() {
if (!readable_)
ReadMore();
readable_ = true;
}

void NodeStreamLoader::NotifyComplete(int result) {
// Wait until write finishes or fails.
if (is_writing_) {
if (is_reading_ || is_writing_) {
ended_ = true;
result_ = result;
return;
Expand All @@ -87,19 +93,24 @@ void NodeStreamLoader::NotifyComplete(int result) {
}

void NodeStreamLoader::ReadMore() {
is_reading_ = true;
// buffer = emitter.read()
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
isolate_, emitter_.Get(isolate_), "read", 0, nullptr, {0, 0});

// If there is no buffer read, wait until |readable| is emitted again.
v8::Local<v8::Value> buffer;
if (!ret.ToLocal(&buffer) || !node::Buffer::HasInstance(buffer))
if (!ret.ToLocal(&buffer) || !node::Buffer::HasInstance(buffer)) {
readable_ = false;
is_reading_ = false;
return;
}

// Hold the buffer until the write is done.
buffer_.Reset(isolate_, buffer);

// Write buffer to mojo pipe asyncronously.
is_reading_ = false;
is_writing_ = true;
producer_->Write(
std::make_unique<mojo::StringDataSource>(
Expand All @@ -118,7 +129,7 @@ void NodeStreamLoader::DidWrite(MojoResult result) {
return;
}

if (result == MOJO_RESULT_OK)
if (result == MOJO_RESULT_OK && readable_)
ReadMore();
else
NotifyComplete(net::ERR_FAILED);
Expand Down
9 changes: 9 additions & 0 deletions shell/browser/net/node_stream_loader.h
Expand Up @@ -39,6 +39,7 @@ class NodeStreamLoader : public network::mojom::URLLoader {
using EventCallback = base::RepeatingCallback<void()>;

void Start(network::ResourceResponseHead head);
void NotifyReadable();
void NotifyComplete(int result);
void ReadMore();
void DidWrite(MojoResult result);
Expand Down Expand Up @@ -69,11 +70,19 @@ class NodeStreamLoader : public network::mojom::URLLoader {
// Whether we are in the middle of write.
bool is_writing_ = false;

// Whether we are in the middle of a stream.read().
bool is_reading_ = false;

// When NotifyComplete is called while writing, we will save the result and
// quit with it after the write is done.
bool ended_ = false;
int result_ = net::OK;

// When the stream emits the readable event, we only want to start reading
// data if the stream was not readable before, so we store the state in a
// flag.
bool readable_ = false;

// Store the V8 callbacks to unsubscribe them later.
std::map<std::string, v8::Global<v8::Value>> handlers_;

Expand Down

0 comments on commit 36a5ba0

Please sign in to comment.