Commit f05b4316 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[Nearby] Allow BluetoothSocket::Close() to be called multiple times

After a successful payload transfer, BluetoothSocket::Close() will be
called multiple times (see crbug/1142071). In this CL, we check if the
underlying socket has already been disconnected and reset before trying
to disconnect and reset. Without this check, the Nearby process will
crash after a successful share.

Verified by manually testing Chromebook-->Android transfers.

Fixed: 1142071
Change-Id: I1b2fd0a092f1025f95c2eed5253862805e46b034
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2500982
Commit-Queue: Josh Nohle <nohle@chromium.org>
Commit-Queue: Ryan Hansberry <hansberry@chromium.org>
Auto-Submit: Josh Nohle <nohle@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821033}
parent f3283786
......@@ -314,8 +314,7 @@ BluetoothSocket::BluetoothSocket(
}
BluetoothSocket::~BluetoothSocket() {
if (socket_)
Close();
Close();
// These properties must be destroyed on the same sequence they are later run
// on. See |task_runner_|.
......@@ -347,8 +346,10 @@ OutputStream& BluetoothSocket::GetOutputStream() {
}
Exception BluetoothSocket::Close() {
socket_->Disconnect();
socket_.reset();
if (socket_) {
socket_->Disconnect();
socket_.reset();
}
Exception input_exception = input_stream_->Close();
Exception output_exception = output_stream_->Close();
if (input_exception.Ok() && output_exception.Ok())
......
......@@ -178,6 +178,10 @@ TEST_F(BluetoothSocketTest, TestClose) {
fake_socket_->SetOnDestroyCallback(run_loop.QuitClosure());
EXPECT_TRUE(bluetooth_socket_->Close().Ok());
run_loop.Run();
// Ensure that calls to Close() succeed even after the underlying socket is
// destroyed.
EXPECT_TRUE(bluetooth_socket_->Close().Ok());
}
TEST_F(BluetoothSocketTest, TestDestroy) {
......
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