handle case where get_session() is called from main, i.e. in combination with -f option

This commit is contained in:
Christian Grothoff 2020-01-20 11:15:19 +01:00
parent 72293a25ac
commit 85f10a86ff
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
2 changed files with 36 additions and 8 deletions

View File

@ -47,6 +47,6 @@ export GNUNET_FORCE_LOG=";;;;ERROR"
for n in afl-tests/* for n in afl-tests/*
do do
echo -n "Test $n " echo -n "Test $n "
$PREFIX taler-exchange-httpd -c test_taler_exchange_httpd.conf -t 1 -f $n -C > /dev/null && echo "OK" || echo "FAIL $!!" $PREFIX taler-exchange-httpd -c test_taler_exchange_httpd.conf -t 1 -f $n -C > /dev/null && echo "OK" || echo "FAIL"
done done
exit 0 exit 0

View File

@ -134,6 +134,16 @@ struct PostgresClosure
* Which currency should we assume all amounts to be in? * Which currency should we assume all amounts to be in?
*/ */
char *currency; char *currency;
/**
* Session to be used if the thread is @e main_self.
*/
struct TALER_EXCHANGEDB_Session *main_session;
/**
* Handle for the main() thread of the program.
*/
pthread_t main_self;
}; };
@ -223,7 +233,12 @@ postgres_get_session (void *cls)
struct GNUNET_PQ_Context *db_conn; struct GNUNET_PQ_Context *db_conn;
struct TALER_EXCHANGEDB_Session *session; struct TALER_EXCHANGEDB_Session *session;
if (NULL != (session = pthread_getspecific (pc->db_conn_threadlocal))) if (pthread_equal (pc->main_self,
pthread_self ()))
session = pc->main_session;
else
session = pthread_getspecific (pc->db_conn_threadlocal);
if (NULL != session)
{ {
GNUNET_PQ_reconnect_if_down (session->conn); GNUNET_PQ_reconnect_if_down (session->conn);
return session; return session;
@ -1381,13 +1396,21 @@ postgres_get_session (void *cls)
return NULL; return NULL;
session = GNUNET_new (struct TALER_EXCHANGEDB_Session); session = GNUNET_new (struct TALER_EXCHANGEDB_Session);
session->conn = db_conn; session->conn = db_conn;
if (0 != pthread_setspecific (pc->db_conn_threadlocal, if (pthread_equal (pc->main_self,
session)) pthread_self ()))
{ {
GNUNET_break (0); pc->main_session = session;
GNUNET_PQ_disconnect (db_conn); }
GNUNET_free (session); else
return NULL; {
if (0 != pthread_setspecific (pc->db_conn_threadlocal,
session))
{
GNUNET_break (0);
GNUNET_PQ_disconnect (db_conn);
GNUNET_free (session);
return NULL;
}
} }
return session; return session;
} }
@ -7195,6 +7218,7 @@ libtaler_plugin_exchangedb_postgres_init (void *cls)
const char *ec; const char *ec;
pg = GNUNET_new (struct PostgresClosure); pg = GNUNET_new (struct PostgresClosure);
pg->main_self = pthread_self (); /* loaded while single-threaded! */
if (GNUNET_OK != if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_filename (cfg, GNUNET_CONFIGURATION_get_value_filename (cfg,
"exchangedb-postgres", "exchangedb-postgres",
@ -7379,6 +7403,10 @@ libtaler_plugin_exchangedb_postgres_done (void *cls)
struct TALER_EXCHANGEDB_Plugin *plugin = cls; struct TALER_EXCHANGEDB_Plugin *plugin = cls;
struct PostgresClosure *pg = plugin->cls; struct PostgresClosure *pg = plugin->cls;
/* If we launched a session for the main thread,
kill it here before we unload */
if (NULL != pg->main_session)
db_conn_destroy (pg->main_session);
GNUNET_free (pg->connection_cfg_str); GNUNET_free (pg->connection_cfg_str);
GNUNET_free (pg->sql_dir); GNUNET_free (pg->sql_dir);
GNUNET_free (pg->currency); GNUNET_free (pg->currency);