Skip to content

Commit 31172a7

Browse files
committed
postponedActionsblocker is thread local
1 parent c1bf983 commit 31172a7

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/main/java/org/htmlunit/javascript/JavaScriptEngine.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class JavaScriptEngine implements AbstractJavaScriptEngine<Script> {
121121

122122
private transient ThreadLocal<Boolean> javaScriptRunning_;
123123
private transient ThreadLocal<List<PostponedAction>> postponedActions_;
124-
private transient RootPostponedActionsBlocker postponedActionsBlocker_;
124+
private transient ThreadLocal<RootPostponedActionsBlocker> postponedActionsBlocker_;
125125
private transient boolean shutdownPending_;
126126

127127
/** The JavaScriptExecutor corresponding to all windows of this Web client */
@@ -702,7 +702,9 @@ public void shutdown() {
702702
if (javaScriptRunning_ != null) {
703703
javaScriptRunning_.remove();
704704
}
705-
postponedActionsBlocker_ = null;
705+
if (postponedActionsBlocker_ != null) {
706+
postponedActionsBlocker_.remove();
707+
}
706708
}
707709

708710
/**
@@ -953,6 +955,7 @@ private void doProcessPostponedActions() {
953955
if (webClient == null) {
954956
// shutdown was already called
955957
postponedActions_.set(null);
958+
postponedActionsBlocker_.set(null);
956959
return;
957960
}
958961

@@ -967,6 +970,7 @@ private void doProcessPostponedActions() {
967970
}
968971

969972
final List<PostponedAction> actions = postponedActions_.get();
973+
final PostponedActionsBlocker postponedActionsBlocker = postponedActionsBlocker_.get();
970974
if (actions != null) {
971975
postponedActions_.set(null);
972976
try {
@@ -977,8 +981,8 @@ private void doProcessPostponedActions() {
977981

978982
// verify that the page that registered this PostponedAction is still alive
979983
if (action.isStillAlive()) {
980-
if (postponedActionsBlocker_ == null
981-
|| !postponedActionsBlocker_.blocks(action)) {
984+
if (postponedActionsBlocker == null
985+
|| !postponedActionsBlocker.blocks(action)) {
982986
action.execute();
983987
}
984988
else {
@@ -1087,18 +1091,21 @@ protected void handleJavaScriptTimeoutError(final HtmlPage page, final TimeoutEr
10871091
*/
10881092
@Override
10891093
public PostponedActionsBlocker blockPostponedActions(final Page page) {
1090-
if (postponedActionsBlocker_ == null) {
1091-
postponedActionsBlocker_ = new RootPostponedActionsBlocker(this, page);
1092-
return postponedActionsBlocker_;
1094+
RootPostponedActionsBlocker blocker = postponedActionsBlocker_.get();
1095+
if (blocker == null) {
1096+
blocker = new RootPostponedActionsBlocker(this, page);
1097+
postponedActionsBlocker_.set(blocker);
1098+
return blocker;
10931099
}
10941100

1095-
if (postponedActionsBlocker_.owningPage_ != page) {
1096-
postponedActionsBlocker_.release();
1097-
postponedActionsBlocker_ = new RootPostponedActionsBlocker(this, page);
1098-
return postponedActionsBlocker_;
1101+
if (blocker.owningPage_ != page) {
1102+
blocker.release();
1103+
blocker = new RootPostponedActionsBlocker(this, page);
1104+
postponedActionsBlocker_.set(blocker);
1105+
return blocker;
10991106
}
11001107

1101-
return new ChildPostponedActionsBlocker(postponedActionsBlocker_);
1108+
return new ChildPostponedActionsBlocker(blocker);
11021109
}
11031110

11041111
/**
@@ -1121,7 +1128,7 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot
11211128
private void initTransientFields() {
11221129
javaScriptRunning_ = new ThreadLocal<>();
11231130
postponedActions_ = new ThreadLocal<>();
1124-
postponedActionsBlocker_ = null;
1131+
postponedActionsBlocker_ = new ThreadLocal<>();
11251132
shutdownPending_ = false;
11261133
}
11271134

@@ -1467,7 +1474,7 @@ public boolean blocks(final PostponedAction postponedAction) {
14671474

14681475
@Override
14691476
public void release() {
1470-
jsEngine_.postponedActionsBlocker_ = null;
1477+
jsEngine_.postponedActionsBlocker_.set(null);
14711478
jsEngine_.processPostponedActions();
14721479
}
14731480
}

0 commit comments

Comments
 (0)