Skip to content

Commit

Permalink
ZK-5677: Executions.schedule cause infinite loop if async event cause…
Browse files Browse the repository at this point in the history
…s exception
  • Loading branch information
jumperchen committed Sep 9, 2024
1 parent 769a16b commit 2ff7450
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
11 changes: 7 additions & 4 deletions zk/src/main/java/org/zkoss/zk/ui/impl/DesktopImpl.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* DesktopImpl.java
Purpose:
Description:
History:
Wed Jun 22 09:50:57 2005, Created by tomyeh
Expand Down Expand Up @@ -1840,8 +1840,11 @@ public void onEvent(Event event) throws Exception {
ScheduleInfo<? extends Event> ifPresent = _schedInfos.getIfPresent(
key);
if (ifPresent != null) {
ifPresent.invoke();
_schedInfos.invalidate(key);
try {
ifPresent.invoke();
} finally {
_schedInfos.invalidate(key);
}
} else if (scheduledServerPush()) {
synchronized (_schedInfos) {
// just in case to avoid endless loop
Expand Down
1 change: 1 addition & 0 deletions zkdoc/release-note
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ZK 10.1.0
ZK-5657: Missing zk-bom version since 10.0.0
ZK-5784: a fontawesome bug causes zk to lose all styles in Chrome and Edge
ZK-5659: Tree only renders 50 nodes in client mvvm
ZK-5677: Executions.schedule cause infinite loop if async event causes exception

* Upgrade Notes
+ Remove Htmls.encodeJavaScript(), Strings.encodeJavaScript(), Strings.escape() with Strings.ESCAPE_JAVASCRIPT, and replace them with OWASP Java Encoder APIs instead.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* B101_ZK_5677_Composer.java
Purpose:
Description:
History:
10:48 AM 2024/9/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.test2;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;

/**
* @author jumperchen
*/
public class B101_ZK_5677_Composer extends SelectorComposer<Component> {


@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
Executions.getCurrent().getDesktop().enableServerPush(true);
}

public static int errorCount = 0;
@Listen("onClick=#btn")
public void scheduleEvent() {
Desktop dt = Executions.getCurrent().getDesktop();
Runnable runnable = () -> {
if (errorCount < 3) {
throw new NullPointerException("test: " + ++errorCount);
}
};
EventListener executeEventListener = (evt)->{
Thread.sleep(500); //simulate processing time
runnable.run();
};
try {
Executions.schedule(dt, executeEventListener, new Event("foo"));
} catch (Exception e) {
String msg = "scheduling error";
throw new RuntimeException(msg,e);
}
}

}
21 changes: 21 additions & 0 deletions zktest/src/main/webapp/test2/B101-ZK-5677.zul
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
B101-ZK-5677.zul
Purpose:
Description:
History:
2024/9/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
-->
<zk>
<div apply="org.zkoss.zktest.test2.B101_ZK_5677_Composer">
Click this button, and you will only see a "test 1" message alert
<button id="btn" label="schedule event" />
</div>
</zk>
1 change: 1 addition & 0 deletions zktest/src/main/webapp/test2/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3130,6 +3130,7 @@ B90-ZK-4431.zul=A,E,Multislider
##zats##B101-ZK-5659.zul=A,E,ClientMVVM,Tree,Model,ROD
##zats##B101-ZK-5659-1.zul=A,E,ClientMVVM,Tree,Model,ROD
##zats##B101-ZK-5659-2.zul=A,E,ClientMVVM,Tree,Model,ROD
##zats##B101-ZK-5677.zul=A,E,Scheduler,Event,Exception

##
# Features - 3.0.x version
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* B101_ZK_5677Test.java
Purpose:
Description:
History:
10:54 AM 2024/9/9, Created by jumperchen
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.zats.test2;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Test;

import org.zkoss.zats.ZatsException;
import org.zkoss.zats.mimic.ComponentAgent;
import org.zkoss.zats.mimic.DesktopAgent;
import org.zkoss.zktest.test2.B101_ZK_5677_Composer;
import org.zkoss.zktest.zats.ZATSTestCase;

/**
* @author jumperchen
*/
public class B101_ZK_5677Test extends ZATSTestCase {
@Test
public void test() {
DesktopAgent desktop = connect();
ComponentAgent btn = desktop.query("#btn");
try {
btn.click();
fail("cannot reach here");
} catch (ZatsException ze) {
assertEquals(1, B101_ZK_5677_Composer.errorCount, "error count should be 1");
}
}
}

0 comments on commit 2ff7450

Please sign in to comment.