Skip to content

Commit f60d399

Browse files
committed
Handle conditions in Drop impls
Aborting the process is sadface
1 parent 47ef8ea commit f60d399

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

src/lib.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use extra::digest::Digest;
44
use extra::md5::Md5;
55
use extra::url::Url;
66
use std::cell::Cell;
7+
use std::rt::io::io_error;
78
use std::rt::io::net::ip::SocketAddr;
89
use std::rt::io::net::tcp::TcpStream;
910
use std::str;
@@ -19,7 +20,9 @@ pub struct PostgresConnection {
1920

2021
impl Drop for PostgresConnection {
2122
fn drop(&self) {
22-
self.write_message(&Terminate);
23+
do io_error::cond.trap(|_| {}).inside {
24+
self.write_message(&Terminate);
25+
}
2326
}
2427
}
2528

@@ -183,12 +186,14 @@ pub struct PostgresStatement<'self> {
183186
#[unsafe_destructor]
184187
impl<'self> Drop for PostgresStatement<'self> {
185188
fn drop(&self) {
186-
self.conn.write_message(&Close('S' as u8, self.name.as_slice()));
187-
self.conn.write_message(&Sync);
188-
loop {
189-
match self.conn.read_message() {
190-
ReadyForQuery(*) => break,
191-
_ => ()
189+
do io_error::cond.trap(|_| {}).inside {
190+
self.conn.write_message(&Close('S' as u8, self.name.as_slice()));
191+
self.conn.write_message(&Sync);
192+
loop {
193+
match self.conn.read_message() {
194+
ReadyForQuery(*) => break,
195+
_ => ()
196+
}
192197
}
193198
}
194199
}
@@ -285,12 +290,15 @@ pub struct PostgresResult<'self> {
285290
#[unsafe_destructor]
286291
impl<'self> Drop for PostgresResult<'self> {
287292
fn drop(&self) {
288-
self.stmt.conn.write_message(&Close('P' as u8, self.name.as_slice()));
289-
self.stmt.conn.write_message(&Sync);
290-
loop {
291-
match self.stmt.conn.read_message() {
292-
ReadyForQuery(*) => break,
293-
_ => ()
293+
do io_error::cond.trap(|_| {}).inside {
294+
self.stmt.conn.write_message(&Close('P' as u8,
295+
self.name.as_slice()));
296+
self.stmt.conn.write_message(&Sync);
297+
loop {
298+
match self.stmt.conn.read_message() {
299+
ReadyForQuery(*) => break,
300+
_ => ()
301+
}
294302
}
295303
}
296304
}

src/test.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,25 @@ fn test_plaintext_pass_no_pass() {
6363
PostgresConnection::connect("postgres://pass_user@127.0.0.1:5432");
6464
}
6565

66+
#[test]
67+
#[should_fail]
68+
fn test_plaintext_pass_wrong_pass() {
69+
PostgresConnection::connect("postgres://pass_user:asdf@127.0.0.1:5432");
70+
}
71+
6672
#[test]
6773
fn test_md5_pass() {
6874
PostgresConnection::connect("postgres://md5_user:password@127.0.0.1:5432");
6975
}
76+
77+
#[test]
78+
#[should_fail]
79+
fn test_md5_pass_no_pass() {
80+
PostgresConnection::connect("postgres://md5_user@127.0.0.1:5432");
81+
}
82+
83+
#[test]
84+
#[should_fail]
85+
fn test_md5_pass_wrong_pass() {
86+
PostgresConnection::connect("postgres://md5_user:asdf@127.0.0.1:5432");
87+
}

0 commit comments

Comments
 (0)