Skip to content

Commit

Permalink
lightningd: cleanup, freeing jsonrpc in shutdown cannot trigger db wr…
Browse files Browse the repository at this point in the history
…ite's anymore

since PR #3867 utxos are unreserved by height, destroy_utxos and
related functions are not used anymore so clean them up also

However free(ld->jsonrpc) still needs to happen before free(ld) because its
destructors need list_head pointers from ld
  • Loading branch information
SimonVrouwe authored and rustyrussell committed Nov 30, 2021
1 parent 5f69674 commit 63bd569
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lightningd/jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum command_mode {
/* Context for a command (from JSON, but might outlive the connection!). */
/* FIXME: move definition into jsonrpc.c */
struct command {
/* Off json_cmd->commands */
/* Off list jcon->commands */
struct list_node list;
/* The global state */
struct lightningd *ld;
Expand Down
6 changes: 1 addition & 5 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1193,12 +1193,8 @@ int main(int argc, char *argv[])
/* Tell plugins we're shutting down, closes the db for write access. */
shutdown_plugins(ld);

/* Clean up the JSON-RPC. This needs to happen in a DB transaction since
* it might actually be touching the DB in some destructors, e.g.,
* unreserving UTXOs (see #1737) */
db_begin_transaction(ld->wallet->db);
/* Cleanup JSON RPC separately: destructors assume some list_head * in ld */
tal_free(ld->jsonrpc);
db_commit_transaction(ld->wallet->db);

/* Clean our our HTLC maps, since they use malloc. */
htlc_in_map_clear(&ld->htlcs_in);
Expand Down
27 changes: 0 additions & 27 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,35 +401,8 @@ bool wallet_unreserve_output(struct wallet *w,
OUTPUT_STATE_AVAILABLE);
}

/**
* unreserve_utxo - Mark a reserved UTXO as available again
*/
static void unreserve_utxo(struct wallet *w, const struct utxo *unres)
{
if (!wallet_update_output_status(w, &unres->outpoint,
OUTPUT_STATE_RESERVED,
OUTPUT_STATE_AVAILABLE)) {
fatal("Unable to unreserve output");
}
}

/**
* destroy_utxos - Destructor for an array of pointers to utxo
*/
static void destroy_utxos(const struct utxo **utxos, struct wallet *w)
{
for (size_t i = 0; i < tal_count(utxos); i++)
unreserve_utxo(w, utxos[i]);
}

void wallet_persist_utxo_reservation(struct wallet *w, const struct utxo **utxos)
{
tal_del_destructor2(utxos, destroy_utxos, w);
}

void wallet_confirm_utxos(struct wallet *w, const struct utxo **utxos)
{
tal_del_destructor2(utxos, destroy_utxos, w);
for (size_t i = 0; i < tal_count(utxos); i++) {
if (!wallet_update_output_status(
w, &utxos[i]->outpoint,
Expand Down
7 changes: 0 additions & 7 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1411,13 +1411,6 @@ void add_unreleased_tx(struct wallet *w, struct unreleased_tx *utx);
/* These will touch the db, so need to be explicitly freed. */
void free_unreleased_txs(struct wallet *w);

/* wallet_persist_utxo_reservation - Removes destructor
*
* Persists the reservation in the database (until a restart)
* instead of clearing the reservation when the utxo object
* is destroyed */
void wallet_persist_utxo_reservation(struct wallet *w, const struct utxo **utxos);

/* wallet_unreserve_output - Unreserve a utxo
*
* We unreserve utxos so that they can be spent elsewhere.
Expand Down

0 comments on commit 63bd569

Please sign in to comment.