{"record":{"id":"7d4d40853346f4bc","repo":"MyCATApache/Mycat-Server","slug":"writenotsend-but-found-connnection-close-err-thi","errorCode":null,"errorMessage":"writeNotSend but found connnection close err:${this}","messagePattern":"writeNotSend but found connnection close err:(.+?)","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":"writeNotSend throws RuntimeException when the connection is already closed while a buffer is being queued for writing (via checkWriteBuffer or writeToBuffer). The close flag was set (connection reset, error, or cleanup) but data was still handed to the write path, so mycat aborts with this exception and logs 'write err'.","triggerScenarios":"Calling writeToBuffer/checkWriteBuffer (or any response-writing path) on an AbstractConnection whose isClosed() is true — e.g. client disconnected mid-result-set and the server still tries to flush remaining rows.","commonSituations":"Client cancels query/closes connection abruptly; backend node died while large result set streaming; socket timeouts causing early close; application-side read timeout causing driver abort.","solutions":["Treat it as expected churn: catch the exception in the write path and abandon the write when isClosed() is true.","Investigate why the connection closed early (client timeouts, frontend kill, backend failure) via the preceding 'write err' warn log.","Check client/driver socket and query timeouts; increase if large result sets are being truncated.","Fix resource cleanup ordering so writes are not scheduled after close/cleanup."],"exampleFix":"// before\nconnection.writeToBuffer(buffer, true);\n// after\nif (!connection.isClosed()) {\n    connection.writeToBuffer(buffer, true);\n} else {\n    LOGGER.warn(\"skip write, connection closed\");\n}","handlingStrategy":"try-catch","validationCode":"if (conn.isClosed()) { /* skip write */ }","typeGuard":"boolean isWritable(io.mycat.net.AbstractConnection c) { return c != null && !c.isClosed(); }","tryCatchPattern":"try {\n    conn.writeToBuffer(buf, true);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"connnection close\")) {\n        LOGGER.warn(\"connection closed mid-write, discarding buffer\");\n    } else {\n        throw e;\n    }\n}","preventionTips":["Check isClosed() before any write path","Set sane client socket/query timeouts so connections close gracefully","Monitor 'write err' warnings to find clients disconnecting mid-result"],"tags":["connection-closed","nio","network","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"}