Repository navigation
refactor: Clean up the implementation of the registerStreamProtocol - #11357
Conversation
|
It seems that we still have a race condition in URLRequestStreamJob, I'm going to try if I can avoid accessing the pending_io_buf_ from 2 different threads. |
tarruda
left a comment
There was a problem hiding this comment.
LGTM, nice improvements. The only reason I didn't use base:Callback in the first place was because it was only being invoked once, but I see you took care of that.
| DCHECK(js_handlers_.find(event_name) == js_handlers_.end()); | ||
| void EventSubscriber::On(const std::string& event, EventCallback&& callback) { // NOLINT | ||
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | ||
| callbacks_[event] = callback; |
There was a problem hiding this comment.
Maybe an assertion to verify the same event is not subscribed twice?
|
|
||
| URLRequestStreamJob::~URLRequestStreamJob() { | ||
| if (subscriber_) { | ||
| content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE, |
There was a problem hiding this comment.
Didn't knew this function existed 👍
Anyway, the only reason I added SafePtr was to encapsulate "deletion in the UI thread" in case other classes that are deleted outside UI thread needed to use EventSubscriber and didn't want to repeat this code in their destructors.
| subscriber_->On("error", &URLRequestStreamJob::OnError); | ||
| subscriber_.reset(new mate::EventSubscriber(isolate, data.GetHandle())); | ||
| subscriber_->On("data", base::Bind(&URLRequestStreamJob::OnData, | ||
| weak_factory_.GetWeakPtr())); |
There was a problem hiding this comment.
So if I understood correctly, the callback is thread-safe and will synchronize internally if the URLJob happens to be destroyed at the same time the event is being emitted?
|
@zcbenz should I close #11283 then? Just for reference, this is the crash #11283 was trying to fix: As you can see, the Deleter was running in the UI thread before the IO function that posted it could release the lock. I assume this won't be a problem with |
I'm afraid the current tests don't exercise |
Just to be clear, |
|
@tarruda I have refactored the code so I'll merge when this change passes your tests. |
| }; | ||
| void OnData(mate::Arguments* args); | ||
| void OnEnd(mate::Arguments* args); | ||
| void OnError(mate::Arguments* args); |
There was a problem hiding this comment.
If we are going to make this class specific to Streams instead of generic event subscribers, maybe it should be renamed to StreamSubscriber or StreamConsumer?
| const char* data = node::Buffer::Data(buf); | ||
| size_t length = node::Buffer::Length(buf); | ||
| std::vector<char> buffer(length); | ||
| buffer.assign(data, data + length); |
There was a problem hiding this comment.
Tip: Initialize the vector with the source buffer to avoid zeroing the buffer first only to replace the content with actual data.
std::vector<char> buffer(data, length);| } | ||
|
|
||
| // Copy the data from Buffer. | ||
| // TODO(zcbenz): Is there a way to move the data? |
There was a problem hiding this comment.
I think there's no way to guarantee that no other JS code is referencing the buffer
|
@zcbenz I just started testing this PR on our app but it crashed at startup. The following patch should fix the crash and a few problems with the refactor: diff --git a/atom/browser/net/url_request_stream_job.cc b/atom/browser/net/url_request_stream_job.cc
index 4474f94..ae625a8 100644
--- a/atom/browser/net/url_request_stream_job.cc
+++ b/atom/browser/net/url_request_stream_job.cc
@@ -117,7 +117,9 @@ void URLRequestStreamJob::OnData(std::vector<char>&& buffer) { // NOLINT
void URLRequestStreamJob::OnEnd() {
ended_ = true;
- ReadRawDataComplete(0);
+ if (pending_buf_ && write_buffer_.empty()) {
+ ReadRawDataComplete(0);
+ }
}
void URLRequestStreamJob::OnError() {
@@ -126,26 +128,29 @@ void URLRequestStreamJob::OnError() {
}
int URLRequestStreamJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
+ if (!write_buffer_.empty()) {
+ // Read from the write buffer and clear them after reading.
+ int len = BufferCopy(&write_buffer_, dest, dest_size);
+ write_buffer_.erase(write_buffer_.begin(), write_buffer_.begin() + len);
+ return len;
+ }
+
if (ended_)
+ // Buffer empty and will no longer emit data events.
return 0;
// When write_buffer_ is empty, there is no data valable yet, we have to save
// the dest buffer util DataAvailable.
- if (write_buffer_.empty()) {
- pending_buf_ = dest;
- pending_buf_size_ = dest_size;
- return net::ERR_IO_PENDING;
- }
-
- // Read from the write buffer and clear them after reading.
- int len = BufferCopy(&write_buffer_, dest, dest_size);
- write_buffer_.erase(write_buffer_.begin(), write_buffer_.begin() + len);
- return len;
+ pending_buf_ = dest;
+ pending_buf_size_ = dest_size;
+ return net::ERR_IO_PENDING;
}
void URLRequestStreamJob::DoneReading() {
- content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
- std::move(subscriber_));
+ if (subscriber_) {
+ content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
+ std::move(subscriber_));
+ }
write_buffer_.clear();
}Reasoning for each change:
IMHO the same reasoning behind only signaling EOF after buffered data is ready should apply to error handling, though I have not included the required changes in the above patch. For example, if the stream starts sending data and then a closed connection causes an error, we should allow the job to read the partial data and only signal the error later by passing it to Since you've restructured the code quite a bit beyond using |
92f5d7a to
cfae831
Compare
cfae831 to
f59c95e
Compare
|
@deepak1556 I discussed privately about this PR with @zcbenz and I don't recommend merging it. Last year I had created a build of this PR and asked our QA team to test it, and there were random crashes happening. Unfortunately due to the concurrent nature of the code, I could not reliably reproduce the crashes or find what's wrong with this PR. |
|
Pasting the discussion with @tarruda from slack,
|
|
@tarruda can you review this before it gets merged. Thanks! |
f59c95e to
fc7ec26
Compare
We should update cpplint for this, but let's do it in other PR.
Some methods are now private so no more need to assert threads.
fc7ec26 to
df87b80
Compare
|
No Release Notes |
This is an alternative fix to #11283.
This PR removes the manual management of
EventSubscriber's lifetime, and uses existing tools for the same purpose. It should both simplify the code and solve possible crashes.Notes: no-notes