{"record":{"id":"9ddc65ae2b7f54b6","repo":"apache/flink","slug":"failed-to-close-current-reader","errorCode":null,"errorMessage":"Failed to close current reader","messagePattern":"Failed to close current reader","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/hybrid/HybridSourceReader.java","lineNumber":212,"sourceCode":"    @Override\n    public void close() throws Exception {\n        if (currentReader != null) {\n            currentReader.close();\n        }\n        LOG.debug(\n                \"Reader closed: subtask={} sourceIndex={} currentReader={}\",\n                readerContext.getIndexOfSubtask(),\n                currentSourceIndex,\n                currentReader);\n    }\n\n    private void setCurrentReader(int index) {\n        Preconditions.checkArgument(index != currentSourceIndex);\n        if (currentReader != null) {\n            try {\n                currentReader.close();\n            } catch (Exception e) {\n                throw new RuntimeException(\"Failed to close current reader\", e);\n            }\n            LOG.debug(\n                    \"Reader closed: subtask={} sourceIndex={} currentReader={}\",\n                    readerContext.getIndexOfSubtask(),\n                    currentSourceIndex,\n                    currentReader);\n        }\n        // TODO: track previous readers splits till checkpoint\n        Source source = switchedSources.sourceOf(index);\n        SourceReader<T, ?> reader;\n        try {\n            reader = source.createReader(readerContext);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Failed to create reader\", e);\n        }\n        // currentReader must be switched before `addSplits` is called.\n        currentSourceIndex = index;\n        currentReader = reader;","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/hybrid/HybridSourceReader.java#L194-L230","documentation":"Thrown as a RuntimeException by HybridSourceReader.setCurrentReader when closing the current underlying SourceReader (before switching to the next source in the hybrid pipeline) throws an exception. setCurrentReader calls currentReader.close() when transitioning from one source to the next; any failure during the underlying reader's cleanup is wrapped and rethrown to prevent continuing with a half-closed reader.","triggerScenarios":"HybridSource transitions from source N to source N+1, and the SourceReader.close() of source N throws — e.g., the reader's network connection cleanup fails, or its background threads do not shut down cleanly.","commonSituations":"Underlying source reader has a resource leak or cleanup bug; network/filesystem connection is lost during source transition; the reader's close() is not idempotent and throws on double-close; a custom source implementation has improper exception handling in close().","solutions":["Check the wrapped exception cause to identify which underlying source reader's close() failed.","If the underlying source has a known close() bug, update to a patched version of that connector.","For custom source implementations, ensure SourceReader.close() catches and logs internal errors rather than propagating them.","If the error is transient (network hiccup during transition), the job will failover and retry from the last checkpoint."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// This error occurs internally in HybridSourceReader; handle at job level\ntry {\n    env.execute(\"hybridSourceJob\");\n} catch (Exception e) {\n    Throwable cause = ExceptionUtils.findThrowable(e, RuntimeException.class).orElse(e);\n    if (cause.getMessage() != null && cause.getMessage().equals(\"Failed to close current reader\")) {\n        // underlying source reader close failed — job will failover and retry\n        log.error(\"Source transition failed during reader close: {}\", cause.getCause());\n    }\n}","preventionTips":["Ensure underlying source readers implement close() robustly (catch internal errors).","Update source connector libraries to patched versions.","Test HybridSource transitions in integration tests."],"tags":["hybrid-source","source-reader","resource-cleanup","source-transition"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}