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

Side by Side Diff: runtime/bin/eventhandler_macos.cc

Issue 2760293002: [dart:io] Adds a finalizer to _NativeSocket to avoid socket leaks (Closed)
Patch Set: Address comments Created 3 years, 8 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
« no previous file with comments | « runtime/bin/eventhandler_linux.cc ('k') | runtime/bin/eventhandler_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #if !defined(DART_IO_DISABLED) 5 #if !defined(DART_IO_DISABLED)
6 6
7 #include "platform/globals.h" 7 #include "platform/globals.h"
8 #if defined(HOST_OS_MACOS) 8 #if defined(HOST_OS_MACOS)
9 9
10 #include "bin/eventhandler.h" 10 #include "bin/eventhandler.h"
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 InterruptMessage msg[MAX_MESSAGES]; 215 InterruptMessage msg[MAX_MESSAGES];
216 ssize_t bytes = TEMP_FAILURE_RETRY( 216 ssize_t bytes = TEMP_FAILURE_RETRY(
217 read(interrupt_fds_[0], msg, MAX_MESSAGES * kInterruptMessageSize)); 217 read(interrupt_fds_[0], msg, MAX_MESSAGES * kInterruptMessageSize));
218 for (ssize_t i = 0; i < bytes / kInterruptMessageSize; i++) { 218 for (ssize_t i = 0; i < bytes / kInterruptMessageSize; i++) {
219 if (msg[i].id == kTimerId) { 219 if (msg[i].id == kTimerId) {
220 timeout_queue_.UpdateTimeout(msg[i].dart_port, msg[i].data); 220 timeout_queue_.UpdateTimeout(msg[i].dart_port, msg[i].data);
221 } else if (msg[i].id == kShutdownId) { 221 } else if (msg[i].id == kShutdownId) {
222 shutdown_ = true; 222 shutdown_ = true;
223 } else { 223 } else {
224 ASSERT((msg[i].data & COMMAND_MASK) != 0); 224 ASSERT((msg[i].data & COMMAND_MASK) != 0);
225 225 Socket* socket = reinterpret_cast<Socket*>(msg[i].id);
226 RefCntReleaseScope<Socket> rs(socket);
227 if (socket->fd() == -1) {
228 continue;
229 }
226 DescriptorInfo* di = 230 DescriptorInfo* di =
227 GetDescriptorInfo(msg[i].id, IS_LISTENING_SOCKET(msg[i].data)); 231 GetDescriptorInfo(socket->fd(), IS_LISTENING_SOCKET(msg[i].data));
228 if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) { 232 if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) {
229 ASSERT(!di->IsListeningSocket()); 233 ASSERT(!di->IsListeningSocket());
230 // Close the socket for reading. 234 // Close the socket for reading.
231 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_RD)); 235 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_RD));
232 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) { 236 } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) {
233 ASSERT(!di->IsListeningSocket()); 237 ASSERT(!di->IsListeningSocket());
234 // Close the socket for writing. 238 // Close the socket for writing.
235 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_WR)); 239 VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_WR));
236 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) { 240 } else if (IS_COMMAND(msg[i].data, kCloseCommand)) {
237 // Close the socket and free system resources and move on to next 241 // Close the socket and free system resources and move on to next
238 // message. 242 // message.
239 intptr_t old_mask = di->Mask(); 243 intptr_t old_mask = di->Mask();
240 Dart_Port port = msg[i].dart_port; 244 Dart_Port port = msg[i].dart_port;
241 di->RemovePort(port); 245 di->RemovePort(port);
242 intptr_t new_mask = di->Mask(); 246 intptr_t new_mask = di->Mask();
243 UpdateKQueueInstance(old_mask, di); 247 UpdateKQueueInstance(old_mask, di);
244 248
245 intptr_t fd = di->fd(); 249 intptr_t fd = di->fd();
246 if (di->IsListeningSocket()) { 250 if (di->IsListeningSocket()) {
247 // We only close the socket file descriptor from the operating 251 // We only close the socket file descriptor from the operating
248 // system if there are no other dart socket objects which 252 // system if there are no other dart socket objects which
249 // are listening on the same (address, port) combination. 253 // are listening on the same (address, port) combination.
250 ListeningSocketRegistry* registry = 254 ListeningSocketRegistry* registry =
251 ListeningSocketRegistry::Instance(); 255 ListeningSocketRegistry::Instance();
252 256
253 MutexLocker locker(registry->mutex()); 257 MutexLocker locker(registry->mutex());
254 258
255 if (registry->CloseSafe(fd)) { 259 if (registry->CloseSafe(socket)) {
256 ASSERT(new_mask == 0); 260 ASSERT(new_mask == 0);
257 socket_map_.Remove(GetHashmapKeyFromFd(fd), 261 socket_map_.Remove(GetHashmapKeyFromFd(fd),
258 GetHashmapHashFromFd(fd)); 262 GetHashmapHashFromFd(fd));
259 di->Close(); 263 di->Close();
260 delete di; 264 delete di;
265 socket->SetClosedFd();
261 } 266 }
262 } else { 267 } else {
263 ASSERT(new_mask == 0); 268 ASSERT(new_mask == 0);
264 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); 269 socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd));
265 di->Close(); 270 di->Close();
266 delete di; 271 delete di;
272 socket->SetClosedFd();
267 } 273 }
268 274
269 DartUtils::PostInt32(port, 1 << kDestroyedEvent); 275 DartUtils::PostInt32(port, 1 << kDestroyedEvent);
270 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { 276 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) {
271 intptr_t old_mask = di->Mask(); 277 intptr_t old_mask = di->Mask();
272 di->ReturnTokens(msg[i].dart_port, TOKEN_COUNT(msg[i].data)); 278 di->ReturnTokens(msg[i].dart_port, TOKEN_COUNT(msg[i].data));
273 UpdateKQueueInstance(old_mask, di); 279 UpdateKQueueInstance(old_mask, di);
274 } else if (IS_COMMAND(msg[i].data, kSetEventMaskCommand)) { 280 } else if (IS_COMMAND(msg[i].data, kSetEventMaskCommand)) {
275 // `events` can only have kInEvent/kOutEvent flags set. 281 // `events` can only have kInEvent/kOutEvent flags set.
276 intptr_t events = msg[i].data & EVENT_MASK; 282 intptr_t events = msg[i].data & EVENT_MASK;
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 if (result == -1) { 468 if (result == -1) {
463 const int kBufferSize = 1024; 469 const int kBufferSize = 1024;
464 char error_message[kBufferSize]; 470 char error_message[kBufferSize];
465 Utils::StrError(errno, error_message, kBufferSize); 471 Utils::StrError(errno, error_message, kBufferSize);
466 FATAL1("kevent failed %s\n", error_message); 472 FATAL1("kevent failed %s\n", error_message);
467 } else { 473 } else {
468 handler_impl->HandleTimeout(); 474 handler_impl->HandleTimeout();
469 handler_impl->HandleEvents(events, result); 475 handler_impl->HandleEvents(events, result);
470 } 476 }
471 } 477 }
478 DEBUG_ASSERT(ReferenceCounted<Socket>::instances() == 0);
472 handler->NotifyShutdownDone(); 479 handler->NotifyShutdownDone();
473 } 480 }
474 481
475 482
476 void EventHandlerImplementation::Start(EventHandler* handler) { 483 void EventHandlerImplementation::Start(EventHandler* handler) {
477 int result = Thread::Start(&EventHandlerImplementation::EventHandlerEntry, 484 int result = Thread::Start(&EventHandlerImplementation::EventHandlerEntry,
478 reinterpret_cast<uword>(handler)); 485 reinterpret_cast<uword>(handler));
479 if (result != 0) { 486 if (result != 0) {
480 FATAL1("Failed to start event handler thread %d", result); 487 FATAL1("Failed to start event handler thread %d", result);
481 } 488 }
(...skipping 22 matching lines...) Expand all
504 // The hashmap does not support keys with value 0. 511 // The hashmap does not support keys with value 0.
505 return dart::Utils::WordHash(fd + 1); 512 return dart::Utils::WordHash(fd + 1);
506 } 513 }
507 514
508 } // namespace bin 515 } // namespace bin
509 } // namespace dart 516 } // namespace dart
510 517
511 #endif // defined(HOST_OS_MACOS) 518 #endif // defined(HOST_OS_MACOS)
512 519
513 #endif // !defined(DART_IO_DISABLED) 520 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/eventhandler_linux.cc ('k') | runtime/bin/eventhandler_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698