Skip to content

Commit ccbca66

Browse files
authoredMay 9, 2022
Poorly behaved client fix (#65)
* Poorly behaved client fix * yes officer * fix tests * no useless rescue * Looks ok
1 parent df85139 commit ccbca66

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed
 

‎src/client.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ impl Client {
359359
// Clean up the server and re-use it.
360360
// This prevents connection thrashing by bad clients.
361361
if server.in_transaction() {
362-
server.query("ROLLBACK; DISCARD ALL;").await?;
362+
server.query("ROLLBACK").await?;
363+
server.query("DISCARD ALL").await?;
363364
}
364365

365366
return Err(err);
@@ -429,7 +430,8 @@ impl Client {
429430
// Pgbouncer closes the connection which leads to
430431
// connection thrashing when clients misbehave.
431432
if server.in_transaction() {
432-
server.query("ROLLBACK; DISCARD ALL;").await?;
433+
server.query("ROLLBACK").await?;
434+
server.query("DISCARD ALL").await?;
433435
}
434436

435437
return Ok(());

‎tests/ruby/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ source "https://rubygems.org"
22

33
gem "pg"
44
gem "activerecord"
5+
gem "rubocop"

‎tests/ruby/Gemfile.lock

+21
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,41 @@ GEM
1111
i18n (>= 1.6, < 2)
1212
minitest (>= 5.1)
1313
tzinfo (~> 2.0)
14+
ast (2.4.2)
1415
concurrent-ruby (1.1.9)
1516
i18n (1.10.0)
1617
concurrent-ruby (~> 1.0)
1718
minitest (5.15.0)
19+
parallel (1.22.1)
20+
parser (3.1.2.0)
21+
ast (~> 2.4.1)
1822
pg (1.3.2)
23+
rainbow (3.1.1)
24+
regexp_parser (2.3.1)
25+
rexml (3.2.5)
26+
rubocop (1.29.0)
27+
parallel (~> 1.10)
28+
parser (>= 3.1.0.0)
29+
rainbow (>= 2.2.2, < 4.0)
30+
regexp_parser (>= 1.8, < 3.0)
31+
rexml (>= 3.2.5, < 4.0)
32+
rubocop-ast (>= 1.17.0, < 2.0)
33+
ruby-progressbar (~> 1.7)
34+
unicode-display_width (>= 1.4.0, < 3.0)
35+
rubocop-ast (1.17.0)
36+
parser (>= 3.1.1.0)
37+
ruby-progressbar (1.11.0)
1938
tzinfo (2.0.4)
2039
concurrent-ruby (~> 1.0)
40+
unicode-display_width (2.1.0)
2141

2242
PLATFORMS
2343
x86_64-linux
2444

2545
DEPENDENCIES
2646
activerecord
2747
pg
48+
rubocop
2849

2950
BUNDLED WITH
3051
2.3.7

‎tests/ruby/tests.rb

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# frozen_string_literal: true
22

33
require 'active_record'
4+
require 'pg'
5+
6+
$stdout.sync = true
47

58
# Uncomment these two to see all queries.
69
# ActiveRecord.verbose_query_logs = true
@@ -110,3 +113,17 @@ def down
110113
rescue ActiveRecord::StatementInvalid
111114
puts 'OK'
112115
end
116+
117+
# Test evil clients
118+
def poorly_behaved_client
119+
conn = PG::connect("postgres://sharding_user:sharding_user@127.0.0.1:6432/rails_dev")
120+
conn.async_exec 'BEGIN'
121+
conn.async_exec 'SELECT 1'
122+
123+
conn.close
124+
puts 'Bad client ok'
125+
end
126+
127+
25.times do
128+
poorly_behaved_client
129+
end

0 commit comments

Comments
 (0)
Please sign in to comment.