Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: net/quic/chromium/quic_chromium_client_session.cc

Issue 2319343004: Makes migration on write error asynchronous to avoid reentrancy issues (Closed)
Patch Set: Avoids multiple tasks from repeating packet write. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/quic/chromium/quic_chromium_client_session.h" 5 #include "net/quic/chromium/quic_chromium_client_session.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 logger_(new QuicConnectionLogger(this, 230 logger_(new QuicConnectionLogger(this,
231 connection_description, 231 connection_description,
232 std::move(socket_performance_watcher), 232 std::move(socket_performance_watcher),
233 net_log_)), 233 net_log_)),
234 going_away_(false), 234 going_away_(false),
235 port_migration_detected_(false), 235 port_migration_detected_(false),
236 disabled_reason_(QUIC_DISABLED_NOT), 236 disabled_reason_(QUIC_DISABLED_NOT),
237 token_binding_signatures_(kTokenBindingSignatureMapSize), 237 token_binding_signatures_(kTokenBindingSignatureMapSize),
238 streams_pushed_count_(0), 238 streams_pushed_count_(0),
239 streams_pushed_and_claimed_count_(0), 239 streams_pushed_and_claimed_count_(0),
240 error_code_from_rewrite_(OK), 240 migration_pending_(false),
241 use_error_code_from_rewrite_(false), 241 write_pending_(false),
242 weak_factory_(this) { 242 weak_factory_(this) {
243 sockets_.push_back(std::move(socket)); 243 sockets_.push_back(std::move(socket));
244 packet_readers_.push_back(base::WrapUnique(new QuicChromiumPacketReader( 244 packet_readers_.push_back(base::WrapUnique(new QuicChromiumPacketReader(
245 sockets_.back().get(), clock, this, yield_after_packets, 245 sockets_.back().get(), clock, this, yield_after_packets,
246 yield_after_duration, net_log_))); 246 yield_after_duration, net_log_)));
247 crypto_stream_.reset( 247 crypto_stream_.reset(
248 crypto_client_stream_factory->CreateQuicCryptoClientStream( 248 crypto_client_stream_factory->CreateQuicCryptoClientStream(
249 server_id, this, base::WrapUnique(new ProofVerifyContextChromium( 249 server_id, this, base::WrapUnique(new ProofVerifyContextChromium(
250 cert_verify_flags, net_log_)), 250 cert_verify_flags, net_log_)),
251 crypto_config)); 251 crypto_config));
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 void QuicChromiumClientSession::OnSuccessfulVersionNegotiation( 945 void QuicChromiumClientSession::OnSuccessfulVersionNegotiation(
946 const QuicVersion& version) { 946 const QuicVersion& version) {
947 logger_->OnSuccessfulVersionNegotiation(version); 947 logger_->OnSuccessfulVersionNegotiation(version);
948 QuicSpdySession::OnSuccessfulVersionNegotiation(version); 948 QuicSpdySession::OnSuccessfulVersionNegotiation(version);
949 } 949 }
950 950
951 int QuicChromiumClientSession::HandleWriteError( 951 int QuicChromiumClientSession::HandleWriteError(
952 int error_code, 952 int error_code,
953 scoped_refptr<StringIOBuffer> packet) { 953 scoped_refptr<StringIOBuffer> packet) {
954 DCHECK(packet != nullptr); 954 DCHECK(packet != nullptr);
955 use_error_code_from_rewrite_ = false; 955 DCHECK_NE(ERR_IO_PENDING, error_code);
956 if (stream_factory_) { 956 DCHECK_GT(0, error_code);
957 stream_factory_->MaybeMigrateSingleSession(this, WRITE_ERROR, packet); 957 DCHECK(!migration_pending_);
958 DCHECK(packet_ == nullptr);
959
960 // Post a task to migrate the session onto a new network.
961 task_runner_->PostTask(
962 FROM_HERE,
963 base::Bind(&QuicChromiumClientSession::MigrateSessionOnWriteError,
964 weak_factory_.GetWeakPtr()));
965
966 // Store packet in the session since the actual migration and packet rewrite
967 // can happen via this posted task or via an async network notification.
968 packet_ = packet;
969 migration_pending_ = true;
970
971 // Cause the packet writer to return ERR_IO_PENDING and block so
972 // that the actual migration happens from the message loop instead
973 // of under the call stack of QuicConnection::WritePacket.
974 return ERR_IO_PENDING;
975 }
976
977 void QuicChromiumClientSession::MigrateSessionOnWriteError() {
978 // If migration_pending_ is false, an earlier task completed migration.
979 if (!migration_pending_)
980 return;
981
982 if (stream_factory_ != nullptr &&
983 stream_factory_->MaybeMigrateSingleSession(this, WRITE_ERROR) ==
984 MigrationResult::SUCCESS)
985 return;
986
987 // Close the connection if migration failed. Do not cause a
988 // connection close packet to be sent since socket may be borked.
989 connection()->CloseConnection(QUIC_PACKET_WRITE_ERROR,
990 "Write and subsequent migration failed",
991 ConnectionCloseBehavior::SILENT_CLOSE);
992 }
993
994 void QuicChromiumClientSession::WriteToNewSocket() {
995 // If write_pending_ is false, an earlier task wrote to the new socket.
996 if (!write_pending_)
997 return;
998 // Prevent any pending migration or write tasks from executing.
999 write_pending_ = false;
1000 migration_pending_ = false;
1001
1002 DCHECK(!connection()->writer()->IsWriteBlocked());
1003
1004 if (packet_ == nullptr) {
1005 connection()->SendPing();
Ryan Hamilton 2016/09/11 19:24:12 Maybe I'm not looking in the right place, but I do
Jana 2016/09/11 19:44:04 If there was a write block just before this WriteT
1006 return;
958 } 1007 }
959 return use_error_code_from_rewrite_ ? error_code_from_rewrite_ : error_code; 1008
1009 // Set packet_ to null first. We cannot set packet_ to null after
1010 // the following write since the write may result in packet_ being
1011 // reused via a write error.
1012 scoped_refptr<StringIOBuffer> packet = packet_;
1013 packet_ = nullptr;
1014
1015 // The connection is waiting for the original write to complete
1016 // asynchronously. The new writer will notify the connection if the
1017 // write below completes asynchronously, but a synchronous competion
1018 // must be propagated back to the connection here.
1019 WriteResult result =
1020 static_cast<QuicChromiumPacketWriter*>(connection()->writer())
1021 ->WritePacketToSocket(packet);
1022
1023 if (result.error_code == ERR_IO_PENDING)
1024 return;
1025 // All write errors should be mapped into ERR_IO_PENDING by
1026 // HandleWriteError.
1027 DCHECK(result.error_code >= 0);
1028 connection()->OnCanWrite();
960 } 1029 }
961 1030
962 void QuicChromiumClientSession::OnWriteError(int error_code) { 1031 void QuicChromiumClientSession::OnWriteError(int error_code) {
963 DCHECK_NE(ERR_IO_PENDING, error_code); 1032 DCHECK_NE(ERR_IO_PENDING, error_code);
964 DCHECK_GT(0, error_code); 1033 DCHECK_GT(0, error_code);
965 connection()->OnWriteError(error_code); 1034 connection()->OnWriteError(error_code);
966 } 1035 }
967 1036
968 void QuicChromiumClientSession::OnWriteUnblocked() { 1037 void QuicChromiumClientSession::OnWriteUnblocked() {
969 connection()->OnCanWrite(); 1038 connection()->OnCanWrite();
970 } 1039 }
971 1040
1041 bool QuicChromiumClientSession::ShouldWriteBlock() {
1042 return write_pending_;
1043 }
1044
972 void QuicChromiumClientSession::OnPathDegrading() { 1045 void QuicChromiumClientSession::OnPathDegrading() {
973 if (stream_factory_) { 1046 if (stream_factory_) {
974 stream_factory_->MaybeMigrateSingleSession(this, EARLY_MIGRATION, nullptr); 1047 stream_factory_->MaybeMigrateSingleSession(this, EARLY_MIGRATION);
975 } 1048 }
976 } 1049 }
977 1050
978 bool QuicChromiumClientSession::HasOpenDynamicStreams() const { 1051 bool QuicChromiumClientSession::HasOpenDynamicStreams() const {
979 return QuicSession::HasOpenDynamicStreams() || 1052 return QuicSession::HasOpenDynamicStreams() ||
980 GetNumDrainingOutgoingStreams() > 0; 1053 GetNumDrainingOutgoingStreams() > 0;
981 } 1054 }
982 1055
983 void QuicChromiumClientSession::OnProofValid( 1056 void QuicChromiumClientSession::OnProofValid(
984 const QuicCryptoClientConfig::CachedState& cached) { 1057 const QuicCryptoClientConfig::CachedState& cached) {
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
1171 going_away_ = true; 1244 going_away_ = true;
1172 DCHECK_EQ(0u, GetNumActiveStreams()); 1245 DCHECK_EQ(0u, GetNumActiveStreams());
1173 // Will delete |this|. 1246 // Will delete |this|.
1174 if (stream_factory_) 1247 if (stream_factory_)
1175 stream_factory_->OnSessionClosed(this); 1248 stream_factory_->OnSessionClosed(this);
1176 } 1249 }
1177 1250
1178 bool QuicChromiumClientSession::MigrateToSocket( 1251 bool QuicChromiumClientSession::MigrateToSocket(
1179 std::unique_ptr<DatagramClientSocket> socket, 1252 std::unique_ptr<DatagramClientSocket> socket,
1180 std::unique_ptr<QuicChromiumPacketReader> reader, 1253 std::unique_ptr<QuicChromiumPacketReader> reader,
1181 std::unique_ptr<QuicChromiumPacketWriter> writer, 1254 std::unique_ptr<QuicChromiumPacketWriter> writer) {
1182 scoped_refptr<StringIOBuffer> packet) {
1183 DCHECK_EQ(sockets_.size(), packet_readers_.size()); 1255 DCHECK_EQ(sockets_.size(), packet_readers_.size());
1184 if (sockets_.size() >= kMaxReadersPerQuicSession) { 1256 if (sockets_.size() >= kMaxReadersPerQuicSession)
1185 return false; 1257 return false;
1186 } 1258
1187 // TODO(jri): Make SetQuicPacketWriter take a scoped_ptr. 1259 // TODO(jri): Make SetQuicPacketWriter take a scoped_ptr.
1188 packet_readers_.push_back(std::move(reader)); 1260 packet_readers_.push_back(std::move(reader));
1189 sockets_.push_back(std::move(socket)); 1261 sockets_.push_back(std::move(socket));
1190 StartReading(); 1262 StartReading();
1191 QuicChromiumPacketWriter* raw_writer = writer.get();
1192 connection()->SetQuicPacketWriter(writer.release(), /*owns_writer=*/true); 1263 connection()->SetQuicPacketWriter(writer.release(), /*owns_writer=*/true);
1193 if (packet == nullptr) { 1264
1194 connection()->SendPing(); 1265 // Post task to write the pending packet or a PING packet to the new
1195 return true; 1266 // socket. Also block the writer to prevent is being used until
1196 } 1267 // WriteToNewSocket completes.
1197 // Packet rewrite after migration on socket write error. 1268 task_runner_->PostTask(
1198 error_code_from_rewrite_ = raw_writer->WritePacketToSocket(packet.get()); 1269 FROM_HERE, base::Bind(&QuicChromiumClientSession::WriteToNewSocket,
1199 use_error_code_from_rewrite_ = true; 1270 weak_factory_.GetWeakPtr()));
1271 // Migration completed and write task posted.
1272 migration_pending_ = false;
1273 write_pending_ = true;
1200 return true; 1274 return true;
1201 } 1275 }
1202 1276
1203 void QuicChromiumClientSession::PopulateNetErrorDetails( 1277 void QuicChromiumClientSession::PopulateNetErrorDetails(
1204 NetErrorDetails* details) { 1278 NetErrorDetails* details) {
1205 details->quic_port_migration_detected = port_migration_detected_; 1279 details->quic_port_migration_detected = port_migration_detected_;
1206 } 1280 }
1207 1281
1208 const DatagramClientSocket* QuicChromiumClientSession::GetDefaultSocket() 1282 const DatagramClientSocket* QuicChromiumClientSession::GetDefaultSocket()
1209 const { 1283 const {
(...skipping 27 matching lines...) Expand all
1237 } 1311 }
1238 1312
1239 void QuicChromiumClientSession::DeletePromised( 1313 void QuicChromiumClientSession::DeletePromised(
1240 QuicClientPromisedInfo* promised) { 1314 QuicClientPromisedInfo* promised) {
1241 if (IsOpenStream(promised->id())) 1315 if (IsOpenStream(promised->id()))
1242 streams_pushed_and_claimed_count_++; 1316 streams_pushed_and_claimed_count_++;
1243 QuicClientSessionBase::DeletePromised(promised); 1317 QuicClientSessionBase::DeletePromised(promised);
1244 } 1318 }
1245 1319
1246 } // namespace net 1320 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/chromium/quic_chromium_client_session.h ('k') | net/quic/chromium/quic_chromium_client_session_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698