{"record":{"id":"f9cdf6bfe569d64c","repo":"redis/jedis","slug":"attempting-to-write-to-a-broken-connection","errorCode":null,"errorMessage":"Attempting to write to a broken connection.","messagePattern":"Attempting to write to a broken connection\\.","errorType":"exception","errorClass":"JedisConnectionException","httpStatus":null,"severity":"critical","filePath":"src/main/java/redis/clients/jedis/Connection.java","lineNumber":525,"sourceCode":"    sendCommand(new CommandArguments(cmd));\n  }\n\n  public void sendCommand(final ProtocolCommand cmd, Rawable keyword) {\n    sendCommand(new CommandArguments(cmd).add(keyword));\n  }\n\n  public void sendCommand(final ProtocolCommand cmd, final String... args) {\n    sendCommand(new CommandArguments(cmd).addObjects((Object[]) args));\n  }\n\n  public void sendCommand(final ProtocolCommand cmd, final byte[]... args) {\n    sendCommand(new CommandArguments(cmd).addObjects((Object[]) args));\n  }\n\n  public void sendCommand(final CommandArguments args) {\n    connect();\n    if (broken) {\n      throw new JedisConnectionException(\"Attempting to write to a broken connection.\", brokenCause);\n    }\n\n    try {\n      Protocol.sendCommand(outputStream, args);\n    } catch (JedisConnectionException ex) {\n      throw enrichWithRedisErrorLine(markBroken(ex));\n    } catch (RuntimeException ex) {\n      throw markBroken(ex);\n    } catch (Error err) {\n      throw markBroken(err);\n    }\n  }\n\n  private JedisConnectionException enrichWithRedisErrorLine(JedisConnectionException ex) {\n    /*\n     * When client send request which formed by invalid protocol, Redis send back error message\n     * before close connection. We try to read it to provide reason of failure.\n     */","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/Connection.java#L507-L543","documentation":"Connection.sendCommand() marks a connection broken when a previous I/O attempt failed. If a command is then submitted to that same broken connection, it throws JedisConnectionException with brokenCause attached instead of writing to a dead socket.","triggerScenarios":"Reusing a Connection object after a previous send/flush IOException marked it broken; writing a command after a socket timeout or remote close on the same pooled connection instance.","commonSituations":"Network blips or Redis restarts mid-pipeline; very long-lived connections dropped by LB idle timeout; bug where a broken connection is returned to and reused from a pool.","solutions":["Retry the operation so the pool replaces the broken connection with a fresh one","Catch JedisConnectionException and invalidate/destroy the connection rather than reusing it","Verify pool eviction/test-on-borrow settings so broken connections are not handed out","Check brokenCause (getCause) to diagnose the original network failure"],"exampleFix":"// before\nconnection.sendCommand(cmd); // reuses broken conn\n// after\ntry {\n  connection.sendCommand(cmd);\n} catch (JedisConnectionException e) {\n  pool.returnBrokenResource(resource);\n  resource = pool.getResource();\n  resource.sendCommand(cmd);\n}","handlingStrategy":"try-catch","validationCode":"if (connection.isBroken()) { connection = pool.getResource(); } // if exposed; otherwise rely on pool test-on-borrow","typeGuard":null,"tryCatchPattern":"try { conn.sendCommand(args); } catch (JedisConnectionException e) { pool.returnBrokenResource(resource); resource = pool.getResource(); /* retry once */ }","preventionTips":["Always return broken resources via returnBrokenResource, not returnResource","Enable testWhileIdle/testOnBorrow in the pool","Use TCP keepalive for long idle periods","Retry idempotent commands once on a fresh connection"],"tags":["jedis","connection","network"],"backgroundTag":"broken-pipe","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}