Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 985777b

Browse files
committed
Fix memory leak.
The URL.openConnection() returns an instance of DelegateHttpsURLConnection type, which overrides the equals() method in a very bad way (so bad that even "connection.equals(connection)" returns false!). Therefore, we were unable to remove the connection objects from the activeConnections list. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=153201249
1 parent 6647b41 commit 985777b

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

  • src/agent/internals/src/main/java/com/google/devtools/cdbg/debuglets/java

src/agent/internals/src/main/java/com/google/devtools/cdbg/debuglets/java/GcpHubClient.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,35 +168,35 @@ public byte[][] serializeBreakpoints() throws IOException {
168168
return serializedBreakpoints;
169169
}
170170
}
171-
171+
172172
/**
173-
* Helper class to automatically insert and remove connection from
173+
* Helper class to automatically insert and remove connection from
174174
* {@link GcpHubClient#activeConnections}.
175175
*/
176176
private final class ActiveConnection implements AutoCloseable {
177177
private final HttpURLConnection connection;
178-
178+
179179
public ActiveConnection(HttpURLConnection connection) {
180180
this.connection = connection;
181-
181+
182182
synchronized (activeConnections) {
183183
if (isShutdown) {
184184
// We don't need to call "disconnect" here. "URL.openConnection" doesn't actually
185185
// establish the connection, so calling "disconnect" will have no effect.
186186
throw new RuntimeException("Shutdown in progress");
187187
}
188-
189-
activeConnections.add(this.connection);
188+
189+
activeConnections.add(this);
190190
}
191191
}
192192

193193
@Override
194194
public void close() {
195195
synchronized (activeConnections) {
196-
activeConnections.remove(connection);
196+
activeConnections.remove(this);
197197
}
198198
}
199-
199+
200200
public HttpURLConnection get() {
201201
return connection;
202202
}
@@ -283,12 +283,12 @@ public JsonElement serialize(JsonElement json, Type t, JsonSerializationContext
283283
* Shutdown flag blocking all outgoing HTTP calls to metadata service.
284284
*/
285285
private boolean isShutdown = false;
286-
286+
287287
/**
288288
* List of active HTTP connections to be closed during shutdown.
289289
*/
290-
private final List<HttpURLConnection> activeConnections = new ArrayList<>();
291-
290+
private final List<ActiveConnection> activeConnections = new ArrayList<>();
291+
292292
/**
293293
* Cache of unique identifier of the application resources. Computing uniquifier is
294294
* an expensive operation and we don't want to repeat it on every
@@ -512,14 +512,14 @@ public void approveBreakpointCanary(String breakpointId) throws Exception {
512512
public void shutdown() {
513513
metadata.shutdown();
514514

515-
List<HttpURLConnection> connections = new ArrayList<>();
515+
List<ActiveConnection> connections = new ArrayList<>();
516516
synchronized (activeConnections) {
517517
isShutdown = true;
518518
connections.addAll(activeConnections);
519519
}
520520

521-
for (HttpURLConnection connection : connections) {
522-
connection.disconnect();
521+
for (ActiveConnection connection : connections) {
522+
connection.get().disconnect();
523523
}
524524
}
525525

0 commit comments

Comments
 (0)