From c84a8629938a04083f84a96b5fc0ef616609054c Mon Sep 17 00:00:00 2001 From: fox0430 Date: Tue, 21 Apr 2026 14:15:03 +0900 Subject: [PATCH] Drop addr-based send in sendBufMsg --- async_postgres/pg_client.nim | 12 ++++-------- async_postgres/pg_connection.nim | 9 +++++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/async_postgres/pg_client.nim b/async_postgres/pg_client.nim index 887aa0b..c5bd426 100644 --- a/async_postgres/pg_client.nim +++ b/async_postgres/pg_client.nim @@ -2564,8 +2564,7 @@ proc executeImpl( # chronos drains the send Future in the background while we descend into # the receive loop. The outer try/except below owns sendFut's lifetime: # it drains sendFut on the normal path (propagating any stored write - # error) and cancels it on any abnormal exit so the Future never leaks - # and `conn.sendBuf` is never mutated while a write still borrows it. + # error) and cancels it on any abnormal exit so the Future never leaks. var sendFut = conn.sendBufMsg() else: await conn.sendBufMsg() @@ -2692,14 +2691,12 @@ proc executeImpl( await conn.fillRecvBuf(timeout) when hasChronos: - # Normal path: drain sendFut to propagate any stored write error and - # release the borrow on conn.sendBuf. + # Normal path: drain sendFut to propagate any stored write error. await sendFut except CatchableError as e: when hasChronos: # Abnormal path (recv error, cancellation, failed stored write): - # ensure sendFut never escapes as an unhandled Future and that no - # in-flight write still holds addr conn.sendBuf[0]. + # ensure sendFut never escapes as an unhandled Future. if not sendFut.finished: try: await cancelAndWait(sendFut) @@ -2945,8 +2942,7 @@ proc executeIsolatedImpl( await sendFut except CatchableError as e: when hasChronos: - # Abnormal path: cancel or drain sendFut so the Future never leaks - # and no in-flight write still borrows conn.sendBuf. + # Abnormal path: cancel or drain sendFut so the Future never leaks. if not sendFut.finished: try: await cancelAndWait(sendFut) diff --git a/async_postgres/pg_connection.nim b/async_postgres/pg_connection.nim index 0a189e2..f7502cf 100644 --- a/async_postgres/pg_connection.nim +++ b/async_postgres/pg_connection.nim @@ -905,14 +905,15 @@ proc sendMsg*(conn: PgConnection, data: seq[byte]): Future[void] {.async.} = await conn.socket.sendRawBytes(data) proc sendBufMsg*(conn: PgConnection): Future[void] {.async.} = - ## Send conn.sendBuf to the server without copying the seq. - ## Safe because conn.state == csBusy prevents concurrent access to sendBuf. + ## Send conn.sendBuf to the server. + ## The transport receives its own copy of the buffer, so conn.sendBuf is safe + ## to mutate while the returned Future is still pending. when hasChronos: if conn.sendBuf.len > 0: - await conn.writer.write(addr conn.sendBuf[0], conn.sendBuf.len) + await conn.writer.write(conn.sendBuf) elif hasAsyncDispatch: if conn.sendBuf.len > 0: - await conn.socket.sendRawData(addr conn.sendBuf[0], conn.sendBuf.len) + await conn.socket.sendRawBytes(conn.sendBuf) proc closeTransport(conn: PgConnection) {.async.} = ## Close transport resources without sending Terminate.