@@ -32,9 +32,9 @@ struct uvTcpConnect
32
32
struct uv_getaddrinfo_s getaddrinfo ; /* DNS resolve request */
33
33
struct uv_connect_s connect ; /* TCP connection request */
34
34
struct uv_write_s write ; /* TCP handshake request */
35
- uv_check_t delayedtcpclose ; /* A check handle required to delay closing tcp */
36
35
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 */
38
38
};
39
39
40
40
/* Encode an handshake message into the given buffer. */
@@ -66,7 +66,6 @@ static void uvTcpConnectFinish(struct uvTcpConnect *connect)
66
66
struct raft_uv_connect * req = connect -> req ;
67
67
int status = connect -> status ;
68
68
QUEUE_REMOVE (& connect -> queue );
69
- uv_close ((struct uv_handle_s * )& connect -> delayedtcpclose , NULL );
70
69
RaftHeapFree (connect -> handshake .base );
71
70
uv_freeaddrinfo (connect -> getaddrinfo .addrinfo );
72
71
raft_free (connect );
@@ -87,27 +86,16 @@ static void uvTcpConnectUvCloseCb(struct uv_handle_s *handle)
87
86
UvTcpMaybeFireCloseCb (t );
88
87
}
89
88
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
-
99
89
/* Abort a connection request. */
100
90
static void uvTcpConnectAbort (struct uvTcpConnect * connect )
101
91
{
102
92
QUEUE_REMOVE (& connect -> queue );
103
93
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
+ /* Only call uv_close in 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 ) {
111
99
uv_close ((struct uv_handle_s * )connect -> tcp , uvTcpConnectUvCloseCb );
112
100
}
113
101
}
@@ -167,6 +155,7 @@ static void uvTcpConnectUvConnectCb(struct uv_connect_s *req, int status)
167
155
uvTcpConnectAbort (connect );
168
156
}
169
157
158
+ /* The hostname resolve is finished */
170
159
static void uvGetAddrInfoCb (uv_getaddrinfo_t * req ,
171
160
int status ,
172
161
struct addrinfo * res )
@@ -175,8 +164,14 @@ static void uvGetAddrInfoCb(uv_getaddrinfo_t *req,
175
164
struct UvTcp * t = connect -> t ;
176
165
int rv ;
177
166
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 ) {
179
171
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 );
180
175
return ;
181
176
}
182
177
@@ -233,9 +228,6 @@ static int uvTcpConnectStart(struct uvTcpConnect *r, const char *address)
233
228
goto err ;
234
229
}
235
230
236
- rv = uv_check_init (t -> loop , & r -> delayedtcpclose );
237
- assert (rv == 0 );
238
-
239
231
rv = uv_tcp_init (r -> t -> loop , r -> tcp );
240
232
assert (rv == 0 );
241
233
r -> tcp -> data = r ;
@@ -258,12 +250,12 @@ static int uvTcpConnectStart(struct uvTcpConnect *r, const char *address)
258
250
rv = RAFT_NOCONNECTION ;
259
251
goto err_after_tcp_init ;
260
252
}
253
+ r -> resolving = true; /* INdicate we are in the name resolving phase */
261
254
262
255
return 0 ;
263
256
264
257
err_after_tcp_init :
265
258
uv_close ((uv_handle_t * )r -> tcp , (uv_close_cb )RaftHeapFree );
266
- uv_close ((uv_handle_t * )& r -> delayedtcpclose , NULL );
267
259
268
260
err :
269
261
RaftHeapFree (r -> handshake .base );
@@ -295,8 +287,8 @@ int UvTcpConnect(struct raft_uv_transport *transport,
295
287
r -> status = 0 ;
296
288
r -> write .data = r ;
297
289
r -> getaddrinfo .data = r ;
290
+ r -> resolving = false;
298
291
r -> connect .data = r ;
299
- r -> delayedtcpclose .data = r ;
300
292
req -> cb = cb ;
301
293
302
294
/* Keep track of the pending request */
0 commit comments