Commit c54cf063 authored by Ehsan Chiniforooshan's avatar Ehsan Chiniforooshan Committed by Commit Bot

Tracing: fix a race if streaming at shutdown

Bug: 832981
Change-Id: I3711f0f03306a74b9a062f388beb2f3915eecb91
Reviewed-on: https://chromium-review.googlesource.com/1024969
Commit-Queue: Ehsan Chiniforooshan <chiniforooshan@chromium.org>
Reviewed-by: default avataroysteine <oysteine@chromium.org>
Reviewed-by: default avatarDirk Pranke <dpranke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553589}
parent 082307a4
...@@ -296,14 +296,8 @@ IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup, ...@@ -296,14 +296,8 @@ IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup,
base::Time::Now().ToInternalValue()); base::Time::Now().ToInternalValue());
} }
// Flaky mojo CHECK stop on ChromeOS. https://crbug.com/832981
#if defined(OS_CHROMEOS)
#define MAYBE_StartupTracingThrottle DISABLED_StartupTracingThrottle
#else
#define MAYBE_StartupTracingThrottle StartupTracingThrottle
#endif
IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup, IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTestOnStartup,
MAYBE_StartupTracingThrottle) { StartupTracingThrottle) {
// The startup scenario should *not* be started, since not enough // The startup scenario should *not* be started, since not enough
// time has elapsed since the last upload (set in the PRE_ above). // time has elapsed since the last upload (set in the PRE_ above).
EXPECT_FALSE( EXPECT_FALSE(
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
#include "base/task_scheduler/task_traits.h" #include "base/task_scheduler/task_traits.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
...@@ -97,6 +98,7 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> { ...@@ -97,6 +98,7 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> {
// shutdown. We either will not write to the stream afterwards or do not care // shutdown. We either will not write to the stream afterwards or do not care
// what happens to what we try to write. // what happens to what we try to write.
void CloseStream() { void CloseStream() {
base::AutoLock mutex(stream_lock_);
DCHECK(stream_.is_valid()); DCHECK(stream_.is_valid());
stream_.reset(); stream_.reset();
} }
...@@ -108,6 +110,14 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> { ...@@ -108,6 +110,14 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> {
} }
private: private:
// Handles synchronize writes to |stream_|, if the stream is not already
// closed.
void WriteToStream(const std::string& data) {
base::AutoLock mutex(stream_lock_);
if (stream_->is_valid())
mojo::BlockingCopyFromString(data, stream_);
}
// Called from |background_task_runner_|. // Called from |background_task_runner_|.
void OnRecorderDataChange(const std::string& label) { void OnRecorderDataChange(const std::string& label) {
// Bail out if we are in the middle of writing events for another label to // Bail out if we are in the middle of writing events for another label to
...@@ -145,7 +155,7 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> { ...@@ -145,7 +155,7 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> {
if (all_finished) { if (all_finished) {
StreamMetadata(); StreamMetadata();
if (!stream_is_empty_ && agent_label_.empty()) { if (!stream_is_empty_ && agent_label_.empty()) {
mojo::BlockingCopyFromString("}", stream_); WriteToStream("}");
stream_is_empty_ = false; stream_is_empty_ = false;
} }
// Recorder connections should be closed on their binding thread. // Recorder connections should be closed on their binding thread.
...@@ -193,11 +203,11 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> { ...@@ -193,11 +203,11 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> {
std::string escaped; std::string escaped;
base::EscapeJSONString(recorder->data(), false /* put_in_quotes */, base::EscapeJSONString(recorder->data(), false /* put_in_quotes */,
&escaped); &escaped);
mojo::BlockingCopyFromString(prefix + escaped, stream_); WriteToStream(prefix + escaped);
} else { } else {
if (prefix.empty() && !stream_is_empty_) if (prefix.empty() && !stream_is_empty_)
prefix = ","; prefix = ",";
mojo::BlockingCopyFromString(prefix + recorder->data(), stream_); WriteToStream(prefix + recorder->data());
} }
stream_is_empty_ = false; stream_is_empty_ = false;
recorder->clear_data(); recorder->clear_data();
...@@ -206,13 +216,13 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> { ...@@ -206,13 +216,13 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> {
if (json_field_name_written_) { if (json_field_name_written_) {
switch (data_type) { switch (data_type) {
case mojom::TraceDataType::ARRAY: case mojom::TraceDataType::ARRAY:
mojo::BlockingCopyFromString("]", stream_); WriteToStream("]");
break; break;
case mojom::TraceDataType::OBJECT: case mojom::TraceDataType::OBJECT:
mojo::BlockingCopyFromString("}", stream_); WriteToStream("}");
break; break;
case mojom::TraceDataType::STRING: case mojom::TraceDataType::STRING:
mojo::BlockingCopyFromString("\"", stream_); WriteToStream("\"");
break; break;
default: default:
NOTREACHED(); NOTREACHED();
...@@ -238,9 +248,8 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> { ...@@ -238,9 +248,8 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> {
if (!metadata_->empty() && if (!metadata_->empty() &&
base::JSONWriter::Write(*metadata_, &metadataJSON)) { base::JSONWriter::Write(*metadata_, &metadataJSON)) {
std::string prefix = stream_is_empty_ ? "{\"" : ",\""; std::string prefix = stream_is_empty_ ? "{\"" : ",\"";
mojo::BlockingCopyFromString( WriteToStream(prefix + std::string(kMetadataTraceLabel) +
prefix + std::string(kMetadataTraceLabel) + "\":" + metadataJSON, "\":" + metadataJSON);
stream_);
stream_is_empty_ = false; stream_is_empty_ = false;
} }
} }
...@@ -248,6 +257,7 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> { ...@@ -248,6 +257,7 @@ class Coordinator::TraceStreamer : public base::SupportsWeakPtr<TraceStreamer> {
// The stream to which trace events from different agents should be // The stream to which trace events from different agents should be
// serialized, eventually. This is set when tracing is stopped. // serialized, eventually. This is set when tracing is stopped.
mojo::ScopedDataPipeProducerHandle stream_; mojo::ScopedDataPipeProducerHandle stream_;
base::Lock stream_lock_;
std::string agent_label_; std::string agent_label_;
scoped_refptr<base::SequencedTaskRunner> main_task_runner_; scoped_refptr<base::SequencedTaskRunner> main_task_runner_;
base::WeakPtr<Coordinator> coordinator_; base::WeakPtr<Coordinator> coordinator_;
......
...@@ -342,6 +342,5 @@ ...@@ -342,6 +342,5 @@
-DeclarativeNetRequestResourceTypeBrowserTest.Test2/1 -DeclarativeNetRequestResourceTypeBrowserTest.Test2/1
-MediaEngagementSessionRestoreBrowserTest.RestoredSession_Playback_MEI -MediaEngagementSessionRestoreBrowserTest.RestoredSession_Playback_MEI
-AutoplayExtensionBrowserTest.* -AutoplayExtensionBrowserTest.*
-ChromeTracingDelegateBrowserTestOnStartup.StartupTracingThrottle
# See comment at top of file regarding adding test exclusions. # See comment at top of file regarding adding test exclusions.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment