{"record":{"id":"63445a39e0ec564d","repo":"elastic/elasticsearch","slug":"thread-waiting-for-the-response-was-interrupted","errorCode":null,"errorMessage":"thread waiting for the response was interrupted","messagePattern":"thread waiting for the response was interrupted","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"client/rest/src/main/java/org/elasticsearch/client/RestClient.java","lineNumber":904,"sourceCode":"        ResponseOrResponseException(Response response) {\n            this.response = Objects.requireNonNull(response);\n            this.responseException = null;\n        }\n\n        ResponseOrResponseException(ResponseException responseException) {\n            this.responseException = Objects.requireNonNull(responseException);\n            this.response = null;\n        }\n    }\n\n    /**\n     * Wrap the exception so the caller's signature shows up in the stack trace, taking care to copy the original type and message\n     * where possible so async and sync code don't have to check different exceptions.\n     */\n    private static Exception extractAndWrapCause(Exception exception) {\n        if (exception instanceof InterruptedException) {\n            Thread.currentThread().interrupt();\n            throw new RuntimeException(\"thread waiting for the response was interrupted\", exception);\n        }\n        if (exception instanceof ExecutionException) {\n            ExecutionException executionException = (ExecutionException) exception;\n            Throwable t = executionException.getCause() == null ? executionException : executionException.getCause();\n            if (t instanceof Error) {\n                throw (Error) t;\n            }\n            exception = (Exception) t;\n        }\n        if (exception instanceof ConnectTimeoutException) {\n            ConnectTimeoutException e = new ConnectTimeoutException(exception.getMessage());\n            e.initCause(exception);\n            return e;\n        }\n        if (exception instanceof SocketTimeoutException) {\n            SocketTimeoutException e = new SocketTimeoutException(exception.getMessage());\n            e.initCause(exception);\n            return e;","sourceCodeStart":886,"sourceCodeEnd":922,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/client/rest/src/main/java/org/elasticsearch/client/RestClient.java#L886-L922","documentation":"Thrown by extractAndWrapCause when the future completing a sync performRequest was cancelled/interrupted. The client re-asserts the interrupt flag (Thread.currentThread().interrupt()) and wraps the original InterruptedException in a RuntimeException so it can surface through methods that only declare checked exceptions. This is a programmer/environment signal, not a server error.","triggerScenarios":"The thread blocked in performRequest is interrupted (Thread.interrupt) while waiting on the async future; the RestClient is shut down while a request is in flight; the surrounding executor cancels the task.","commonSituations":"Calling RestClient.close() from another thread while a request is pending; shutting down an ExecutorService that wraps the client call; test timeouts interrupting the request thread.","solutions":["Ensure no other thread interrupts or shuts down the client/executor while a request is in flight.","If you intentionally cancel, treat this RuntimeException as expected and re-check Thread.interrupted() in your handler.","Avoid sharing one RestClient across threads that may close it independently."],"exampleFix":"// before\nnew Thread(() -> client.performRequest(req)).start();\n// later, from another thread:\nclient.close(); // interrupts the in-flight request\n// after\n// drain/await outstanding requests, then close:\nclient.close(); // only after no caller is mid-request","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { client.performRequest(req); }\ncatch (RuntimeException e) {\n    if (e.getCause() instanceof InterruptedException) {\n        Thread.currentThread().interrupt();\n        // handle shutdown/cancellation gracefully\n    } else throw e;\n}","preventionTips":["Do not close the RestClient from a different thread while requests are pending.","Own request lifecycle: await completion before teardown.","If you must interrupt, propagate the interrupt and treat this exception as expected."],"tags":["rest-client","threading","interruption","lifecycle"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}