apache/seatunnel · error · IOException

Interrupted while connecting to NATS JetStream

Error message

Interrupted while connecting to NATS JetStream

What it means

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.

Source

Thrown at seatunnel-connectors-v2/connector-nats-jetstream/src/main/java/org/apache/seatunnel/connectors/seatunnel/natsjetstream/sink/NatsJetStreamSinkWriter.java:151

        Optional<String> username = pluginConfig.getOptional(NatsJetStreamSinkOptions.USERNAME);
        Optional<String> password = pluginConfig.getOptional(NatsJetStreamSinkOptions.PASSWORD);
        Optional<String> token = pluginConfig.getOptional(NatsJetStreamSinkOptions.TOKEN);
        boolean hasUsername = username.map(NatsJetStreamSinkWriter::isNotBlank).orElse(false);
        boolean hasPassword = password.map(NatsJetStreamSinkWriter::isNotBlank).orElse(false);
        boolean hasToken = token.map(NatsJetStreamSinkWriter::isNotBlank).orElse(false);
        if (hasUsername && hasPassword) {
            builder.userInfo(username.get().trim(), password.get());
        } else if (hasToken) {
            builder.token(token.get().toCharArray());
        }

        try {
            connection = Nats.connect(builder.build());
            jetStream = connection.jetStream();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException("Interrupted while connecting to NATS JetStream", e);
        } catch (IOException | RuntimeException e) {
            IOException failure =
                    new IOException(
                            String.format(
                                    "Failed to connect NATS JetStream sink writer for subtask %d",
                                    subtaskIndex),
                            e);
            closeConnectionQuietly(connection, failure);
            throw failure;
        }
    }

    /**
     * Best-effort close of a connection that could not be fully initialized. Preserves the current
     * thread's interrupt status and attaches any close failure as a suppressed exception on the
     * provided primary failure so the original initialization error remains visible.
     */
    private static void closeConnectionQuietly(Connection toClose, IOException primaryFailure) {

View on GitHub (pinned to cf67b549a7)

Solutions

  1. Verify whether task cancellation/failover was in progress — this error is usually a symptom of shutdown, not a NATS problem
  2. Check NATS server reachability (servers option, TLS) so connect completes quickly before any shutdown
  3. Retry the job/subtask; interruption during init is generally transient
  4. Reduce connect timeout in the Options builder so cancellation lands outside connect rather than mid-handshake
Defensive patterns

Strategy: retry

Validate before calling

// preflight
try (Socket s = new Socket()) {
    s.connect(new InetSocketAddress(natsHost, natsPort), 3000); // reachable
}

Try / catch

try {
    writer = new NatsJetStreamSinkWriter(...);
} catch (IOException e) {
    if (e.getCause() instanceof InterruptedException) {
        Thread.currentThread().interrupt(); // task shutting down
        return;
    }
    throw e;
}

Prevention

When it happens

Trigger: Thread interrupted while blocked inside Nats.connect(builder.build()) — typically task cancellation or failover racing with sink writer initialization.

Common situations: 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().

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/fc666009dc69f7ab. Report an issue: GitHub.