[mod_pgsql] Prevent a stall of PQconsumeInput().

This commit is contained in:
Andrey Volk 2020-08-04 23:06:59 +04:00
parent 4fe8aecbfb
commit 785d92a5f5

View File

@ -373,6 +373,20 @@ switch_status_t pgsql_handle_connect(switch_pgsql_handle_t *handle)
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
if (PQsetnonblocking(handle->con, 1) == -1) {
char *err_str;
if ((err_str = pgsql_handle_get_error(handle))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s\n", err_str);
switch_safe_free(err_str);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to setup socket for the database [%s]\n", handle->dsn);
pgsql_handle_disconnect(handle);
}
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connected to [%s]\n", handle->dsn); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connected to [%s]\n", handle->dsn);
handle->state = SWITCH_PGSQL_STATE_CONNECTED; handle->state = SWITCH_PGSQL_STATE_CONNECTED;
handle->sock = PQsocket(handle->con); handle->sock = PQsocket(handle->con);
@ -635,6 +649,7 @@ switch_status_t pgsql_next_result_timed(switch_pgsql_handle_t *handle, switch_pg
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
if (PQisBusy(handle->con)) {
/* Try to consume input that might be waiting right away */ /* Try to consume input that might be waiting right away */
if (PQconsumeInput(handle->con)) { if (PQconsumeInput(handle->con)) {
/* And check to see if we have a full result ready for reading */ /* And check to see if we have a full result ready for reading */
@ -719,6 +734,7 @@ switch_status_t pgsql_next_result_timed(switch_pgsql_handle_t *handle, switch_pg
/* pgsql_cancel(handle); */ /* pgsql_cancel(handle); */
goto error; goto error;
} }
}
/* At this point, we know we can read a full result without blocking. */ /* At this point, we know we can read a full result without blocking. */
if (!(res = malloc(sizeof(switch_pgsql_result_t)))) { if (!(res = malloc(sizeof(switch_pgsql_result_t)))) {