diff --git a/tokio-postgres/src/bind.rs b/tokio-postgres/src/bind.rs index 06cd14d2..ac1a4f90 100644 --- a/tokio-postgres/src/bind.rs +++ b/tokio-postgres/src/bind.rs @@ -10,6 +10,34 @@ use std::sync::atomic::{AtomicUsize, Ordering}; static NEXT_ID: AtomicUsize = AtomicUsize::new(0); +/// Closes the named portal on the server if the `bind` future is dropped before +/// the `Portal` handle (whose own `Drop` performs the close) has been constructed. +struct CloseOnDrop<'a> { + client: &'a Arc, + name: Option, +} + +impl CloseOnDrop<'_> { + fn disarm(mut self) -> String { + self.name.take().expect("guard already disarmed") + } +} + +impl Drop for CloseOnDrop<'_> { + fn drop(&mut self) { + if let Some(name) = self.name.take() { + let buf = self.client.with_buf(|buf| { + frontend::close(b'P', &name, buf).unwrap(); + frontend::sync(buf); + buf.split().freeze() + }); + let _ = self + .client + .send(RequestMessages::Single(FrontendMessage::Raw(buf))); + } + } +} + pub async fn bind( client: &Arc, statement: Statement, @@ -28,11 +56,15 @@ where })?; let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?; + let guard = CloseOnDrop { + client, + name: Some(name), + }; match responses.next().await? { Message::BindComplete => {} _ => return Err(Error::unexpected_message()), } - Ok(Portal::new(client, name, statement)) + Ok(Portal::new(client, guard.disarm(), statement)) } diff --git a/tokio-postgres/src/client.rs b/tokio-postgres/src/client.rs index 6d3b03d3..e4136665 100644 --- a/tokio-postgres/src/client.rs +++ b/tokio-postgres/src/client.rs @@ -786,6 +786,17 @@ impl Client { .send(RequestMessages::Single(FrontendMessage::Raw(buf))); } + #[doc(hidden)] + pub fn __private_api_release_savepoint(&self, name: &str) { + let buf = self.inner().with_buf(|buf| { + frontend::query(&format!("RELEASE {name}"), buf).unwrap(); + buf.split().freeze() + }); + let _ = self + .inner() + .send(RequestMessages::Single(FrontendMessage::Raw(buf))); + } + #[doc(hidden)] pub fn __private_api_close(&mut self) { self.inner.sender.close_channel() diff --git a/tokio-postgres/src/prepare.rs b/tokio-postgres/src/prepare.rs index 8484fa33..ad673a4c 100644 --- a/tokio-postgres/src/prepare.rs +++ b/tokio-postgres/src/prepare.rs @@ -58,6 +58,35 @@ ORDER BY attnum static NEXT_ID: AtomicUsize = AtomicUsize::new(0); +/// Closes the named statement on the server if the `prepare` future is dropped +/// before the `Statement` handle (whose own `Drop` performs the close) has been +/// constructed. +struct CloseOnDrop<'a> { + client: &'a Arc, + name: Option, +} + +impl CloseOnDrop<'_> { + fn disarm(mut self) -> String { + self.name.take().expect("guard already disarmed") + } +} + +impl Drop for CloseOnDrop<'_> { + fn drop(&mut self) { + if let Some(name) = self.name.take() { + let buf = self.client.with_buf(|buf| { + frontend::close(b'S', &name, buf).unwrap(); + frontend::sync(buf); + buf.split().freeze() + }); + let _ = self + .client + .send(RequestMessages::Single(FrontendMessage::Raw(buf))); + } + } +} + pub async fn prepare( client: &Arc, query: &str, @@ -66,6 +95,12 @@ pub async fn prepare( let name = format!("s{}", NEXT_ID.fetch_add(1, Ordering::SeqCst)); let buf = encode(client, &name, query, types)?; let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?; + // From this point on the server may own a statement named `name`; make sure + // it is closed if this future is abandoned before `Statement` takes over. + let guard = CloseOnDrop { + client, + name: Some(name), + }; match responses.next().await? { Message::ParseComplete => {} @@ -106,7 +141,7 @@ pub async fn prepare( } } - Ok(Statement::new(client, name, parameters, columns)) + Ok(Statement::new(client, guard.disarm(), parameters, columns)) } fn prepare_rec<'a>( diff --git a/tokio-postgres/src/transaction.rs b/tokio-postgres/src/transaction.rs index 81e8036b..d901764e 100644 --- a/tokio-postgres/src/transaction.rs +++ b/tokio-postgres/src/transaction.rs @@ -332,7 +332,34 @@ impl<'a> Transaction<'a> { let depth = self.savepoint.as_ref().map_or(0, |sp| sp.depth) + 1; let name = name.unwrap_or_else(|| format!("sp_{depth}")); let query = format!("SAVEPOINT {name}"); - self.batch_execute(&query).await?; + + struct ReleaseIfNotDone<'me> { + client: &'me Client, + name: &'me str, + done: bool, + } + + impl Drop for ReleaseIfNotDone<'_> { + fn drop(&mut self) { + if self.done { + return; + } + + // The future was dropped after `SAVEPOINT` was sent but before the + // nested `Transaction` (which would roll it back on drop) was created. + self.client.__private_api_release_savepoint(self.name); + } + } + + { + let mut cleaner = ReleaseIfNotDone { + client: self.client, + name: &name, + done: false, + }; + self.client.batch_execute(&query).await?; + cleaner.done = true; + } Ok(Transaction { client: self.client,