Skip to content

Commit

Permalink
pool: set state to CONNECTING before initializing tcp handle
Browse files Browse the repository at this point in the history
This garuntees that if hsk_peer_open() fails, that hsk_peer_close()
will call uv_close() before freeing the peer, preventing a race
condition where libuv tries to free the connection handle after
the peer has been destroyed.
  • Loading branch information
pinheadmz committed Sep 21, 2020
1 parent 60a3796 commit 522f0b9
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,8 @@ hsk_peer_open(hsk_peer_t *peer, const hsk_addr_t *addr) {
assert(peer && addr);
assert(peer->pool && peer->loop && peer->state == HSK_STATE_DISCONNECTED);

peer->state = HSK_STATE_CONNECTING;

hsk_pool_t *pool = (hsk_pool_t *)peer->pool;
uv_loop_t *loop = pool->loop;

Expand Down Expand Up @@ -927,9 +929,6 @@ hsk_peer_open(hsk_peer_t *peer, const hsk_addr_t *addr) {
return HSK_EFAILURE;
}

peer->state = HSK_STATE_CONNECTING;


hsk_peer_t *peerIter, *next;
uint64_t active = 0;
for (peerIter = pool->head; peerIter; peerIter = next) {
Expand Down

0 comments on commit 522f0b9

Please sign in to comment.