diff --git a/src/ckdb.h b/src/ckdb.h index 41dd3608..18da3190 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -52,7 +52,7 @@ #define DB_VLOCK "1" #define DB_VERSION "0.9.6" -#define CKDB_VERSION DB_VERSION"-0.771" +#define CKDB_VERSION DB_VERSION"-0.780" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index 991d6774..c0aaeeaa 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -1364,6 +1364,7 @@ static char *cmd_percent(char *cmd, char *id, tv_t *now, USERS *users) pa_item = prev_in_ktree(pay_ctx); DATA_PAYMENTADDRESSES_NULL(pa, pa_item); } + K_RUNLOCK(paymentaddresses_free); snprintf(tmp, sizeof(tmp), "rows=%d%cflds=%s%c", diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index c8093800..64aa9437 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -1448,7 +1448,7 @@ bool paymentaddresses_set(PGconn *conn, int64_t userid, K_STORE *pa_store, PAYMENTADDRESSES *row, *pa; char *upd = NULL, *ins; size_t len, off; - bool ok = false, first; + bool ok = false, first, locked = false; char *params[1002]; // Limit of 999 addresses per user char tmp[1024]; int n, par = 0, count, matches; @@ -1459,11 +1459,6 @@ bool paymentaddresses_set(PGconn *conn, int64_t userid, K_STORE *pa_store, if (pa_store->count > 999) return false; - /* Since we are merging the changes in rather than just - * replacing the db contents, lock the data for the duration - * of the update to ensure nothing else changes it */ - K_WLOCK(paymentaddresses_free); - if (conn == NULL) { conn = dbconnect(); conned = true; @@ -1491,6 +1486,12 @@ bool paymentaddresses_set(PGconn *conn, int64_t userid, K_STORE *pa_store, params[par++] = bigint_to_buf(userid, NULL, 0); params[par++] = tv_to_buf((tv_t *)&default_expiry, NULL, 0); + /* Since we are merging the changes in rather than just + * replacing the db contents, lock the data for the duration + * of the update to ensure nothing else changes it */ + K_WLOCK(paymentaddresses_free); + locked = true; + first = true; item = find_paymentaddresses(userid, ctx); DATA_PAYMENTADDRESSES_NULL(row, item); @@ -1663,9 +1664,10 @@ unparam: match = next; } } - LOGDEBUG("%s(): Step 3, untouched %d expired %d added %d", __func__, matches, n, count); + if (locked) + K_WUNLOCK(paymentaddresses_free); - K_WUNLOCK(paymentaddresses_free); + LOGDEBUG("%s(): Step 3, untouched %d expired %d added %d", __func__, matches, n, count); // Calling function must clean up anything left in pa_store return ok;