Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit c75b969

Browse files
Add review suggestions
Signed-off-by: Norbert Heusser <norbert.heusser@cedalo.com>
1 parent c4e3819 commit c75b969

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/uv_tcp_connect.c

+17-25
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ struct uvTcpConnect
3232
struct uv_getaddrinfo_s getaddrinfo; /* DNS resolve request */
3333
struct uv_connect_s connect; /* TCP connection request */
3434
struct uv_write_s write; /* TCP handshake request */
35-
uv_check_t delayedtcpclose; /* A check handle required to delay closing tcp */
3635
int status; /* Returned to the request callback */
37-
queue queue; /* Pending connect queue */
36+
bool resolving; /* Indicate name resolving in progress */
37+
queue queue; /* Pending connect queue */
3838
};
3939

4040
/* Encode an handshake message into the given buffer. */
@@ -66,7 +66,6 @@ static void uvTcpConnectFinish(struct uvTcpConnect *connect)
6666
struct raft_uv_connect *req = connect->req;
6767
int status = connect->status;
6868
QUEUE_REMOVE(&connect->queue);
69-
uv_close((struct uv_handle_s *)&connect->delayedtcpclose, NULL);
7069
RaftHeapFree(connect->handshake.base);
7170
uv_freeaddrinfo(connect->getaddrinfo.addrinfo);
7271
raft_free(connect);
@@ -87,27 +86,16 @@ static void uvTcpConnectUvCloseCb(struct uv_handle_s *handle)
8786
UvTcpMaybeFireCloseCb(t);
8887
}
8988

90-
static void uvTcpConnectCheckMayClose(uv_check_t *handle)
91-
{
92-
struct uvTcpConnect *connect = handle->data;
93-
if (connect->status || uv_is_active((uv_handle_t *)connect->tcp)) {
94-
uv_check_stop(&connect->delayedtcpclose);
95-
uv_close((struct uv_handle_s *)connect->tcp, uvTcpConnectUvCloseCb);
96-
}
97-
}
98-
9989
/* Abort a connection request. */
10090
static void uvTcpConnectAbort(struct uvTcpConnect *connect)
10191
{
10292
QUEUE_REMOVE(&connect->queue);
10393
QUEUE_PUSH(&connect->t->aborting, &connect->queue);
104-
if (uv_cancel((struct uv_req_s *)&connect->getaddrinfo) == 0 ||
105-
!uv_is_active((uv_handle_t *)connect->tcp)) {
106-
/* If canceling the addrinfo call was not successfull, but the tcp
107-
handle is not active the getaddrinfo is in progress and we need to
108-
delay closing the tcp handle until it's finished */
109-
uv_check_start(&connect->delayedtcpclose, uvTcpConnectCheckMayClose);
110-
} else {
94+
uv_cancel((struct uv_req_s *)&connect->getaddrinfo);
95+
/* Call uv_close on the tcp handle, if there is no getaddrinfo request
96+
in flight. Data structures may only be freed after the uvGetAddrInfoCb was
97+
triggered. Tcp handle will be closed in the uvGetAddrInfoCb in this case. */
98+
if (!connect->resolving) {
11199
uv_close((struct uv_handle_s *)connect->tcp, uvTcpConnectUvCloseCb);
112100
}
113101
}
@@ -167,6 +155,7 @@ static void uvTcpConnectUvConnectCb(struct uv_connect_s *req, int status)
167155
uvTcpConnectAbort(connect);
168156
}
169157

158+
/* The hostname resolve is finished */
170159
static void uvGetAddrInfoCb(uv_getaddrinfo_t *req,
171160
int status,
172161
struct addrinfo *res)
@@ -175,8 +164,14 @@ static void uvGetAddrInfoCb(uv_getaddrinfo_t *req,
175164
struct UvTcp *t = connect->t;
176165
int rv;
177166

178-
if (t->closing || status == UV_ECANCELED) {
167+
connect->resolving =
168+
false; /* Indicate we are in the name resolving phase */
169+
170+
if (t->closing) {
179171
connect->status = RAFT_CANCELED;
172+
173+
/* We need to close the tcp handle to to connection attempt */
174+
uv_close((struct uv_handle_s *)connect->tcp, uvTcpConnectUvCloseCb);
180175
return;
181176
}
182177

@@ -233,9 +228,6 @@ static int uvTcpConnectStart(struct uvTcpConnect *r, const char *address)
233228
goto err;
234229
}
235230

236-
rv = uv_check_init(t->loop, &r->delayedtcpclose);
237-
assert(rv == 0);
238-
239231
rv = uv_tcp_init(r->t->loop, r->tcp);
240232
assert(rv == 0);
241233
r->tcp->data = r;
@@ -258,12 +250,12 @@ static int uvTcpConnectStart(struct uvTcpConnect *r, const char *address)
258250
rv = RAFT_NOCONNECTION;
259251
goto err_after_tcp_init;
260252
}
253+
r->resolving = true; /* Indicate we are in the name resolving phase */
261254

262255
return 0;
263256

264257
err_after_tcp_init:
265258
uv_close((uv_handle_t *)r->tcp, (uv_close_cb)RaftHeapFree);
266-
uv_close((uv_handle_t *)&r->delayedtcpclose, NULL);
267259

268260
err:
269261
RaftHeapFree(r->handshake.base);
@@ -295,8 +287,8 @@ int UvTcpConnect(struct raft_uv_transport *transport,
295287
r->status = 0;
296288
r->write.data = r;
297289
r->getaddrinfo.data = r;
290+
r->resolving = false;
298291
r->connect.data = r;
299-
r->delayedtcpclose.data = r;
300292
req->cb = cb;
301293

302294
/* Keep track of the pending request */

0 commit comments

Comments
 (0)