{"record":{"id":"cc736835d2795f87","repo":"apache/pulsar","slug":"not-support-to-set-removeoncancelpolicy-it-is-alw","errorCode":null,"errorMessage":"Not support to set removeOnCancelPolicy, it is always false","messagePattern":"Not support to set removeOnCancelPolicy, it is always false","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/SingleThreadNonConcurrentFixedRateScheduler.java","lineNumber":235,"sourceCode":"                + \" always false\");\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() {\n        return false;\n    }\n\n    /**\n     * {@inheritDoc}\n     * Since the method \"remove(runnable)\" of DelayedWorkQueue is expensive if the tasks in the queue is not the same\n     * type, we denied the remove policy.\n     */\n    @Override\n    public void setRemoveOnCancelPolicy(boolean value) {\n        throw new IllegalArgumentException(\"Not support to set removeOnCancelPolicy, it is always false\");\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public boolean getRemoveOnCancelPolicy() {\n        return false;\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    @Override\n    public void setRejectedExecutionHandler(RejectedExecutionHandler handler) {\n        super.setRejectedExecutionHandler(handler);\n        this.rejectedExecutionHandler = handler;\n    }","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/SingleThreadNonConcurrentFixedRateScheduler.java#L217-L253","documentation":"SingleThreadNonConcurrentFixedRateScheduler does not support the removeOnCancelPolicy. Because DelayedWorkQueue.remove(runnable) is expensive when queued tasks are not of the same type, cancellation never removes tasks from the queue (policy is always false), and setRemoveOnCancelPolicy unconditionally throws IllegalArgumentException.","triggerScenarios":"Calling setRemoveOnCancelPolicy(true or false) on a SingleThreadNonConcurrentFixedRateScheduler, typically from generic executor-tuning code that calls the setter on any ScheduledExecutorService to avoid cancelled tasks piling up in the queue.","commonSituations":"Performance-tuning snippets written for ScheduledThreadPoolExecutor (where setRemoveOnCancelPolicy(true) is a common recommendation) applied to this Pulsar scheduler; long-lived schedulers with many cancellations where a developer tries to enable queue pruning; migration from java.util.concurrent schedulers.","solutions":["Remove the call to setRemoveOnCancelPolicy; cancelled tasks remain until they fire but the scheduler denies removal by design.","If cancelled-task accumulation is a concern, minimize scheduling of tasks that will be cancelled immediately, or use ScheduledThreadPoolExecutor with setRemoveOnCancelPolicy(true).","Wrap policy-setting calls in a check for ScheduledThreadPoolExecutor before invoking the setter.","Catch IllegalArgumentException in generic configuration code that must handle mixed executor types."],"exampleFix":"// before\nscheduler.setRemoveOnCancelPolicy(true);\n// after\nif (scheduler instanceof java.util.concurrent.ScheduledThreadPoolExecutor) {\n    ((java.util.concurrent.ScheduledThreadPoolExecutor) scheduler).setRemoveOnCancelPolicy(true);\n}","handlingStrategy":"try-catch","validationCode":"static boolean supportsRemoveOnCancel(java.util.concurrent.ScheduledExecutorService s) {\n    return s instanceof java.util.concurrent.ScheduledThreadPoolExecutor;\n}","typeGuard":"static boolean isPolicyConfigurable(java.util.concurrent.ScheduledExecutorService s) {\n    return s instanceof java.util.concurrent.ScheduledThreadPoolExecutor;\n}","tryCatchPattern":"try {\n    scheduler.setRemoveOnCancelPolicy(true);\n} catch (IllegalArgumentException e) {\n    LOG.info(\"Scheduler {} does not support removeOnCancelPolicy (always false): {}\", scheduler, e.getMessage());\n}","preventionTips":["Do not enable removeOnCancelPolicy on this scheduler — removal from DelayedWorkQueue is intentionally denied for performance","Avoid scheduling tasks that are cancelled immediately; prefer not scheduling them at all","Use ScheduledThreadPoolExecutor if queue pruning of cancelled tasks is essential","Guard shared executor-tuning code paths with instanceof checks before calling policy setters"],"tags":["pulsar","scheduler","unsupported-operation","cancellation"],"backgroundTag":"unsupported-scheduler-policy","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}