Skip to content

Commit d5bd168

Browse files
committed
lightningd: free timers on shutdown.
Direct leak of 1024 byte(s) in 2 object(s) allocated from: #0 0x7f4c84ce4448 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10c448) #1 0x55d11b782c96 in timer_default_alloc ccan/ccan/timer/timer.c:16 #2 0x55d11b7832b7 in add_level ccan/ccan/timer/timer.c:166 #3 0x55d11b783864 in timer_fast_forward ccan/ccan/timer/timer.c:334 #4 0x55d11b78396a in timers_expire ccan/ccan/timer/timer.c:359 #5 0x55d11b774993 in io_loop ccan/ccan/io/poll.c:395 #6 0x55d11b72322f in plugins_init lightningd/plugin.c:1013 #7 0x55d11b7060ea in main lightningd/lightningd.c:664 #8 0x7f4c84696b6a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x26b6a) To fix this, we actually make 'ld->timers' a pointer, so we can clean it up last of all. We can't free it before ld, because that causes timers to be destroyed. Signed-off-by: Rusty Russell <[email protected]>
1 parent 43d5492 commit d5bd168

File tree

10 files changed

+21
-13
lines changed

10 files changed

+21
-13
lines changed

lightningd/bitcoind.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static void bcli_failure(struct bitcoind *bitcoind,
161161
bitcoind->error_count++;
162162

163163
/* Retry in 1 second (not a leak!) */
164-
new_reltimer(&bitcoind->ld->timers, notleak(bcli), time_from_sec(1),
164+
new_reltimer(bitcoind->ld->timers, notleak(bcli), time_from_sec(1),
165165
retry_bcli, bcli);
166166
}
167167

lightningd/connect_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ void delay_then_reconnect(struct channel *channel, u32 seconds_delay,
222222

223223
/* We fuzz the timer by up to 1 second, to avoid getting into
224224
* simultanous-reconnect deadlocks with peer. */
225-
notleak(new_reltimer(&ld->timers, d,
225+
notleak(new_reltimer(ld->timers, d,
226226
timerel_add(time_from_sec(seconds_delay),
227227
time_from_usec(pseudorand(1000000))),
228228
maybe_reconnect, d));

lightningd/io_loop_with_timers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void *io_loop_with_timers(struct lightningd *ld)
2121
* It will only exit if there's an expired timer, *or* someone
2222
* calls io_break, or if there are no more file descriptors
2323
* (which never happens in our code). */
24-
retval = io_loop(&ld->timers, &expired);
24+
retval = io_loop(ld->timers, &expired);
2525

2626
/*~ Notice that timers are called here in the event loop like
2727
* anything else, so there are no weird concurrency issues. */

lightningd/jsonrpc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void slowcmd_finish(struct slowcmd *sc)
265265
static void slowcmd_start(struct slowcmd *sc)
266266
{
267267
sc->js = json_stream_success(sc->cmd);
268-
new_reltimer(&sc->cmd->ld->timers, sc, time_from_msec(*sc->msec),
268+
new_reltimer(sc->cmd->ld->timers, sc, time_from_msec(*sc->msec),
269269
slowcmd_finish, sc);
270270
}
271271

@@ -282,7 +282,7 @@ static struct command_result *json_slowcmd(struct command *cmd,
282282
NULL))
283283
return command_param_failed();
284284

285-
new_reltimer(&cmd->ld->timers, sc, time_from_msec(0), slowcmd_start, sc);
285+
new_reltimer(cmd->ld->timers, sc, time_from_msec(0), slowcmd_start, sc);
286286
return command_still_pending(cmd);
287287
}
288288

lightningd/lightningd.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
189189
* ingenious bucket system which more precisely sorts timers as they
190190
* approach expiry. It's a fascinating implementation you should read
191191
* if you have a spare few hours. */
192-
timers_init(&ld->timers, time_mono());
192+
ld->timers = tal(ld, struct timers);
193+
timers_init(ld->timers, time_mono());
193194

194195
/*~ This is detailed in chaintopology.c */
195196
ld->topology = new_topology(ld, ld->log);
@@ -619,6 +620,7 @@ int main(int argc, char *argv[])
619620
u32 min_blockheight, max_blockheight;
620621
int connectd_gossipd_fd, pid_fd;
621622
int stop_fd;
623+
struct timers *timers;
622624
const char *stop_response;
623625

624626
/*~ What happens in strange locales should stay there. */
@@ -677,7 +679,7 @@ int main(int argc, char *argv[])
677679
/*~ Our "wallet" code really wraps the db, which is more than a simple
678680
* bitcoin wallet (though it's that too). It also stores channel
679681
* states, invoices, payments, blocks and bitcoin transactions. */
680-
ld->wallet = wallet_new(ld, ld->log, &ld->timers);
682+
ld->wallet = wallet_new(ld, ld->log, ld->timers);
681683

682684
/*~ We keep a filter of scriptpubkeys we're interested in. */
683685
ld->owned_txfilter = txfilter_new(ld);
@@ -750,7 +752,7 @@ int main(int argc, char *argv[])
750752

751753
/*~ Initialize block topology. This does its own io_loop to
752754
* talk to bitcoind, so does its own db transactions. */
753-
setup_topology(ld->topology, &ld->timers,
755+
setup_topology(ld->topology, ld->timers,
754756
min_blockheight, max_blockheight);
755757

756758
/*~ Pull peers, channels and HTLCs from db. Needs to happen after the
@@ -854,7 +856,13 @@ int main(int argc, char *argv[])
854856
/* FIXME: pay can have children off tmpctx which unlink from
855857
* ld->payments, so clean that up. */
856858
clean_tmpctx();
859+
860+
/* Free this last: other things may clean up timers. */
861+
timers = tal_steal(NULL, ld->timers);
857862
tal_free(ld);
863+
864+
timers_cleanup(timers);
865+
tal_free(timers);
858866
opt_free_table();
859867

860868
daemon_shutdown();

lightningd/lightningd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct lightningd {
112112
u8 *rgb; /* tal_len() == 3. */
113113

114114
/* Any pending timers. */
115-
struct timers timers;
115+
struct timers *timers;
116116

117117
/* Port we're listening on */
118118
u16 portnum;

lightningd/memdump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static void report_leak_info(struct command *cmd, struct subd *leaker)
209209
leak_info->leaker = leaker;
210210

211211
/* Leak detection in a reply handler thinks we're leaking conn. */
212-
notleak(new_reltimer(&leak_info->cmd->ld->timers, leak_info->cmd,
212+
notleak(new_reltimer(leak_info->cmd->ld->timers, leak_info->cmd,
213213
time_from_sec(0),
214214
report_leak_info2, leak_info));
215215
}

lightningd/pay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ static struct command_result *json_waitsendpay(struct command *cmd,
921921
return res;
922922

923923
if (timeout)
924-
new_reltimer(&cmd->ld->timers, cmd, time_from_sec(*timeout),
924+
new_reltimer(cmd->ld->timers, cmd, time_from_sec(*timeout),
925925
&waitsendpay_timeout, cmd);
926926
return command_still_pending(cmd);
927927
}

lightningd/peer_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ register_close_command(struct lightningd *ld,
336336
tal_add_destructor2(channel,
337337
&destroy_close_command_on_channel_destroy,
338338
cc);
339-
new_reltimer(&ld->timers, cc, time_from_sec(timeout),
339+
new_reltimer(ld->timers, cc, time_from_sec(timeout),
340340
&close_command_timeout, cc);
341341
}
342342

lightningd/peer_htlcs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ enum onion_type send_htlc_out(struct channel *out,
456456

457457
/* Give channel 30 seconds to commit (first) htlc. */
458458
if (!out->htlc_timeout)
459-
out->htlc_timeout = new_reltimer(&out->peer->ld->timers,
459+
out->htlc_timeout = new_reltimer(out->peer->ld->timers,
460460
out, time_from_sec(30),
461461
htlc_offer_timeout,
462462
out);

0 commit comments

Comments
 (0)