Skip to content

Commit

Permalink
Release GVL while calling PQsetClientEncoding()
Browse files Browse the repository at this point in the history
PQsetClientEncoding() is also called within PG::Connection.new
which could block due to network delay.

This fixes bitbucket issue ged#245 .
  • Loading branch information
larskanis committed Aug 13, 2016
1 parent e5cb1df commit faa57c4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ext/gvl_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ extern void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
#define FOR_EACH_PARAM_OF_PQsendDescribePortal(param) \
param(PGconn *, conn)

#define FOR_EACH_PARAM_OF_PQsetClientEncoding(param) \
param(PGconn *, conn)

#define FOR_EACH_PARAM_OF_PQisBusy(param)

#define FOR_EACH_PARAM_OF_PQcancel(param) \
Expand Down Expand Up @@ -226,6 +229,7 @@ extern void *rb_thread_call_without_gvl(void *(*func)(void *), void *data1,
function(PQsendQueryPrepared, GVL_TYPE_NONVOID, int, int, resultFormat) \
function(PQsendDescribePrepared, GVL_TYPE_NONVOID, int, const char *, stmt) \
function(PQsendDescribePortal, GVL_TYPE_NONVOID, int, const char *, portal) \
function(PQsetClientEncoding, GVL_TYPE_NONVOID, int, const char *, encoding) \
function(PQisBusy, GVL_TYPE_NONVOID, int, PGconn *, conn) \
function(PQcancel, GVL_TYPE_NONVOID, int, int, errbufsize);

Expand Down
6 changes: 3 additions & 3 deletions ext/pg_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2967,7 +2967,7 @@ pgconn_set_client_encoding(VALUE self, VALUE str)

Check_Type(str, T_STRING);

if ( (PQsetClientEncoding(conn, StringValueCStr(str))) == -1 ) {
if ( (gvl_PQsetClientEncoding(conn, StringValueCStr(str))) == -1 ) {
rb_raise(rb_ePGerror, "invalid encoding name: %s",StringValueCStr(str));
}
#ifdef M17N_SUPPORTED
Expand Down Expand Up @@ -3629,7 +3629,7 @@ pgconn_internal_encoding_set(VALUE self, VALUE enc)
rb_encoding *rbenc = rb_to_encoding( enc );
const char *name = pg_get_rb_encoding_as_pg_encoding( rbenc );

if ( PQsetClientEncoding(pg_get_pgconn( self ), name) == -1 ) {
if ( gvl_PQsetClientEncoding(pg_get_pgconn( self ), name) == -1 ) {
VALUE server_encoding = pgconn_external_encoding( self );
rb_raise( rb_eEncCompatError, "incompatible character encodings: %s and %s",
rb_enc_name(rb_to_encoding(server_encoding)), name );
Expand Down Expand Up @@ -3689,7 +3689,7 @@ pgconn_set_default_encoding( VALUE self )

if (( enc = rb_default_internal_encoding() )) {
encname = pg_get_rb_encoding_as_pg_encoding( enc );
if ( PQsetClientEncoding(conn, encname) != 0 )
if ( gvl_PQsetClientEncoding(conn, encname) != 0 )
rb_warn( "Failed to set the default_internal encoding to %s: '%s'",
encname, PQerrorMessage(conn) );
pgconn_set_internal_encoding_index( self );
Expand Down

0 comments on commit faa57c4

Please sign in to comment.