MyCATApache/Mycat-Server · error · RuntimeException

writeNotSend but found connnection close err

Error message

writeNotSend but found connnection close err:${this}

What it means

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'.

Solutions

  1. Treat it as expected churn: catch the exception in the write path and abandon the write when isClosed() is true.
  2. Investigate why the connection closed early (client timeouts, frontend kill, backend failure) via the preceding 'write err' warn log.
  3. Check client/driver socket and query timeouts; increase if large result sets are being truncated.
  4. Fix resource cleanup ordering so writes are not scheduled after close/cleanup.

Example fix

// before
connection.writeToBuffer(buffer, true);
// after
if (!connection.isClosed()) {
    connection.writeToBuffer(buffer, true);
} else {
    LOGGER.warn("skip write, connection closed");
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (conn.isClosed()) { /* skip write */ }

Type guard

boolean isWritable(io.mycat.net.AbstractConnection c) { return c != null && !c.isClosed(); }

Try / catch

try {
    conn.writeToBuffer(buf, true);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().contains("connnection close")) {
        LOGGER.warn("connection closed mid-write, discarding buffer");
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/7d4d40853346f4bc. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/net/AbstractConnection.java:451

		ByteBuffer buffer = allocate();
		buffer = writeToBuffer(data, buffer);
		write(buffer);

	}

	private final void writeNotSend(ByteBuffer buffer) {
		if (isSupportCompress()) {
			ByteBuffer newBuffer = CompressUtil.compressMysqlPacket(buffer, this, compressUnfinishedDataQueue);
			writeQueue.offer(newBuffer);
			
		} else {
			writeQueue.offer(buffer);
		}
		
		if(isClosed()) {
			LOGGER.warn("write err:{}", this);
			this.close("found this connection has close , try to reClean the connection");
			throw new RuntimeException("writeNotSend but found connnection close err:" + this);
		}
	}


    @Override
	public final void write(ByteBuffer buffer) {
    	
		if (isSupportCompress()) {
			ByteBuffer newBuffer = CompressUtil.compressMysqlPacket(buffer, this, compressUnfinishedDataQueue);
			writeQueue.offer(newBuffer);
		} else {
			writeQueue.offer(buffer);
		}

		// if ansyn write finishe event got lock before me ,then writing
		// flag is set false but not start a write request
		// so we check again
		try {

View on GitHub (pinned to 65f8d8beb7)