Commit 2aa3fb13 authored by hubbe@chromium.org's avatar hubbe@chromium.org

Fix posix IPC channel hanging problem.

If a channel closes right before a send call, listeners might not be notified of
the problem, which can cause hangs. This CL fixes that and adds a test that makes
sure that this does not happen in the future.

This is cl/30133002 + a memory leak fix.

BUG=338709

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@250964 0039d316-1c4b-4281-b951-d872f2087c98
parent 44929ee9
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "base/posix/global_descriptors.h" #include "base/posix/global_descriptors.h"
#include "base/process/process_handle.h" #include "base/process/process_handle.h"
#include "base/rand_util.h" #include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
...@@ -327,7 +328,7 @@ bool Channel::ChannelImpl::CreatePipe( ...@@ -327,7 +328,7 @@ bool Channel::ChannelImpl::CreatePipe(
bool Channel::ChannelImpl::Connect() { bool Channel::ChannelImpl::Connect() {
if (server_listen_pipe_ == -1 && pipe_ == -1) { if (server_listen_pipe_ == -1 && pipe_ == -1) {
DLOG(INFO) << "Channel creation failed: " << pipe_name_; DLOG(WARNING) << "Channel creation failed: " << pipe_name_;
return false; return false;
} }
...@@ -519,10 +520,18 @@ bool Channel::ChannelImpl::Send(Message* message) { ...@@ -519,10 +520,18 @@ bool Channel::ChannelImpl::Send(Message* message) {
Logging::GetInstance()->OnSendMessage(message, ""); Logging::GetInstance()->OnSendMessage(message, "");
#endif // IPC_MESSAGE_LOG_ENABLED #endif // IPC_MESSAGE_LOG_ENABLED
if (!waiting_connect_ && pipe_ == -1) {
delete message;
return false;
}
message->TraceMessageBegin(); message->TraceMessageBegin();
output_queue_.push(message); output_queue_.push(message);
if (!is_blocked_on_write_ && !waiting_connect_) { if (!is_blocked_on_write_ && !waiting_connect_) {
return ProcessOutgoingMessages(); if (!ProcessOutgoingMessages()) {
ClosePipeOnError();
return false;
}
} }
return true; return true;
...@@ -707,7 +716,11 @@ bool Channel::ChannelImpl::AcceptConnection() { ...@@ -707,7 +716,11 @@ bool Channel::ChannelImpl::AcceptConnection() {
// In server mode we will send a hello message when we receive one from a // In server mode we will send a hello message when we receive one from a
// client. // client.
waiting_connect_ = false; waiting_connect_ = false;
return ProcessOutgoingMessages(); if (!ProcessOutgoingMessages()) {
ClosePipeOnError();
return false;
}
return true;
} else if (mode_ & MODE_SERVER_FLAG) { } else if (mode_ & MODE_SERVER_FLAG) {
waiting_connect_ = true; waiting_connect_ = true;
return true; return true;
......
...@@ -41,7 +41,9 @@ class IPCChannelPosixTestListener : public IPC::Listener { ...@@ -41,7 +41,9 @@ class IPCChannelPosixTestListener : public IPC::Listener {
}; };
IPCChannelPosixTestListener(bool quit_only_on_message) IPCChannelPosixTestListener(bool quit_only_on_message)
: status_(DISCONNECTED), quit_only_on_message_(quit_only_on_message) {} : status_(DISCONNECTED),
quit_only_on_message_(quit_only_on_message) {
}
virtual ~IPCChannelPosixTestListener() {} virtual ~IPCChannelPosixTestListener() {}
...@@ -83,7 +85,13 @@ class IPCChannelPosixTestListener : public IPC::Listener { ...@@ -83,7 +85,13 @@ class IPCChannelPosixTestListener : public IPC::Listener {
STATUS status() { return status_; } STATUS status() { return status_; }
void QuitRunLoop() { void QuitRunLoop() {
base::MessageLoopForIO::current()->QuitNow(); base::MessageLoopForIO* loop = base::MessageLoopForIO::current();
if (loop->is_running()) {
loop->QuitNow();
} else {
// Die as soon as Run is called.
loop->PostTask(FROM_HERE, loop->QuitClosure());
}
} }
private: private:
...@@ -186,7 +194,7 @@ void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) { ...@@ -186,7 +194,7 @@ void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) {
// in the case of a bad test. Usually, the run loop will quit sooner than // in the case of a bad test. Usually, the run loop will quit sooner than
// that because all tests use a IPCChannelPosixTestListener which quits the // that because all tests use a IPCChannelPosixTestListener which quits the
// current run loop on any channel activity. // current run loop on any channel activity.
loop->PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(), delay); loop->PostDelayedTask(FROM_HERE, loop->QuitClosure(), delay);
loop->Run(); loop->Run();
} }
...@@ -228,6 +236,45 @@ TEST_F(IPCChannelPosixTest, BasicConnected) { ...@@ -228,6 +236,45 @@ TEST_F(IPCChannelPosixTest, BasicConnected) {
ASSERT_FALSE(channel2.AcceptsConnections()); ASSERT_FALSE(channel2.AcceptsConnections());
} }
// If a connection closes right before a Send() call, we may end up closing
// the connection without notifying the listener, which can cause hangs in
// sync_message_filter and others. Make sure the listener is notified.
TEST_F(IPCChannelPosixTest, SendHangTest) {
IPCChannelPosixTestListener out_listener(true);
IPCChannelPosixTestListener in_listener(true);
IPC::ChannelHandle in_handle("IN");
IPC::Channel in_chan(in_handle, IPC::Channel::MODE_SERVER, &in_listener);
base::FileDescriptor out_fd(in_chan.TakeClientFileDescriptor(), false);
IPC::ChannelHandle out_handle("OUT", out_fd);
IPC::Channel out_chan(out_handle, IPC::Channel::MODE_CLIENT, &out_listener);
ASSERT_TRUE(in_chan.Connect());
ASSERT_TRUE(out_chan.Connect());
in_chan.Close(); // simulate remote process dying at an unfortunate time.
// Send will fail, because it cannot write the message.
ASSERT_FALSE(out_chan.Send(new IPC::Message(
0, // routing_id
kQuitMessage, // message type
IPC::Message::PRIORITY_NORMAL)));
ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status());
}
// If a connection closes right before a Connect() call, we may end up closing
// the connection without notifying the listener, which can cause hangs in
// sync_message_filter and others. Make sure the listener is notified.
TEST_F(IPCChannelPosixTest, AcceptHangTest) {
IPCChannelPosixTestListener out_listener(true);
IPCChannelPosixTestListener in_listener(true);
IPC::ChannelHandle in_handle("IN");
IPC::Channel in_chan(in_handle, IPC::Channel::MODE_SERVER, &in_listener);
base::FileDescriptor out_fd(in_chan.TakeClientFileDescriptor(), false);
IPC::ChannelHandle out_handle("OUT", out_fd);
IPC::Channel out_chan(out_handle, IPC::Channel::MODE_CLIENT, &out_listener);
ASSERT_TRUE(in_chan.Connect());
in_chan.Close(); // simulate remote process dying at an unfortunate time.
ASSERT_FALSE(out_chan.Connect());
ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status());
}
TEST_F(IPCChannelPosixTest, AdvancedConnected) { TEST_F(IPCChannelPosixTest, AdvancedConnected) {
// Test creating a connection to an external process. // Test creating a connection to an external process.
IPCChannelPosixTestListener listener(false); IPCChannelPosixTestListener listener(false);
......
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