diff --git a/p2p/base/connection.cc b/p2p/base/connection.cc index aa50e267a7..e0dcf7f160 100644 --- a/p2p/base/connection.cc +++ b/p2p/base/connection.cc @@ -282,7 +282,8 @@ int ConnectionRequest::resend_delay() { Connection::Connection(Port* port, size_t index, const Candidate& remote_candidate) - : id_(rtc::CreateRandomId()), + : network_thread_(port->thread()), + id_(rtc::CreateRandomId()), port_(port), local_candidate_index_(index), remote_candidate_(remote_candidate), @@ -304,6 +305,7 @@ Connection::Connection(Port* port, time_created_ms_(rtc::TimeMillis()), field_trials_(&kDefaultFieldTrials), rtt_estimate_(DEFAULT_RTT_ESTIMATE_HALF_TIME_MS) { + RTC_DCHECK_RUN_ON(network_thread()); // All of our connections start in WAITING state. // TODO(mallinath) - Start connections from STATE_FROZEN. // Wire up to send stun packets @@ -311,7 +313,13 @@ Connection::Connection(Port* port, RTC_LOG(LS_INFO) << ToString() << ": Connection created"; } -Connection::~Connection() {} +Connection::~Connection() { + RTC_DCHECK_RUN_ON(network_thread()); +} + +webrtc::TaskQueueBase* Connection::network_thread() const { + return network_thread_; +} const Candidate& Connection::local_candidate() const { RTC_DCHECK(local_candidate_index_ < port_->Candidates().size()); @@ -750,6 +758,7 @@ void Connection::Destroy() { // tests, with a workaround in // AutoSocketServerThread::~AutoSocketServerThread. RTC_LOG(LS_VERBOSE) << ToString() << ": Connection destroyed"; + // TODO(bugs.webrtc.org/11988): Use PostTask. port_->thread()->Post(RTC_FROM_HERE, this, MSG_DELETE); LogCandidatePairConfig(webrtc::IceCandidatePairConfigType::kDestroyed); } diff --git a/p2p/base/connection.h b/p2p/base/connection.h index 8155dd4c59..9fb1e79895 100644 --- a/p2p/base/connection.h +++ b/p2p/base/connection.h @@ -88,6 +88,8 @@ class Connection : public CandidatePairInterface, // A unique ID assigned when the connection is created. uint32_t id() const { return id_; } + webrtc::TaskQueueBase* network_thread() const; + // Implementation of virtual methods in CandidatePairInterface. // Returns the description of the local port const Candidate& local_candidate() const override; @@ -369,8 +371,14 @@ class Connection : public CandidatePairInterface, Port* port() { return port_; } const Port* port() const { return port_; } - uint32_t id_; - Port* port_; + // NOTE: A pointer to the network thread is held by `port_` so in theory we + // shouldn't need to hold on to this pointer here, but rather defer to + // port_->thread(). However, some tests delete the classes in the wrong order + // so `port_` may be deleted before an instance of this class is deleted. + // TODO(tommi): This ^^^ should be fixed. + webrtc::TaskQueueBase* const network_thread_; + const uint32_t id_; + Port* const port_; size_t local_candidate_index_; Candidate remote_candidate_;