Commit 24c51bc6 authored by Leonid Kaplan's avatar Leonid Kaplan Committed by Commit Bot

Fix possible shutdown leak in SerialWorker

SerialWorker instances could leak when the original task runner is
shutting down and no longer accepting tasks. This is because of the
implementation of ~PostTaskAndReplyRelay, which attempts to destroy its
own instance by DeleteSoon, which is also not accepted by the task
runner. This behavior produces warnings in ASAN builds, for example when
an instance of DnsConfigServicePosix::HostsReader is destroyed in
services_unittests.NetworkContextTest.

BUG=879152

Change-Id: I48df8e3171975ff6f11fe0676e456f8e8b756ec9
Reviewed-on: https://chromium-review.googlesource.com/1193344Reviewed-by: default avatarBence Béky <bnc@chromium.org>
Commit-Queue: Bence Béky <bnc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#588499}
parent 30cfcbcf
......@@ -11,7 +11,7 @@
namespace net {
SerialWorker::SerialWorker() : state_(IDLE) {}
SerialWorker::SerialWorker() : state_(IDLE), weak_factory_(this) {}
SerialWorker::~SerialWorker() = default;
......@@ -19,11 +19,16 @@ void SerialWorker::WorkNow() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
switch (state_) {
case IDLE:
// We are posting weak pointer to OnWorkJobFinished to avoid leak when
// PostTaskWithTraitsAndReply fails to post task back to the original
// task runner. In this case the callback is not destroyed, and the
// weak reference allows SerialWorker instance to be deleted.
base::PostTaskWithTraitsAndReply(
FROM_HERE,
{base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&SerialWorker::DoWork, this),
base::BindOnce(&SerialWorker::OnWorkJobFinished, this));
base::BindOnce(&SerialWorker::OnWorkJobFinished,
weak_factory_.GetWeakPtr()));
state_ = WORKING;
return;
case WORKING:
......
......@@ -10,6 +10,7 @@
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/task_traits.h"
#include "net/base/net_export.h"
......@@ -74,6 +75,8 @@ class NET_EXPORT_PRIVATE SerialWorker
State state_;
base::WeakPtrFactory<SerialWorker> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(SerialWorker);
};
......
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