Failed to execute rule ... java.util.concurrent.RejectExecutionException

Hi, did some work to prepare home heating based on PID and PWM automation module. Putting it all together works, but there are some problems with rules switching the thermostat modes on/off etc.
using openhab-js to create rules.

1/ when this rules fires with case ON


rules.JSRule({
  name: `Termostat wyłącz wszystkie `,
  id: `Termostats_all`,
  tags: [`_DYNAMIC_`],
  overwrite: `true`,
  description: `Group termostat`,
  triggers: [triggers.ItemStateChangeTrigger(`Radiators_AwayMode`)],
  execute: (event) => {
    switch (event.receivedCommand.toString()) {
      case "ON": {
          console.log("Wlaczono tryb wakacji");
          for (let i = 1; i <= RoomNumber ; i++) {
            items.getItem(`RadiatorAC${i}_homekit_TargetHeatingCoolingMode`).sendCommand("off");
          }
            break;
          }      
      case "OFF": {
        console.log("Wylaczono tryb wakacji")
          for (let i = 1; i <= RoomNumber; i++) {
            items.getItem(`RadiatorAC${i}_homekit_TargetHeatingCoolingMode`).sendCommand("auto"); 
            }
            break;  
          }

    }
   
  }
});

2/ “Termostat_1_modes_management” (i variable comes from a loop, not important) - item receives “off” from rule 1


rules.JSRule({
    name: `Termostat ${i} modes management`,
    id: `Termostat_${i}_modes_management`,
    tags: [`Room${i}`, `_DYNAMIC_`],
    overwrite: `true`,
    description: `Seeting and syncing modes between iOS APP and Google Assistant`,
    triggers: [triggers.ItemCommandTrigger(`RadiatorAC${i}_homekit_TargetHeatingCoolingMode`)],
    execute: data => {
    
      switch (data.receivedCommand.toString()) {
          case "auto": {
          rules.setEnabled(`Termostat_${i}_modes_management_from_schedule`,true);
          items.getItem(`RadiatorAC${i}_SystemMode`).sendCommand("auto")
          items.getItem(`RadiatorAC${i}_Preset`).sendCommand("schedule")
          items.getItem(`RadiatorAC${i}_TargetTemperature`).sendCommand(items.getItem(`RadiatorAC${i}_scheduled_TargetTemperature`).state)
          rules.setEnabled(`Termostat_${i}_PWM_rule`,true);
          rules.setEnabled(`Termostat_${i}_PID_controller`,true);
          break;
        }
        case "heat": {
          console.log("otrzymał heat")
          items.getItem(`RadiatorAC${i}_SystemMode`).sendCommand("auto")
          items.getItem(`RadiatorAC${i}_Preset`).sendCommand("manual")
          rules.setEnabled(`Termostat_${i}_modes_management_from_schedule`,false);
          rules.setEnabled(`Termostat_${i}_PWM_rule`,true);
          rules.setEnabled(`Termostat_${i}_PID_controller`,true);
          break;
        }
        case "off": {
          items.getItem(`RadiatorAC${i}_SystemMode`).sendCommand("off")
          rules.setEnabled(`Termostat_${i}_PWM_rule`,false);
          rules.setEnabled(`Termostat_${i}_modes_management_from_schedule`,false);
          rules.setEnabled(`Termostat_${i}_PID_controller`,false);
          items.getItem(`RadiatorAC${i}_PWM_Relay`).sendCommand("OFF")
          break;
         
        }
      }
    }
    });

i get


2022-06-15 22:43:45.714 [INFO ] [tomation.script.file.rulesCreator.js] - Wlaczono tryb wakacji
2022-06-15 22:43:45.779 [ERROR] [tomation.script.file.rulesCreator.js] - Failed to execute rule Termostat_1_modes_management: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@764b8cb[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@5666048d[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@3a2ebf6d]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@894258f[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7752]
2022-06-15 22:43:45.796 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Termostat_1_modes_management': Fail to execute action: 1
2022-06-15 22:43:45.809 [ERROR] [tomation.script.file.rulesCreator.js] - Failed to execute rule Termostat_3_modes_management: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@4fc1bc98[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@efcb6a3[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@ac333a4]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@1566b04c[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7761]
2022-06-15 22:43:45.811 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Termostat_3_modes_management': Fail to execute action: 1
2022-06-15 22:43:45.816 [ERROR] [tomation.script.file.rulesCreator.js] - Failed to execute rule Termostat_4_modes_management: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@6597400d[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@36365227[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@2ab3db61]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@365bc336[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7344]
2022-06-15 22:43:45.820 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Termostat_4_modes_management': Fail to execute action: 1
2022-06-15 22:43:45.823 [ERROR] [tomation.script.file.rulesCreator.js] - Failed to execute rule Termostat_6_modes_management: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@320b0d31[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@5ecce1b[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@7bfec0ed]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@639f17ae[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7177]
2022-06-15 22:43:45.827 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Termostat_6_modes_management': Fail to execute action: 1
2022-06-15 22:43:45.838 [ERROR] [tomation.script.file.rulesCreator.js] - Failed to execute rule Termostat_7_modes_management: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@2e89ef67[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@6fb4e55c[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@6b8b7c72]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@57bb36b3[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7567]
2022-06-15 22:43:45.840 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Termostat_7_modes_management': Fail to execute action: 1
2022-06-15 22:43:45.845 [ERROR] [tomation.script.file.rulesCreator.js] - Failed to execute rule Termostat_5_modes_management: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@46f2613c[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@7cf6b07[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@3f0ef26]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@b157780[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7568]
2022-06-15 22:43:45.847 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Termostat_5_modes_management': Fail to execute action: 1
2022-06-15 22:43:45.851 [ERROR] [tomation.script.file.rulesCreator.js] - Failed to execute rule Termostat_2_modes_management: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@1e5a0b14[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@66319e18[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@7aee13ae]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@2b402746[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7751]
2022-06-15 22:43:45.855 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Termostat_2_modes_management': Fail to execute action: 1

Any ideas what to do ? i think the problem is appearing when the rules are enabled/disabled:
rules.setEnabled(Termostat_${i}_PWM_rule,false);
rules.setEnabled(Termostat_${i}_modes_management_from_schedule,false);
rules.setEnabled(Termostat_${i}_PID_controller,false);
PWM and PID are automation modules addons, modes management from schedule is simple cronbased rule (every 5 minutes check the desired temperature from schedule)

Did some further testing and this error is caused when i want to disable Puls width modulation rule.

rules.setEnabled(Termostat_${i}_PWM_rule,false);

@fwolter would you be so kind and look at this issue?

Tried to disable te rule also from GUI, and if i do it for the first time i get:

==> /var/log/openhab/openhab.log <==
2022-06-22 11:22:27.342 [ERROR] [internal.JSONResponseExceptionMapper] - Unexpected exception occurred while processing REST request.
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@365787[Not completed, task = java.util.concurrent.Executors$RunnableAdapter@289638[Wrapped task = org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData@22cdc9]] rejected from java.util.concurrent.ScheduledThreadPoolExecutor@176da02[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 1]
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2055) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:825) ~[?:?]
        at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:340) ~[?:?]
        at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:562) ~[?:?]
        at java.util.concurrent.ScheduledThreadPoolExecutor.submit(ScheduledThreadPoolExecutor.java:715) ~[?:?]
        at java.util.concurrent.Executors$DelegatedExecutorService.submit(Executors.java:714) ~[?:?]
        at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl.triggered(TriggerHandlerCallbackImpl.java:59) ~[?:?]
        at org.openhab.automation.pwm.internal.handler.PWMTriggerHandler.setOutput(PWMTriggerHandler.java:189) ~[?:?]
        at org.openhab.automation.pwm.internal.handler.state.StateMachine.controlOutput(StateMachine.java:70) ~[?:?]
        at org.openhab.automation.pwm.internal.handler.state.State.controlOutput(State.java:83) ~[?:?]
        at org.openhab.automation.pwm.internal.handler.state.AlwaysOffState.<init>(AlwaysOffState.java:27) ~[?:?]
        at org.openhab.automation.pwm.internal.handler.state.State.nextState(State.java:60) ~[?:?]
        at org.openhab.automation.pwm.internal.handler.state.StateMachine.stop(StateMachine.java:78) ~[?:?]
        at org.openhab.automation.pwm.internal.handler.PWMTriggerHandler.dispose(PWMTriggerHandler.java:235) ~[?:?]
        at org.openhab.core.automation.handler.BaseModuleHandlerFactory.ungetHandler(BaseModuleHandlerFactory.java:84) ~[?:?]
        at org.openhab.core.automation.internal.RuleEngineImpl.removeModuleHandlers(RuleEngineImpl.java:616) ~[?:?]
        at org.openhab.core.automation.internal.RuleEngineImpl.unregister(RuleEngineImpl.java:687) ~[?:?]
        at org.openhab.core.automation.internal.RuleEngineImpl.unregister(RuleEngineImpl.java:667) ~[?:?]
        at org.openhab.core.automation.internal.RuleEngineImpl.setEnabled(RuleEngineImpl.java:807) ~[?:?]
        at org.openhab.core.automation.rest.internal.RuleResource.enableRule(RuleResource.java:307) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
        at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179) ~[bundleFile:3.4.5]
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) ~[bundleFile:3.4.5]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201) ~[bundleFile:3.4.5]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104) ~[bundleFile:3.4.5]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) ~[bundleFile:3.4.5]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) ~[bundleFile:3.4.5]
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:265) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:225) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:298) ~[bundleFile:3.4.5]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:217) ~[bundleFile:3.4.5]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[bundleFile:3.1.0]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:273) ~[bundleFile:3.4.5]
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:550) ~[bundleFile:9.4.46.v20220331]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:74) ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:600) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1440) ~[bundleFile:9.4.46.v20220331]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:294) ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:501) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1355) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[bundleFile:9.4.46.v20220331]
        at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:90) ~[bundleFile:?]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.Server.handle(Server.java:516) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487) ~[bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:409) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883) [bundleFile:9.4.46.v20220331]
        at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034) [bundleFile:9.4.46.v20220331]
        at java.lang.Thread.run(Thread.java:834) [?:?]

The PWM module tries to switch the controlling output off, when the module is disabled. But that doesn’t seem to work as the scheduler is terminated beforehand.

I removed disabling of the output upon disabling the module. Try this: https://github.com/fwolter/openhab-addons/releases/download/fix-pwm-disable/org.openhab.automation.pwm-3.4.0-SNAPSHOT.jar