Skip to content

Commit c8aa3d3

Browse files
committed
bugfix: the "open socket #n left in connection" alerts would appear in the nginx error log file when the pg connection pool was used and the worker process was shutting down. this issue could be captured by running t/sanity.t with the environment TEST_NGINX_USE_HUP=1.
1 parent 714081d commit c8aa3d3

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/ngx_postgres_keepalive.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev)
269269
c = ev->data;
270270
item = c->data;
271271

272+
if (c->close) {
273+
goto close;
274+
}
275+
272276
if (PQconsumeInput(item->pgconn) && !PQisBusy(item->pgconn)) {
273277
res = PQgetResult(item->pgconn);
274278
if (res == NULL) {
@@ -283,13 +287,12 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev)
283287
"postgres: received result on idle keepalive connection");
284288
}
285289

290+
close:
286291
pgscf = item->srv_conf;
287292

288-
ngx_queue_remove(&item->queue);
289-
290-
ngx_postgres_upstream_free_connection(ev->log, item->connection,
291-
item->pgconn, pgscf);
293+
ngx_postgres_upstream_free_connection(ev->log, c, item->pgconn, pgscf);
292294

295+
ngx_queue_remove(&item->queue);
293296
ngx_queue_insert_head(&pgscf->free, &item->queue);
294297

295298
dd("returning");

src/ngx_postgres_upstream.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ ngx_postgres_upstream_free_connection(ngx_log_t *log, ngx_connection_t *c,
572572
#endif
573573

574574
ngx_free_connection(c);
575+
576+
c->fd = (ngx_socket_t) -1;
575577
}
576578

577579
/* free spot in keepalive connection pool */

t/sanity.t

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
55

66
repeat_each(2);
77

8-
plan tests => repeat_each() * (blocks() * 3);
8+
plan tests => repeat_each() * (blocks() * 5);
99

1010
$ENV{TEST_NGINX_POSTGRESQL_HOST} ||= '127.0.0.1';
1111
$ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432;
@@ -69,6 +69,9 @@ Content-Type: application/x-resty-dbd-stream
6969
"bob". # field data
7070
"\x{00}" # row list terminator
7171
--- timeout: 10
72+
--- no_error_log
73+
[alert]
74+
[error]
7275
7376
7477
@@ -115,6 +118,9 @@ Content-Type: application/x-resty-dbd-stream
115118
"bob". # field data
116119
"\x{00}" # row list terminator
117120
--- timeout: 10
121+
--- no_error_log
122+
[alert]
123+
[error]
118124
119125
120126
@@ -142,6 +148,10 @@ Content-Type: application/x-resty-dbd-stream
142148
"\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id
143149
"\x{00}\x{00}" # col count
144150
--- timeout: 10
151+
--- no_error_log
152+
[alert]
153+
[error]
154+
--- LAST
145155
146156
147157
@@ -178,6 +188,9 @@ Content-Type: application/x-resty-dbd-stream
178188
"name". # col name data
179189
"\x{00}" # row list terminator
180190
--- timeout: 10
191+
--- no_error_log
192+
[alert]
193+
[error]
181194
182195
183196
@@ -206,6 +219,9 @@ Content-Type: application/x-resty-dbd-stream
206219
"\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id
207220
"\x{00}\x{00}" # col count
208221
--- timeout: 10
222+
--- no_error_log
223+
[alert]
224+
[error]
209225
210226
211227
@@ -224,6 +240,9 @@ Content-Type: application/x-resty-dbd-stream
224240
--- response_body eval
225241
""
226242
--- timeout: 10
243+
--- no_error_log
244+
[alert]
245+
[error]
227246
228247
229248
@@ -275,3 +294,6 @@ Content-Type: application/x-resty-dbd-stream
275294
"bob". # field data
276295
"\x{00}" # row list terminator
277296
--- timeout: 10
297+
--- no_error_log
298+
[alert]
299+
[error]

0 commit comments

Comments
 (0)