{"record":{"id":"fc666009dc69f7ab","repo":"apache/seatunnel","slug":"interrupted-while-connecting-to-nats-jetstream","errorCode":null,"errorMessage":"Interrupted while connecting to NATS JetStream","messagePattern":"Interrupted while connecting to NATS JetStream","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"seatunnel-connectors-v2/connector-nats-jetstream/src/main/java/org/apache/seatunnel/connectors/seatunnel/natsjetstream/sink/NatsJetStreamSinkWriter.java","lineNumber":151,"sourceCode":"\n        Optional<String> username = pluginConfig.getOptional(NatsJetStreamSinkOptions.USERNAME);\n        Optional<String> password = pluginConfig.getOptional(NatsJetStreamSinkOptions.PASSWORD);\n        Optional<String> token = pluginConfig.getOptional(NatsJetStreamSinkOptions.TOKEN);\n        boolean hasUsername = username.map(NatsJetStreamSinkWriter::isNotBlank).orElse(false);\n        boolean hasPassword = password.map(NatsJetStreamSinkWriter::isNotBlank).orElse(false);\n        boolean hasToken = token.map(NatsJetStreamSinkWriter::isNotBlank).orElse(false);\n        if (hasUsername && hasPassword) {\n            builder.userInfo(username.get().trim(), password.get());\n        } else if (hasToken) {\n            builder.token(token.get().toCharArray());\n        }\n\n        try {\n            connection = Nats.connect(builder.build());\n            jetStream = connection.jetStream();\n        } catch (InterruptedException e) {\n            Thread.currentThread().interrupt();\n            throw new IOException(\"Interrupted while connecting to NATS JetStream\", e);\n        } catch (IOException | RuntimeException e) {\n            IOException failure =\n                    new IOException(\n                            String.format(\n                                    \"Failed to connect NATS JetStream sink writer for subtask %d\",\n                                    subtaskIndex),\n                            e);\n            closeConnectionQuietly(connection, failure);\n            throw failure;\n        }\n    }\n\n    /**\n     * Best-effort close of a connection that could not be fully initialized. Preserves the current\n     * thread's interrupt status and attaches any close failure as a suppressed exception on the\n     * provided primary failure so the original initialization error remains visible.\n     */\n    private static void closeConnectionQuietly(Connection toClose, IOException primaryFailure) {","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-connectors-v2/connector-nats-jetstream/src/main/java/org/apache/seatunnel/connectors/seatunnel/natsjetstream/sink/NatsJetStreamSinkWriter.java#L133-L169","documentation":"NatsJetStreamSinkWriter.connect() establishes the io.nats client connection and JetStream context. If Nats.connect is interrupted (thread interrupt during connection), the writer rethrows as IOException with this message after restoring the interrupt flag. Other connect failures get a different 'Failed to connect' message, so this error specifically means interruption.","triggerScenarios":"Thread interrupted while blocked inside Nats.connect(builder.build()) — typically task cancellation or failover racing with sink writer initialization.","commonSituations":"Zeta cancels a slow-starting task while NATS server is unreachable and connect retries; job shutdown during initialization; long DNS/network stall making the interrupt land inside connect().","solutions":["Verify whether task cancellation/failover was in progress — this error is usually a symptom of shutdown, not a NATS problem","Check NATS server reachability (servers option, TLS) so connect completes quickly before any shutdown","Retry the job/subtask; interruption during init is generally transient","Reduce connect timeout in the Options builder so cancellation lands outside connect rather than mid-handshake"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// preflight\ntry (Socket s = new Socket()) {\n    s.connect(new InetSocketAddress(natsHost, natsPort), 3000); // reachable\n}","typeGuard":null,"tryCatchPattern":"try {\n    writer = new NatsJetStreamSinkWriter(...);\n} catch (IOException e) {\n    if (e.getCause() instanceof InterruptedException) {\n        Thread.currentThread().interrupt(); // task shutting down\n        return;\n    }\n    throw e;\n}","preventionTips":["Verify NATS server host/port and TLS config before job start","Set a bounded connect timeout in NATS Options","Ensure clean task cancellation so interrupts don't land mid-connect","Retry subtask initialization on transient network failures"],"tags":["nats","jetstream","interrupted","connect"],"backgroundTag":"thread-interrupted","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}