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

Unified Diff: runtime/bin/socket.h

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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/reference_counting.h ('k') | runtime/bin/socket.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket.h
diff --git a/runtime/bin/socket.h b/runtime/bin/socket.h
index ebd40674c341cb179bafc843da48e84878717dc9..7b8a03df6f01026ecc83f5c179d4a53b40ded8f3 100644
--- a/runtime/bin/socket.h
+++ b/runtime/bin/socket.h
@@ -27,6 +27,7 @@
#include "bin/builtin.h"
#include "bin/dartutils.h"
+#include "bin/reference_counting.h"
#include "bin/thread.h"
#include "bin/utils.h"
#include "platform/hashmap.h"
@@ -247,7 +248,11 @@ class AddressList {
};
-class Socket {
+// We write Sockets into the native field of the _NativeSocket object
+// on the Dart side. They are allocated in SetSocketIdNativeField(), and are
+// deallocated either from the finalizer attached to _NativeSockets there, or
+// from the eventhandler, whichever drops the last reference.
+class Socket : public ReferenceCounted<Socket> {
public:
enum SocketRequest {
kLookupRequest = 0,
@@ -255,6 +260,15 @@ class Socket {
kReverseLookupRequest = 2,
};
+ explicit Socket(intptr_t fd);
+
+ intptr_t fd() const { return fd_; }
+ void SetClosedFd();
+
+ Dart_Port port() const { return port_; }
+ void set_port(Dart_Port port) { port_ = port; }
+
+ // TODO(dart:io): Convert these to instance methods where possible.
static bool Initialize();
static intptr_t Available(intptr_t fd);
static intptr_t Read(intptr_t fd, void* buffer, intptr_t num_bytes);
@@ -333,12 +347,24 @@ class Socket {
static Dart_Port GetServicePort();
- static void SetSocketIdNativeField(Dart_Handle socket, intptr_t id);
- static intptr_t GetSocketIdNativeField(Dart_Handle socket);
+ static void SetSocketIdNativeField(Dart_Handle handle,
+ intptr_t id,
+ bool listening);
+ static void ReuseSocketIdNativeField(Dart_Handle handle,
+ Socket* socket,
+ bool listening);
+ static Socket* GetSocketIdNativeField(Dart_Handle socket);
private:
- DISALLOW_ALLOCATION();
- DISALLOW_IMPLICIT_CONSTRUCTORS(Socket);
+ ~Socket() { ASSERT(fd_ == kClosedFd); }
+
+ static const int kClosedFd = -1;
+
+ intptr_t fd_;
+ Dart_Port port_;
+
+ friend class ReferenceCounted<Socket>;
+ DISALLOW_COPY_AND_ASSIGN(Socket);
};
@@ -405,7 +431,7 @@ class ListeningSocketRegistry {
//
// The caller is responsible for obtaining the mutex first, before calling
// this function.
- bool CloseSafe(intptr_t socketfd);
+ bool CloseSafe(Socket* socketfd);
Mutex* mutex() { return mutex_; }
@@ -416,7 +442,7 @@ class ListeningSocketRegistry {
bool v6_only;
bool shared;
int ref_count;
- intptr_t socketfd;
+ Socket* socketfd;
// Singly linked lists of OSSocket instances which listen on the same port
// but on different addresses.
@@ -426,7 +452,7 @@ class ListeningSocketRegistry {
int port,
bool v6_only,
bool shared,
- intptr_t socketfd)
+ Socket* socketfd)
: address(address),
port(port),
v6_only(v6_only),
@@ -438,7 +464,7 @@ class ListeningSocketRegistry {
static const intptr_t kInitialSocketsCount = 8;
- OSSocket* findOSSocketWithAddress(OSSocket* current, const RawAddr& addr) {
+ OSSocket* FindOSSocketWithAddress(OSSocket* current, const RawAddr& addr) {
while (current != NULL) {
if (SocketAddress::AreAddressesEqual(current->address, addr)) {
return current;
@@ -465,9 +491,9 @@ class ListeningSocketRegistry {
void InsertByPort(intptr_t port, OSSocket* socket);
void RemoveByPort(intptr_t port);
- OSSocket* LookupByFd(intptr_t fd);
- void InsertByFd(intptr_t fd, OSSocket* socket);
- void RemoveByFd(intptr_t fd);
+ OSSocket* LookupByFd(Socket* fd);
+ void InsertByFd(Socket* fd, OSSocket* socket);
+ void RemoveByFd(Socket* fd);
bool CloseOneSafe(OSSocket* os_socket, bool update_hash_maps);
void CloseAllSafe();
« no previous file with comments | « runtime/bin/reference_counting.h ('k') | runtime/bin/socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698