{"record":{"id":"b25d8793bfca7c2f","repo":"redis/jedis","slug":"resource-is-returned-to-the-pool-as-broken","errorCode":null,"errorMessage":"Resource is returned to the pool as broken","messagePattern":"Resource is returned to the pool as broken","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/main/java/redis/clients/jedis/JedisPool.java","lineNumber":412,"sourceCode":"    super(poolConfig, factory);\n  }\n\n  @Override\n  public Jedis getResource() {\n    Jedis jedis = super.getResource();\n    jedis.setDataSource(this);\n    return jedis;\n  }\n\n  @Override\n  public void returnResource(final Jedis resource) {\n    if (resource != null) {\n      try {\n        resource.resetState();\n        super.returnResource(resource);\n      } catch (RuntimeException e) {\n        super.returnBrokenResource(resource);\n        log.warn(\"Resource is returned to the pool as broken\", e);\n      }\n    }\n  }\n\n  public void withResource(Consumer<Jedis> consumer) {\n    try (Jedis jedis = this.getResource()) {\n      consumer.accept(jedis);\n    }\n  }\n\n  public <K> K withResourceGet(Function<Jedis, K> function) {\n    try (Jedis jedis = this.getResource()) {\n      return function.apply(jedis);\n    }\n  }\n\n}\n","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/JedisPool.java#L394-L430","documentation":"JedisPool.returnResource first calls resource.resetState() before returning the Jedis to the pool. If resetState (or the return path) throws a RuntimeException — usually because the connection is dead or in a bad protocol state — the object is instead returned to the pool as broken (destroyed) and this warning is logged with the cause. The caller's Jedis instance is effectively discarded; subsequent borrows get other instances.","triggerScenarios":"Calling returnResource(jedis) (or closing a Jedis obtained from getResource()) when the connection's state cannot be reset — socket already closed, unread partial replies after an error, DB/state reset throwing due to a broken transport.","commonSituations":"Using a Jedis after the server closed the connection (timeout); abandoning a command mid-reply (e.g. after a very large response was interrupted); calling close() twice or after a network exception inside try-with-resources.","solutions":["Look at the logged exception: a socket error means the connection is gone — this is normal cleanup, ensure pool validation settings cull such connections proactively.","Always use try-with-resources so returns/broken-returns happen deterministically.","Avoid abandoning commands mid-stream (e.g. not fully reading replies); finish or discard via a fresh connection.","Ensure soTimeout is larger than your slowest command so replies aren't half-consumed.","If caused by resetState failing on DB select/auth state, verify client config matches server ACL/DB settings."],"exampleFix":"// before\nJedis j = pool.getResource();\nj.get(\"k\"); // exception mid-call leaves state broken; manual return\npool.returnResource(j);\n// after\ntry (Jedis j = pool.getResource()) {\n  j.get(\"k\");\n} // pool handles broken return internally","handlingStrategy":"try-catch","validationCode":"if (jedis.isConnected()) {\n  jedis.ping(); // verify state before returning to pool\n}","typeGuard":null,"tryCatchPattern":"try (Jedis j = pool.getResource()) { j.set(\"k\",\"v\"); } // pool routes broken resources to returnBrokenResource internally","preventionTips":["Always use try-with-resources instead of manual returnResource","Fully consume command replies; never abandon a mid-response connection","Set soTimeout above worst-case command time to avoid half-read replies","Don't share a Jedis instance across threads"],"tags":["redis","connection-pool","resource-cleanup","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"}