mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-04-15 16:39:14 +00:00
[mod_pgsql] Fix potential hang when calling PQconsumeInput() while checking if db_is_up().
This commit is contained in:
parent
b5459fb5f5
commit
034a7eea5e
@ -113,6 +113,7 @@ static int db_is_up(switch_pgsql_handle_t *handle)
|
|||||||
int max_tries = DEFAULT_PGSQL_RETRIES;
|
int max_tries = DEFAULT_PGSQL_RETRIES;
|
||||||
int code = 0;
|
int code = 0;
|
||||||
int recon = 0;
|
int recon = 0;
|
||||||
|
switch_byte_t sanity = 255;
|
||||||
|
|
||||||
if (handle) {
|
if (handle) {
|
||||||
max_tries = handle->num_retries;
|
max_tries = handle->num_retries;
|
||||||
@ -132,10 +133,24 @@ top:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Try a non-blocking read on the connection to gobble up any EOF from a closed connection and mark the connection BAD if it is closed. */
|
/* Try a non-blocking read on the connection to gobble up any EOF from a closed connection and mark the connection BAD if it is closed. */
|
||||||
PQconsumeInput(handle->con);
|
while (--sanity > 0)
|
||||||
|
{
|
||||||
|
if (PQisBusy(handle->con)) {
|
||||||
|
PQconsumeInput(handle->con);
|
||||||
|
switch_yield(1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sanity) {
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Can not check DB Connection status: sanity = 0. Reconnecting...\n");
|
||||||
|
goto reset;
|
||||||
|
}
|
||||||
|
|
||||||
if (PQstatus(handle->con) == CONNECTION_BAD) {
|
if (PQstatus(handle->con) == CONNECTION_BAD) {
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "PQstatus returned bad connection; reconnecting...\n");
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "PQstatus returned bad connection; reconnecting...\n");
|
||||||
|
reset:
|
||||||
handle->state = SWITCH_PGSQL_STATE_ERROR;
|
handle->state = SWITCH_PGSQL_STATE_ERROR;
|
||||||
PQreset(handle->con);
|
PQreset(handle->con);
|
||||||
if (PQstatus(handle->con) == CONNECTION_BAD) {
|
if (PQstatus(handle->con) == CONNECTION_BAD) {
|
||||||
@ -486,15 +501,13 @@ error:
|
|||||||
err_str = pgsql_handle_get_error(handle);
|
err_str = pgsql_handle_get_error(handle);
|
||||||
|
|
||||||
if (zstr(err_str)) {
|
if (zstr(err_str)) {
|
||||||
if (zstr(er)) {
|
if (!er) {
|
||||||
err_str = strdup((char *)"SQL ERROR!");
|
err_str = strdup((char *)"SQL ERROR!");
|
||||||
} else {
|
} else {
|
||||||
err_str = er;
|
err_str = er;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!zstr(er)) {
|
switch_safe_free(er);
|
||||||
free(er);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (err_str) {
|
if (err_str) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user