diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 1a3160430..37a8ec4b9 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -192,10 +192,17 @@ static struct command_result *json_stop(struct command *cmd, if (!param(cmd, buffer, params, NULL)) return command_param_failed(); - /* This can't have closed yet! */ - cmd->ld->stop_conn = cmd->jcon->conn; log_unusual(cmd->ld->log, "JSON-RPC shutdown"); + /* With rpc_command_hook, jcon might have closed in the meantime! */ + if (!cmd->jcon) { + /* Return us to toplevel lightningd.c */ + io_break(cmd->ld); + return command_still_pending(cmd); + } + + cmd->ld->stop_conn = cmd->jcon->conn; + /* This is the one place where result is a literal string. */ jout = json_out_new(tmpctx); json_out_start(jout, NULL, '{'); diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 8de0457ee..26f2694a9 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -1021,12 +1021,13 @@ int main(int argc, char *argv[]) assert(io_loop_ret == ld); ld->state = LD_STATE_SHUTDOWN; + stop_fd = -1; + stop_response = NULL; + /* Were we exited via `lightningd_exit`? */ if (ld->exit_code) { exit_code = *ld->exit_code; - stop_fd = -1; - stop_response = NULL; - } else { + } else if (ld->stop_conn) { /* Keep this fd around, to write final response at the end. */ stop_fd = io_conn_fd(ld->stop_conn); io_close_taken_fd(ld->stop_conn);