From b0da40d8de7d8c7d4a88b4203ce5b47b009b51b0 Mon Sep 17 00:00:00 2001
From: "perkj@chromium.org"
 <perkj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Fri, 21 Sep 2012 08:00:12 +0000
Subject: [PATCH] Fix asan test failure in P2PSocketClient::Init. Make sure 
 P2PSocketClient::Init only access |delegate_| on the delegate_message_loop_.

Please see the bug for more information.

BUG=150842
TEST= https://apprtc.appspot.com/?r&debug=loopback - I have not been able to reproduce the problem with asan since it will very rarely happen.

TBR=sergeyu@chromium.org

Review URL: https://codereview.chromium.org/10962010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157944 0039d316-1c4b-4281-b951-d872f2087c98
---
 content/renderer/p2p/socket_client.cc | 28 ++++++++++++++++++---------
 content/renderer/p2p/socket_client.h  |  5 +++++
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/content/renderer/p2p/socket_client.cc b/content/renderer/p2p/socket_client.cc
index 76357cd2b45fd..9a99d55e8cc1f 100644
--- a/content/renderer/p2p/socket_client.cc
+++ b/content/renderer/p2p/socket_client.cc
@@ -28,16 +28,21 @@ void P2PSocketClient::Init(
     const net::IPEndPoint& local_address,
     const net::IPEndPoint& remote_address,
     P2PSocketClient::Delegate* delegate) {
-  if (!ipc_message_loop_->BelongsToCurrentThread()) {
-    ipc_message_loop_->PostTask(
-        FROM_HERE, base::Bind(&P2PSocketClient::Init, this, type, local_address,
-                              remote_address, delegate));
-    return;
-  }
-
-  DCHECK_EQ(state_, STATE_UNINITIALIZED);
-  state_ = STATE_OPENING;
+  DCHECK(delegate_message_loop_->BelongsToCurrentThread());
+  // |delegate_| is only accessesed on |delegate_message_loop_|.
   delegate_ = delegate;
+
+  ipc_message_loop_->PostTask(
+      FROM_HERE, base::Bind(&P2PSocketClient::DoInit, this, type, local_address,
+                            remote_address));
+}
+
+void P2PSocketClient::DoInit(P2PSocketType type,
+                             const net::IPEndPoint& local_address,
+                             const net::IPEndPoint& remote_address) {
+  DCHECK_EQ(state_, STATE_UNINITIALIZED);
+  DCHECK(delegate_);
+  state_ = STATE_OPENING;
   socket_id_ = dispatcher_->RegisterClient(this);
   dispatcher_->SendP2PMessage(new P2PHostMsg_CreateSocket(
       type, socket_id_, local_address, remote_address));
@@ -68,6 +73,7 @@ void P2PSocketClient::Close() {
 }
 
 void P2PSocketClient::DoClose() {
+  DCHECK(ipc_message_loop_->BelongsToCurrentThread());
   if (dispatcher_) {
     if (state_ == STATE_OPEN || state_ == STATE_OPENING ||
         state_ == STATE_ERROR) {
@@ -95,6 +101,7 @@ void P2PSocketClient::OnSocketCreated(const net::IPEndPoint& address) {
 }
 
 void P2PSocketClient::DeliverOnSocketCreated(const net::IPEndPoint& address) {
+  DCHECK(delegate_message_loop_->BelongsToCurrentThread());
   if (delegate_)
     delegate_->OnOpen(address);
 }
@@ -118,6 +125,7 @@ void P2PSocketClient::OnIncomingTcpConnection(const net::IPEndPoint& address) {
 
 void P2PSocketClient::DeliverOnIncomingTcpConnection(
     const net::IPEndPoint& address, scoped_refptr<P2PSocketClient> new_client) {
+  DCHECK(delegate_message_loop_->BelongsToCurrentThread());
   if (delegate_)
     delegate_->OnIncomingTcpConnection(address, new_client);
 }
@@ -131,6 +139,7 @@ void P2PSocketClient::OnError() {
 }
 
 void P2PSocketClient::DeliverOnError() {
+  DCHECK(delegate_message_loop_->BelongsToCurrentThread());
   if (delegate_)
     delegate_->OnError();
 }
@@ -146,6 +155,7 @@ void P2PSocketClient::OnDataReceived(const net::IPEndPoint& address,
 
 void P2PSocketClient::DeliverOnDataReceived(const net::IPEndPoint& address,
                                             const std::vector<char>& data) {
+  DCHECK(delegate_message_loop_->BelongsToCurrentThread());
   if (delegate_)
     delegate_->OnDataReceived(address, data);
 }
diff --git a/content/renderer/p2p/socket_client.h b/content/renderer/p2p/socket_client.h
index 103f729fc9df6..d74107f2ef492 100644
--- a/content/renderer/p2p/socket_client.h
+++ b/content/renderer/p2p/socket_client.h
@@ -93,6 +93,11 @@ class P2PSocketClient : public base::RefCountedThreadSafe<P2PSocketClient> {
   void DeliverOnDataReceived(const net::IPEndPoint& address,
                              const std::vector<char>& data);
 
+  // Scheduled on the IPC thread to finish initialization.
+  void DoInit(P2PSocketType type,
+              const net::IPEndPoint& local_address,
+              const net::IPEndPoint& remote_address);
+
   // Scheduled on the IPC thread to finish closing the connection.
   void DoClose();