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

Commit 0b2b66e

Browse files
committed
unix: don't abort loop initalization
There are several conditions which could cause uv_loop_init() (and by extension uv_loop_new()) to trigger an SIGABRT. This includes mutex initialization failure or running out of FDs. This patch removes the abort()s and returns error codes instead. Cleaning up prior initialization if something has actually failed.
1 parent 471e844 commit 0b2b66e

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/unix/loop.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,26 @@ static int uv__loop_init(uv_loop_t* loop, int default_loop) {
139139
loop->child_watcher.flags |= UV__HANDLE_INTERNAL;
140140
QUEUE_INIT(&loop->process_handles);
141141

142-
if (uv_rwlock_init(&loop->cloexec_lock))
143-
abort();
142+
err = uv_rwlock_init(&loop->cloexec_lock);
143+
if (err) {
144+
uv__platform_loop_delete(loop);
145+
return err;
146+
}
144147

145-
if (uv_mutex_init(&loop->wq_mutex))
146-
abort();
148+
err = uv_mutex_init(&loop->wq_mutex);
149+
if (err) {
150+
uv__platform_loop_delete(loop);
151+
uv_rwlock_destroy(&loop->cloexec_lock);
152+
return err;
153+
}
147154

148-
if (uv_async_init(loop, &loop->wq_async, uv__work_done))
149-
abort();
155+
err = uv_async_init(loop, &loop->wq_async, uv__work_done);
156+
if (err) {
157+
uv__platform_loop_delete(loop);
158+
uv_rwlock_destroy(&loop->cloexec_lock);
159+
uv_mutex_destroy(&loop->wq_mutex);
160+
return err;
161+
}
150162

151163
uv__handle_unref(&loop->wq_async);
152164
loop->wq_async.flags |= UV__HANDLE_INTERNAL;

src/unix/thread.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,24 @@ int uv_mutex_init(uv_mutex_t* mutex) {
4343
pthread_mutexattr_t attr;
4444
int err;
4545

46-
if (pthread_mutexattr_init(&attr))
47-
abort();
46+
err = pthread_mutexattr_init(&attr);
47+
if (err) {
48+
return -err;
49+
}
4850

49-
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK))
50-
abort();
51+
err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
52+
if (err) {
53+
phread_mutexattr_destroy(&attr);
54+
return -err;
55+
}
5156

5257
err = pthread_mutex_init(mutex, &attr);
58+
if (err) {
59+
phread_mutexattr_destroy(&attr);
60+
return -err;
61+
}
5362

54-
if (pthread_mutexattr_destroy(&attr))
55-
abort();
63+
err = pthread_mutexattr_destroy(&attr);
5664

5765
return -err;
5866
#endif

0 commit comments

Comments
 (0)