Skip to content

Commit

Permalink
bugfix: the "open socket #n left in connection" alerts would appear i…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
agentzh committed Aug 17, 2012
1 parent 714081d commit c8aa3d3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/ngx_postgres_keepalive.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev)
c = ev->data;
item = c->data;

if (c->close) {
goto close;
}

if (PQconsumeInput(item->pgconn) && !PQisBusy(item->pgconn)) {
res = PQgetResult(item->pgconn);
if (res == NULL) {
Expand All @@ -283,13 +287,12 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev)
"postgres: received result on idle keepalive connection");
}

close:
pgscf = item->srv_conf;

ngx_queue_remove(&item->queue);

ngx_postgres_upstream_free_connection(ev->log, item->connection,
item->pgconn, pgscf);
ngx_postgres_upstream_free_connection(ev->log, c, item->pgconn, pgscf);

ngx_queue_remove(&item->queue);
ngx_queue_insert_head(&pgscf->free, &item->queue);

dd("returning");
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_postgres_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ ngx_postgres_upstream_free_connection(ngx_log_t *log, ngx_connection_t *c,
#endif

ngx_free_connection(c);

c->fd = (ngx_socket_t) -1;
}

/* free spot in keepalive connection pool */
Expand Down
24 changes: 23 additions & 1 deletion t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Test::Nginx::Socket;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3);
plan tests => repeat_each() * (blocks() * 5);

$ENV{TEST_NGINX_POSTGRESQL_HOST} ||= '127.0.0.1';
$ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432;
Expand Down Expand Up @@ -69,6 +69,9 @@ Content-Type: application/x-resty-dbd-stream
"bob". # field data
"\x{00}" # row list terminator
--- timeout: 10
--- no_error_log
[alert]
[error]
Expand Down Expand Up @@ -115,6 +118,9 @@ Content-Type: application/x-resty-dbd-stream
"bob". # field data
"\x{00}" # row list terminator
--- timeout: 10
--- no_error_log
[alert]
[error]
Expand Down Expand Up @@ -142,6 +148,10 @@ Content-Type: application/x-resty-dbd-stream
"\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id
"\x{00}\x{00}" # col count
--- timeout: 10
--- no_error_log
[alert]
[error]
--- LAST
Expand Down Expand Up @@ -178,6 +188,9 @@ Content-Type: application/x-resty-dbd-stream
"name". # col name data
"\x{00}" # row list terminator
--- timeout: 10
--- no_error_log
[alert]
[error]
Expand Down Expand Up @@ -206,6 +219,9 @@ Content-Type: application/x-resty-dbd-stream
"\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id
"\x{00}\x{00}" # col count
--- timeout: 10
--- no_error_log
[alert]
[error]
Expand All @@ -224,6 +240,9 @@ Content-Type: application/x-resty-dbd-stream
--- response_body eval
""
--- timeout: 10
--- no_error_log
[alert]
[error]
Expand Down Expand Up @@ -275,3 +294,6 @@ Content-Type: application/x-resty-dbd-stream
"bob". # field data
"\x{00}" # row list terminator
--- timeout: 10
--- no_error_log
[alert]
[error]

0 comments on commit c8aa3d3

Please sign in to comment.