Skip to content

Commit d27a5d3

Browse files
committed
lightningd/lightningd: shutdown subdaemons on exit.
Especially under valgrind, we should give them some time to exit. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 3c5a709 commit d27a5d3

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

lightningd/lightningd.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ void derive_peer_seed(struct lightningd *ld, struct privkey *peer_seed,
197197
ld->peer_counter++;
198198
}
199199

200+
static void shutdown_subdaemons(struct lightningd *ld)
201+
{
202+
struct peer *p;
203+
204+
/* Let everyone shutdown cleanly. */
205+
subd_shutdown(ld->hsm, 10);
206+
subd_shutdown(ld->gossip, 10);
207+
208+
/* Duplicates are OK: no need to check here. */
209+
list_for_each(&ld->peers, p, list)
210+
if (p->owner)
211+
subd_shutdown(p->owner, 0);
212+
}
213+
200214
int main(int argc, char *argv[])
201215
{
202216
struct lightningd *ld = new_lightningd(NULL);
@@ -250,7 +264,6 @@ int main(int argc, char *argv[])
250264
#if 0
251265
/* Load peers from database. */
252266
db_load_peers(dstate);
253-
254267
#endif
255268

256269
for (;;) {
@@ -265,6 +278,8 @@ int main(int argc, char *argv[])
265278
timer_expired(&ld->dstate, expired);
266279
}
267280

281+
shutdown_subdaemons(ld);
282+
268283
tal_free(ld);
269284
opt_free_table();
270285
return 0;

lightningd/subd.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,32 @@ void subd_req_(const tal_t *ctx,
431431
add_req(ctx, sd, type, num_fds_in, replycb, replycb_data);
432432
}
433433

434+
void subd_shutdown(struct subd *sd, unsigned int seconds)
435+
{
436+
/* Idempotent. */
437+
if (!sd->conn)
438+
return;
439+
440+
log_debug(sd->log, "Shutting down");
441+
442+
/* No finished callback any more. */
443+
sd->finished = NULL;
444+
/* Don't free sd when we close connection manually. */
445+
tal_steal(sd->ld, sd);
446+
/* Close connection: should begin shutdown now. */
447+
sd->conn = tal_free(sd->conn);
448+
449+
/* Do we actually want to wait? */
450+
while (seconds) {
451+
if (waitpid(sd->pid, NULL, WNOHANG) > 0) {
452+
tal_del_destructor(sd, destroy_subd);
453+
return;
454+
}
455+
sleep(1);
456+
seconds--;
457+
}
458+
}
459+
434460
char *opt_subd_debug(const char *optarg, struct lightningd *ld)
435461
{
436462
ld->dev_debug_subdaemon = optarg;

lightningd/subd.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ void subd_req_(const tal_t *ctx,
113113
bool (*replycb)(struct subd *, const u8 *, const int *, void *),
114114
void *replycb_data);
115115

116+
/**
117+
* subd_shutdown - try to politely shut down a subdaemon.
118+
* @subd: subd to shutdown.
119+
* @seconds: maximum seconds to wait for it to exit.
120+
*
121+
* This closes the fd to the subdaemon, and gives it a little while to exit.
122+
* The @finished callback will never be called.
123+
*/
124+
void subd_shutdown(struct subd *subd, unsigned int seconds);
125+
116126
char *opt_subd_debug(const char *optarg, struct lightningd *ld);
117127

118128
#endif /* LIGHTNING_LIGHTNINGD_SUBD_H */

0 commit comments

Comments
 (0)