1
0
mirror of synced 2026-08-05 15:06:54 +00:00

fix: limit number of tracked unused Deferred objects to 10 (#189)

This commit is contained in:
Yury Semikhatsky
2021-01-05 15:05:12 -08:00
committed by GitHub
parent b2c27143a8
commit 6a67124e1c
3 changed files with 19 additions and 17 deletions
@@ -104,7 +104,7 @@ class ChannelOwner {
@Override
protected void finalize() {
if (constructionStackTrace != null) {
connection.unusedDeferredObjects.add(constructionStackTrace);
connection.addUnusedDeferredObject(constructionStackTrace);
}
}
};
@@ -20,9 +20,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.PlaywrightException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.time.Duration;
import java.util.*;
@@ -56,7 +54,7 @@ public class Connection {
private final Root root;
private int lastId = 0;
private final Map<Integer, WaitableResult<JsonElement>> callbacks = new HashMap<>();
final Deque<Exception> unusedDeferredObjects = new ArrayDeque<>();
private final Deque<Exception> unusedDeferredObjects = new ArrayDeque<>();
class Root extends ChannelOwner {
Root(Connection connection) {
@@ -267,4 +265,19 @@ public class Connection {
unusedDeferredObjects.removeLast();
}
}
void checkNoUnusedDeferredObjects() {
if (unusedDeferredObjects.isEmpty()) {
return;
}
List<String> chunks = new ArrayList<>();
chunks.add("Method get() has not been called on some Deferred<> objects. This indicates a " +
"bug in the client code. Here are some stack traces where such objects were constructed");
for (Exception e : unusedDeferredObjects) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
chunks.add(writer.toString());
}
throw new PlaywrightException(String.join("\n", chunks));
}
}
@@ -108,17 +108,6 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
System.err.println("WARNING: Timed out while waiting for driver process to exit");
}
if (connection.unusedDeferredObjects.isEmpty()) {
return;
}
List<String> chunks = new ArrayList<>();
chunks.add("Method get() has not been called on some Deferred<> objects. This indicates a " +
"bug in the client code. Here are some stack traces where such objects were constructed");
for (Exception e : connection.unusedDeferredObjects) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
chunks.add(writer.toString());
}
throw new PlaywrightException(String.join("\n", chunks));
connection.checkNoUnusedDeferredObjects();
}
}