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

Commit 133b9d1

Browse files
committed
unix: allow uv_getnameinfo to be cancelled
1 parent 6ffb82e commit 133b9d1

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/unix/getnameinfo.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ static void uv__getnameinfo_done(struct uv__work* w, int status) {
5959

6060
req = container_of(w, uv_getnameinfo_t, work_req);
6161
uv__req_unregister(req->loop, req);
62+
host = service = NULL;
6263

63-
if (req->retcode == 0) {
64+
if (status == -ECANCELED) {
65+
assert(req->retcode == 0);
66+
req->retcode = UV_EAI_CANCELED;
67+
} else if (req->retcode == 0) {
6468
host = req->host;
6569
service = req->service;
66-
} else {
67-
host = NULL;
68-
service = NULL;
6970
}
7071

7172
req->getnameinfo_cb(req, req->retcode, host, service);
@@ -102,6 +103,7 @@ int uv_getnameinfo(uv_loop_t* loop,
102103
req->flags = flags;
103104
req->type = UV_GETNAMEINFO;
104105
req->loop = loop;
106+
req->retcode = 0;
105107

106108
uv__work_submit(loop,
107109
&req->work_req,

test/test-list.h

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ TEST_DECLARE (threadpool_queue_work_simple)
229229
TEST_DECLARE (threadpool_queue_work_einval)
230230
TEST_DECLARE (threadpool_multiple_event_loops)
231231
TEST_DECLARE (threadpool_cancel_getaddrinfo)
232+
TEST_DECLARE (threadpool_cancel_getnameinfo)
232233
TEST_DECLARE (threadpool_cancel_work)
233234
TEST_DECLARE (threadpool_cancel_fs)
234235
TEST_DECLARE (threadpool_cancel_single)
@@ -573,6 +574,7 @@ TASK_LIST_START
573574
TEST_ENTRY (threadpool_queue_work_einval)
574575
TEST_ENTRY (threadpool_multiple_event_loops)
575576
TEST_ENTRY (threadpool_cancel_getaddrinfo)
577+
TEST_ENTRY (threadpool_cancel_getnameinfo)
576578
TEST_ENTRY (threadpool_cancel_work)
577579
TEST_ENTRY (threadpool_cancel_fs)
578580
TEST_ENTRY (threadpool_cancel_single)

test/test-threadpool-cancel.c

+48-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ static unsigned work_cb_called;
4646
static unsigned done_cb_called;
4747
static unsigned done2_cb_called;
4848
static unsigned timer_cb_called;
49-
static unsigned getaddrinfo_cb_called;
5049

5150

5251
static void work_cb(uv_work_t* req) {
@@ -125,7 +124,16 @@ static void getaddrinfo_cb(uv_getaddrinfo_t* req,
125124
ASSERT(status == UV_EAI_CANCELED);
126125
ASSERT(res == NULL);
127126
uv_freeaddrinfo(res); /* Should not crash. */
128-
getaddrinfo_cb_called++;
127+
}
128+
129+
130+
static void getnameinfo_cb(uv_getnameinfo_t* handle,
131+
int status,
132+
char* hostname,
133+
char* service) {
134+
ASSERT(status == UV_EAI_CANCELED);
135+
ASSERT(hostname == NULL);
136+
ASSERT(service == NULL);
129137
}
130138

131139

@@ -202,6 +210,44 @@ TEST_IMPL(threadpool_cancel_getaddrinfo) {
202210
}
203211

204212

213+
TEST_IMPL(threadpool_cancel_getnameinfo) {
214+
uv_getnameinfo_t reqs[4];
215+
struct sockaddr_in addr4;
216+
struct cancel_info ci;
217+
uv_loop_t* loop;
218+
int r;
219+
220+
r = uv_ip4_addr("127.0.0.1", 80, &addr4);
221+
ASSERT(r == 0);
222+
223+
INIT_CANCEL_INFO(&ci, reqs);
224+
loop = uv_default_loop();
225+
saturate_threadpool();
226+
227+
r = uv_getnameinfo(loop, reqs + 0, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
228+
ASSERT(r == 0);
229+
230+
r = uv_getnameinfo(loop, reqs + 1, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
231+
ASSERT(r == 0);
232+
233+
r = uv_getnameinfo(loop, reqs + 2, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
234+
ASSERT(r == 0);
235+
236+
r = uv_getnameinfo(loop, reqs + 3, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
237+
ASSERT(r == 0);
238+
239+
ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
240+
ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
241+
ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
242+
ASSERT(1 == timer_cb_called);
243+
244+
cleanup_threadpool();
245+
246+
MAKE_VALGRIND_HAPPY();
247+
return 0;
248+
}
249+
250+
205251
TEST_IMPL(threadpool_cancel_work) {
206252
struct cancel_info ci;
207253
uv_work_t reqs[16];

0 commit comments

Comments
 (0)