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

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

Issue 2760293002: [dart:io] Adds a finalizer to _NativeSocket to avoid socket leaks (Closed)
Patch Set: Address comments Created 3 years, 9 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_fuchsia.cc ('k') | runtime/bin/eventhandler_macos.cc » ('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_LINUX) 8 #if defined(HOST_OS_LINUX)
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 int64_t millis = timeout_queue_.CurrentTimeout(); 215 int64_t millis = timeout_queue_.CurrentTimeout();
216 it.it_value.tv_sec = millis / 1000; 216 it.it_value.tv_sec = millis / 1000;
217 it.it_value.tv_nsec = (millis % 1000) * 1000000; 217 it.it_value.tv_nsec = (millis % 1000) * 1000000;
218 } 218 }
219 VOID_NO_RETRY_EXPECTED( 219 VOID_NO_RETRY_EXPECTED(
220 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL)); 220 timerfd_settime(timer_fd_, TFD_TIMER_ABSTIME, &it, NULL));
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 UpdateEpollInstance(old_mask, di); 247 UpdateEpollInstance(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
269 DartUtils::PostInt32(port, 1 << kDestroyedEvent); 274 DartUtils::PostInt32(port, 1 << kDestroyedEvent);
270 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { 275 } else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) {
271 int count = TOKEN_COUNT(msg[i].data); 276 int count = TOKEN_COUNT(msg[i].data);
272 intptr_t old_mask = di->Mask(); 277 intptr_t old_mask = di->Mask();
273 di->ReturnTokens(msg[i].dart_port, count); 278 di->ReturnTokens(msg[i].dart_port, count);
274 UpdateEpollInstance(old_mask, di); 279 UpdateEpollInstance(old_mask, di);
275 } else if (IS_COMMAND(msg[i].data, kSetEventMaskCommand)) { 280 } else if (IS_COMMAND(msg[i].data, kSetEventMaskCommand)) {
276 // `events` can only have kInEvent/kOutEvent flags set. 281 // `events` can only have kInEvent/kOutEvent flags set.
277 intptr_t events = msg[i].data & EVENT_MASK; 282 intptr_t events = msg[i].data & EVENT_MASK;
278 ASSERT(0 == (events & ~(1 << kInEvent | 1 << kOutEvent))); 283 ASSERT(0 == (events & ~(1 << kInEvent | 1 << kOutEvent)));
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 epoll_wait(handler_impl->epoll_fd_, events, kMaxEvents, -1)); 400 epoll_wait(handler_impl->epoll_fd_, events, kMaxEvents, -1));
396 ASSERT(EAGAIN == EWOULDBLOCK); 401 ASSERT(EAGAIN == EWOULDBLOCK);
397 if (result <= 0) { 402 if (result <= 0) {
398 if (errno != EWOULDBLOCK) { 403 if (errno != EWOULDBLOCK) {
399 perror("Poll failed"); 404 perror("Poll failed");
400 } 405 }
401 } else { 406 } else {
402 handler_impl->HandleEvents(events, result); 407 handler_impl->HandleEvents(events, result);
403 } 408 }
404 } 409 }
410 DEBUG_ASSERT(ReferenceCounted<Socket>::instances() == 0);
405 handler->NotifyShutdownDone(); 411 handler->NotifyShutdownDone();
406 } 412 }
407 413
408 414
409 void EventHandlerImplementation::Start(EventHandler* handler) { 415 void EventHandlerImplementation::Start(EventHandler* handler) {
410 int result = Thread::Start(&EventHandlerImplementation::Poll, 416 int result = Thread::Start(&EventHandlerImplementation::Poll,
411 reinterpret_cast<uword>(handler)); 417 reinterpret_cast<uword>(handler));
412 if (result != 0) { 418 if (result != 0) {
413 FATAL1("Failed to start event handler thread %d", result); 419 FATAL1("Failed to start event handler thread %d", result);
414 } 420 }
(...skipping 22 matching lines...) Expand all
437 // The hashmap does not support keys with value 0. 443 // The hashmap does not support keys with value 0.
438 return dart::Utils::WordHash(fd + 1); 444 return dart::Utils::WordHash(fd + 1);
439 } 445 }
440 446
441 } // namespace bin 447 } // namespace bin
442 } // namespace dart 448 } // namespace dart
443 449
444 #endif // defined(HOST_OS_LINUX) 450 #endif // defined(HOST_OS_LINUX)
445 451
446 #endif // !defined(DART_IO_DISABLED) 452 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/eventhandler_fuchsia.cc ('k') | runtime/bin/eventhandler_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698