diff --git a/common/jsonrpc_errors.h b/common/jsonrpc_errors.h index d7e5219f5..f7cb09776 100644 --- a/common/jsonrpc_errors.h +++ b/common/jsonrpc_errors.h @@ -28,6 +28,9 @@ static const errcode_t PLUGIN_ERROR = -3; /* Plugin terminated while handling a request. */ static const errcode_t PLUGIN_TERMINATED = -4; +/* Lightningd is shutting down while handling a request. */ +static const errcode_t LIGHTNINGD_SHUTDOWN = -5; + /* Errors from `pay`, `sendpay`, or `waitsendpay` commands */ static const errcode_t PAY_IN_PROGRESS = 200; static const errcode_t PAY_RHASH_ALREADY_USED = 201; diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 3b2d2e077..abaa940d1 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -933,6 +933,11 @@ parse_request(struct json_connection *jcon, const jsmntok_t tok[]) json_tok_full(jcon->buffer, method)); } + if (jcon->ld->state == LD_STATE_SHUTDOWN) { + return command_fail(c, LIGHTNINGD_SHUTDOWN, + "lightningd is shutting down"); + } + rpc_hook = tal(c, struct rpc_command_hook_payload); rpc_hook->cmd = c; /* Duplicate since we might outlive the connection */ diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 6294c70aa..5a9cc58a6 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -1168,6 +1168,8 @@ int main(int argc, char *argv[]) * shut down. */ assert(io_loop_ret == ld); + + /* Fail JSON RPC requests and ignore plugin's responses */ ld->state = LD_STATE_SHUTDOWN; stop_fd = -1;