{"record":{"id":"5e3c546749f2376c","repo":"MyCATApache/Mycat-Server","slug":"writenotsend-but-found-connnection-close-err-t","errorCode":null,"errorMessage":"writeNotSend but found connnection close err:\" + this","messagePattern":"writeNotSend but found connnection close err:\" \\+ this","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/net/AbstractConnection.java","lineNumber":451,"sourceCode":"\t\tByteBuffer buffer = allocate();\n\t\tbuffer = writeToBuffer(data, buffer);\n\t\twrite(buffer);\n\n\t}\n\n\tprivate final void writeNotSend(ByteBuffer buffer) {\n\t\tif (isSupportCompress()) {\n\t\t\tByteBuffer newBuffer = CompressUtil.compressMysqlPacket(buffer, this, compressUnfinishedDataQueue);\n\t\t\twriteQueue.offer(newBuffer);\n\t\t\t\n\t\t} else {\n\t\t\twriteQueue.offer(buffer);\n\t\t}\n\t\t\n\t\tif(isClosed()) {\n\t\t\tLOGGER.warn(\"write err:{}\", this);\n\t\t\tthis.close(\"found this connection has close , try to reClean the connection\");\n\t\t\tthrow new RuntimeException(\"writeNotSend but found connnection close err:\" + this);\n\t\t}\n\t}\n\n\n    @Override\n\tpublic final void write(ByteBuffer buffer) {\n    \t\n\t\tif (isSupportCompress()) {\n\t\t\tByteBuffer newBuffer = CompressUtil.compressMysqlPacket(buffer, this, compressUnfinishedDataQueue);\n\t\t\twriteQueue.offer(newBuffer);\n\t\t} else {\n\t\t\twriteQueue.offer(buffer);\n\t\t}\n\n\t\t// if ansyn write finishe event got lock before me ,then writing\n\t\t// flag is set false but not start a write request\n\t\t// so we check again\n\t\ttry {","sourceCodeStart":433,"sourceCodeEnd":469,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/net/AbstractConnection.java#L433-L469","documentation":"AbstractConnection.writeNotSend enqueues a buffer for async write but first checks isClosed(). If the connection is already closed, it logs a warning, cleans the connection, and throws RuntimeException('writeNotSend but found connnection close err:' + this). It guards against writing on a dead socket (e.g. after a disconnect) which would otherwise silently drop or corrupt data.","triggerScenarios":"Calling writeToBuffer or checkWriteBuffer on a connection that a peer already closed or that was closed by a timeout/kill, then attempting to flush queued bytes.","commonSituations":"Backend MySQL connections killed by wait_timeout while pooled, client disconnected mid-query, or frontend/frontend session closed by an earlier error while a response is still being written.","solutions":["Check connection.isClosed() before writing, and drop/refresh the session instead of writing.","Re-obtain or reconnect the backend connection and re-execute the operation if the exchange is still needed.","Tune idle timeouts (wait_timeout, MyCat heartbeat/idleTimeout) so stale connections are evicted before reuse.","Ensure earlier error paths close the session so no further writes are attempted after close."],"exampleFix":"// before\ncon.writeToBuffer(buffer);\n// after\nif (!con.isClosed()) {\n    con.writeToBuffer(buffer);\n} else {\n    LOGGER.warn(\"skip write on closed connection\");\n}","handlingStrategy":"type-guard","validationCode":"if (connection == null || connection.isClosed()) {\n  logger.warn(\"skip write, connection closed\");\n  return;\n}","typeGuard":"// Java: guard helper\nstatic boolean writable(AbstractConnection c) {\n  return c != null && !c.isClosed();\n}","tryCatchPattern":"try {\n  conn.writeToBuffer(buf);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"writeNotSend but found connnection close\")) {\n    reconnectOrAbort();\n  } else throw e;\n}","preventionTips":["Check isClosed() before every write on pooled/backend connections.","Use heartbeats to evict connections killed by wait_timeout.","Close sessions deterministically on error so no late writes occur."],"tags":["connection","closed-connection","nio","write"],"backgroundTag":"broken-pipe","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}