{"record":{"id":"d3ee20bd239a21eb","repo":"apache/cassandra","slug":"maximum-number-of-workers-must-not-be-negative","errorCode":null,"errorMessage":"Maximum number of workers must not be negative","messagePattern":"Maximum number of workers must not be negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/concurrent/SEPExecutor.java","lineNumber":375,"sourceCode":"    public void setCorePoolSize(int newCorePoolSize)\n    {\n        throw new IllegalArgumentException(\"Cannot resize core pool size of SEPExecutor\");\n    }\n\n    @Override\n    public int getMaximumPoolSize()\n    {\n        return maximumPoolSize.get();\n    }\n\n    @Override\n    public synchronized void setMaximumPoolSize(int newMaximumPoolSize)\n    {\n        final int oldMaximumPoolSize = maximumPoolSize.get();\n\n        if (newMaximumPoolSize < 0)\n        {\n            throw new IllegalArgumentException(\"Maximum number of workers must not be negative\");\n        }\n\n        int deltaWorkPermits = newMaximumPoolSize - oldMaximumPoolSize;\n        if (!maximumPoolSize.compareAndSet(oldMaximumPoolSize, newMaximumPoolSize))\n        {\n            throw new IllegalStateException(\"Maximum pool size has been changed while resizing\");\n        }\n\n        if (deltaWorkPermits == 0)\n            return;\n\n        permits.updateAndGet(cur -> updateWorkPermits(cur, workPermits(cur) + deltaWorkPermits));\n        logger.info(\"Resized {} maximum pool size from {} to {}\", name, oldMaximumPoolSize, newMaximumPoolSize);\n\n        // If we we have more work permits than before we should spin up a worker now rather than waiting\n        // until either a new task is enqueued (if all workers are descheduled) or a spinning worker calls\n        // maybeSchedule().\n        pool.maybeStartSpinningWorker();","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/concurrent/SEPExecutor.java#L357-L393","documentation":"setMaximumPoolSize validates that the requested maximum worker count is non-negative; a negative value would corrupt the shared permit accounting of SharedExecutorPool, so an IllegalArgumentException is thrown before any state changes. It is a simple argument-validation failure.","triggerScenarios":"Calling setMaximumPoolSize with a negative int, e.g. from a computed value like maxSize - delta where delta exceeds maxSize, config parsing that yields a negative number, or integer overflow producing a negative result.","commonSituations":"Config mistakes where a resize value is subtracted without clamping; user-provided pool settings parsed into int with a sign error; arithmetic overflow when scaling pool sizes programmatically.","solutions":["Clamp/validate the new size before calling: Math.max(0, newSize)","Fix the configuration value or formula that produced the negative number","Check for integer overflow in size calculations; use Math.subtractExact or long arithmetic then clamp","Ensure config parsing rejects negative pool sizes at load time"],"exampleFix":"// before\nexecutor.setMaximumPoolSize(currentSize - reduction); // may go negative\n// after\nint newSize = Math.max(0, currentSize - reduction);\nexecutor.setMaximumPoolSize(newSize);","handlingStrategy":"validation","validationCode":"if (newSize < 0)\n    throw new IllegalArgumentException(\"newMaximumPoolSize must be >= 0, got \" + newSize);","typeGuard":"static boolean isValidPoolSize(int size)\n{\n    return size >= 0;\n}","tryCatchPattern":null,"preventionTips":["Clamp computed sizes with Math.max(0, x) before calling","Use Math.subtractExact/long math to catch overflow in resize arithmetic","Validate user/config-provided pool sizes at load time","Review subtraction-based resize formulas for sign errors"],"tags":["illegal-argument","executor","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}