Datasets:
Tasks:
Text Generation
Formats:
parquet
Sub-tasks:
language-modeling
Languages:
English
Size:
< 1K
License:
File size: 5,867 Bytes
fe4e0a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 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<InnerClient>,
+ name: Option<String>,
+}
+
+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<P, I>(
client: &Arc<InnerClient>,
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<InnerClient>,
+ name: Option<String>,
+}
+
+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<InnerClient>,
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,
|