MyCATApache/Mycat-Server · error · RuntimeException

writeNotSend but found connnection close err:" + this

Error message

writeNotSend but found connnection close err:" + this

What it means

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.

Solutions

  1. Check connection.isClosed() before writing, and drop/refresh the session instead of writing.
  2. Re-obtain or reconnect the backend connection and re-execute the operation if the exchange is still needed.
  3. Tune idle timeouts (wait_timeout, MyCat heartbeat/idleTimeout) so stale connections are evicted before reuse.
  4. Ensure earlier error paths close the session so no further writes are attempted after close.

Example fix

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

Strategy: type-guard

Validate before calling

if (connection == null || connection.isClosed()) {
  logger.warn("skip write, connection closed");
  return;
}

Type guard

// Java: guard helper
static boolean writable(AbstractConnection c) {
  return c != null && !c.isClosed();
}

Try / catch

try {
  conn.writeToBuffer(buf);
} catch (RuntimeException e) {
  if (e.getMessage() != null && e.getMessage().startsWith("writeNotSend but found connnection close")) {
    reconnectOrAbort();
  } else throw e;
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/5e3c546749f2376c. 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)