redis/jedis · error · JedisConnectionException
It seems like server has closed the connection.
Error message
It seems like server has closed the connection.
What it means
RedisInputStream.readLine builds a reply line byte by byte; if the very first byte available is an immediate end-of-stream producing an empty line, the server has closed the connection without sending any reply. Jedis throws JedisConnectionException because no response exists to parse.
Solutions
- Discard and reopen the connection; retry the command on a fresh connection (idempotent commands only).
- Enable Jedis pool testOnBorrow/testWhileIdle and set pool eviction settings so dead connections are pruned.
- Match client socket timeout below the server's `timeout` setting and any LB idle timeout; send periodic PINGs or enable TCP keepalive.
- Check Redis server logs (and `CLIENT LIST`) for kills or restarts around the failure time.
Example fix
// before
JedisPool pool = new JedisPool(config, host);
// idle connections die silently
// after
JedisPool pool = new JedisPoolBuilder()
.connectionConfig(JedisClientConfig.builder()
.timeoutMillis(30000)
.blockingSocketTimeoutMillis(0).build())
.poolConfig(new GenericObjectPoolConfig.Builder()
.testWhileIdle(true)
.timeBetweenEvictionRunsMillis(30000).build())
.hostAndPort(host, port).build(); Defensive patterns
Strategy: retry
Validate before calling
// before executing: ensure pool validates connections
if (!jedis.isConnected()) { jedis = pool.getResource(); } Try / catch
try {
return jedis.get(key);
} catch (JedisConnectionException e) {
// server closed connection; retry once on a fresh resource
try (Jedis fresh = pool.getResource()) {
return fresh.get(key);
}
} Prevention
- Enable testWhileIdle/testOnBorrow in the pool config.
- Set client socket timeout below server `timeout` and LB idle timeout.
- Send keepalive PINGs for long idle periods; enable TCP keepalive.
- Retry only idempotent commands.
When it happens
Trigger: Thrown from the public readLine (used by message, readErrorLineIfPossible, readDoubleCrLf, readBigIntegerCrLf) when the accumulated line is empty because the underlying stream hit EOF immediately (ensureFill saw limit == -1).
Common situations: Redis server restarted or crashed between request and reply; server-side client timeout (timeout config) killing idle connections; a load balancer idling out the TCP connection; network partition dropping the socket.
Related errors
- Unexpected end of stream.
- Failed to create socket.
- It seems like server has closed the connection.
- All configured databases are unhealthy. Cannot initialize…
- Attempting to write to a broken connection.
AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08).
Data as JSON: /api/errors/21085ee413fe0779.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/redis/clients/jedis/util/RedisInputStream.java:96
byte b = buf[count++];
if (b == '\r') {
ensureFill(); // Must be one more byte
byte c = buf[count++];
if (c == '\n') {
break;
}
sb.append((char) b);
sb.append((char) c);
} else {
sb.append((char) b);
}
}
final String reply = sb.toString();
if (reply.isEmpty()) {
throw new JedisConnectionException("It seems like server has closed the connection.");
}
return reply;
}
public byte[] readLineBytes() {
/*
* This operation should only require one fill. In that typical case we optimize allocation and
* copy of the byte array. In the edge case where more than one fill is required then we take a
* slower path and expand a byte array output stream as is necessary.
*/
ensureFill();
int pos = count;
final byte[] buf = this.buf;
while (true) {View on GitHub (pinned to 6dac31d4c2)