Skip to content

Commit f3c5aa7

Browse files
committed
daemon: don't close conn until we've sent all the output packets.
Otherwise we won't finish the conversation. In fact, only the writer side should ever close: we wake it if we want to close and it tests peer->cond. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 8b666ea commit f3c5aa7

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

daemon/peer.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ static void state_single(struct peer *peer,
149149
bitcoind_send_tx(peer->dstate, broadcast);
150150
}
151151

152+
/* Start output if not running already; it will close conn. */
152153
if (peer->cond == PEER_CLOSED)
153-
io_close(peer->conn);
154+
io_wake(peer);
154155
}
155156

156157
static void try_command(struct peer *peer)
@@ -170,9 +171,6 @@ static void try_command(struct peer *peer)
170171
if (peer->curr_cmd.cmd != INPUT_NONE) {
171172
state_single(peer, peer->curr_cmd.cmd,
172173
&peer->curr_cmd.cmddata);
173-
174-
if (peer->cond == PEER_CLOSED)
175-
io_close(peer->conn);
176174
}
177175
}
178176
}
@@ -209,8 +207,12 @@ static struct io_plan *pkt_out(struct io_conn *conn, struct peer *peer)
209207
{
210208
Pkt *out;
211209

212-
if (peer->num_outpkt == 0)
210+
if (peer->num_outpkt == 0) {
211+
/* We close the connection once we've sent everything. */
212+
if (peer->cond == PEER_CLOSED)
213+
return io_close(conn);
213214
return io_out_wait(conn, peer, pkt_out, peer);
215+
}
214216

215217
out = peer->outpkt[--peer->num_outpkt];
216218
return peer_write_packet(conn, peer, out, pkt_out);
@@ -222,16 +224,14 @@ static struct io_plan *pkt_in(struct io_conn *conn, struct peer *peer)
222224
const tal_t *ctx = tal(peer, char);
223225

224226
idata.pkt = tal_steal(ctx, peer->inpkt);
225-
state_event(peer, peer->inpkt->pkt_case, &idata);
227+
228+
/* We ignore packets if they tell us to. */
229+
if (peer->cond != PEER_CLOSED)
230+
state_event(peer, peer->inpkt->pkt_case, &idata);
226231

227232
/* Free peer->inpkt unless stolen above. */
228233
tal_free(ctx);
229234

230-
/* If we've closed (above), don't try to read (we can call
231-
* io_close multiple times with no harm). */
232-
if (peer->cond == PEER_CLOSED)
233-
return io_close(conn);
234-
235235
return peer_read_packet(conn, peer, pkt_in);
236236
}
237237

@@ -252,11 +252,6 @@ static struct io_plan *peer_crypto_on(struct io_conn *conn, struct peer *peer)
252252
queue_cmd(peer, do_anchor_offer, NULL);
253253
try_command(peer);
254254

255-
/* If we've closed (above), don't continue (we can call
256-
* io_close multiple times with no harm). */
257-
if (peer->cond == PEER_CLOSED)
258-
return io_close(conn);
259-
260255
return io_duplex(conn,
261256
peer_read_packet(conn, peer, pkt_in),
262257
pkt_out(conn, peer));

0 commit comments

Comments
 (0)