{"record":{"id":"31b658e2ee9f69e7","repo":"Netflix/Hystrix","slug":"getlogmessageprefix-command-executed-mul","errorCode":null,"errorMessage":"\" + getLogMessagePrefix() + \" command executed multiple times - this is not permitted.","messagePattern":"\" \\+ getLogMessagePrefix\\(\\) \\+ \" command executed multiple times - this is not permitted\\.","errorType":"exception","errorClass":"HystrixRuntimeException","httpStatus":null,"severity":"error","filePath":"hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java","lineNumber":462,"sourceCode":"        final Action0 fireOnCompletedHook = new Action0() {\n            @Override\n            public void call() {\n                try {\n                    executionHook.onSuccess(_cmd);\n                } catch (Throwable hookEx) {\n                    logger.warn(\"Error calling HystrixCommandExecutionHook.onSuccess\", hookEx);\n                }\n            }\n        };\n\n        return Observable.defer(new Func0<Observable<R>>() {\n            @Override\n            public Observable<R> call() {\n                 /* this is a stateful object so can only be used once */\n                if (!commandState.compareAndSet(CommandState.NOT_STARTED, CommandState.OBSERVABLE_CHAIN_CREATED)) {\n                    IllegalStateException ex = new IllegalStateException(\"This instance can only be executed once. Please instantiate a new instance.\");\n                    //TODO make a new error type for this\n                    throw new HystrixRuntimeException(FailureType.BAD_REQUEST_EXCEPTION, _cmd.getClass(), getLogMessagePrefix() + \" command executed multiple times - this is not permitted.\", ex, null);\n                }\n\n                commandStartTimestamp = System.currentTimeMillis();\n\n                if (properties.requestLogEnabled().get()) {\n                    // log this command execution regardless of what happened\n                    if (currentRequestLog != null) {\n                        currentRequestLog.addExecutedCommand(_cmd);\n                    }\n                }\n\n                final boolean requestCacheEnabled = isRequestCachingEnabled();\n                final String cacheKey = getCacheKey();\n\n                /* try from cache first */\n                if (requestCacheEnabled) {\n                    HystrixCommandResponseFromCache<R> fromCache = (HystrixCommandResponseFromCache<R>) requestCache.get(cacheKey);\n                    if (fromCache != null) {","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/Netflix/Hystrix/blob/5ce3bc58c38e7ca60ef2fe0e516e390e294ad941/hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java#L444-L480","documentation":"A HystrixCommand/HystrixObservableCommand instance is a stateful object that may be executed only once; the constructor-to-execution chain guards this with commandState.compareAndSet(NOT_STARTED, OBSERVABLE_CHAIN_CREATED). Calling execute(), queue(), observe(), or toObservable() again on the same instance throws HystrixRuntimeException (typed BAD_REQUEST_EXCEPTION) with 'command executed multiple times - this is not permitted'.","triggerScenarios":"Calling command.execute() a second time; mixing execute() then toObservable() on the same instance; caching command instances (fields, collections, request-scoped beans) and reusing them across requests; subscribing twice to the Observable returned by a single instance.","commonSituations":"Spring singleton beans holding a HystrixCommand field; retry loops that re-run the same command object; accidentally putting commands in a map keyed by cache key; toObservable() invoked in both a doOnSubscribe hook and by the caller.","solutions":["Instantiate a fresh command instance for every execution (move construction inside the method/lambda that runs it)","For retries, create a new command per attempt (e.g. in a Observable.defer or a loop factory)","Never store commands in long-lived fields of singletons; store the parameters instead and build the command on demand","Catch HystrixRuntimeException and check getFailureType()==BAD_REQUEST_EXCEPTION to detect reuse bugs early in tests"],"exampleFix":"// before\nclass Service { private final HystrixCommand<String> cmd = new MyCommand(); \n  String call() { return cmd.execute(); } } // fails on 2nd call\n// after\nclass Service {\n  String call() { return new MyCommand().execute(); } }","handlingStrategy":"validation","validationCode":"// construct a fresh instance per call — nothing to validate at runtime beyond not reusing the instance\nif (commandWasAlreadyUsed) throw new IllegalStateException(\"create a new command instance\");","typeGuard":"null","tryCatchPattern":"catch (HystrixRuntimeException e) { if (e.getFailureType() == FailureType.BAD_REQUEST_EXCEPTION && e.getMessage().contains(\"executed multiple times\")) { // reuse bug — create new instance and retry once with a NEW command } }","preventionTips":["Treat commands as single-use objects: build them inside the method that executes them","For retries, factory-produce a new command per attempt","Never cache command instances in fields, maps, or singletons"],"tags":["java","hystrix","state-machine","reuse","api-misuse"],"backgroundTag":null,"analyzedSha":"5ce3bc58c38e7ca60ef2fe0e516e390e294ad941","analyzedAt":"2026-08-14T10:55:35.600Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}