From 9ec804341c35d08a279f4d04a2f7cfb390837870 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 3 Jul 2017 11:31:21 +0930 Subject: [PATCH] channeld: fix intermittant failure on reading init msg from master. As tracked down by Christian; by setting up the master conn first, we make the master fd async. This means that the synchronous read (in init_channel) can fail with -EAGAIN, and indeed, Christian saw this when not running under valgrind. Signed-off-by: Rusty Russell --- lightningd/channel/channel.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 6555b121c..d983e0b57 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -1934,6 +1934,12 @@ out_next: return daemon_conn_read_next(conn, master); } +static void master_gone(struct io_conn *unused, struct daemon_conn *dc) +{ + /* Can't tell master, it's gone. */ + exit(2); +} + /* We do this synchronously. */ static void init_channel(struct peer *peer) { @@ -2001,6 +2007,10 @@ static void init_channel(struct peer *peer) status_failed(WIRE_CHANNEL_BAD_COMMAND, "Init: %s", tal_hex(msg, msg)); + /* After this we'll be async, so set up now. */ + daemon_conn_init(peer, &peer->master, REQ_FD, req_in, master_gone); + status_setup_async(&peer->master); + status_trace("init %s: remote_per_commit = %s, old_remote_per_commit = %s" " next_idx_local = %"PRIu64 " next_idx_remote = %"PRIu64 @@ -2059,12 +2069,6 @@ static void init_channel(struct peer *peer) } #ifndef TESTING -static void master_gone(struct io_conn *unused, struct daemon_conn *dc) -{ - /* Can't tell master, it's gone. */ - exit(2); -} - static void gossip_gone(struct io_conn *unused, struct daemon_conn *dc) { status_failed(WIRE_CHANNEL_GOSSIP_BAD_MESSAGE, @@ -2088,9 +2092,6 @@ int main(int argc, char *argv[]) secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - daemon_conn_init(peer, &peer->master, REQ_FD, req_in, master_gone); - status_setup_async(&peer->master); - peer->num_pings_outstanding = 0; timers_init(&peer->timers, time_mono()); peer->commit_timer = NULL;