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

Commit

Permalink
unix: don't abort loop initalization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LinuxJedi committed Oct 9, 2014
1 parent 471e844 commit 0b2b66e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
24 changes: 18 additions & 6 deletions src/unix/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,26 @@ static int uv__loop_init(uv_loop_t* loop, int default_loop) {
loop->child_watcher.flags |= UV__HANDLE_INTERNAL;
QUEUE_INIT(&loop->process_handles);

if (uv_rwlock_init(&loop->cloexec_lock))
abort();
err = uv_rwlock_init(&loop->cloexec_lock);
if (err) {
uv__platform_loop_delete(loop);
return err;
}

if (uv_mutex_init(&loop->wq_mutex))
abort();
err = uv_mutex_init(&loop->wq_mutex);
if (err) {
uv__platform_loop_delete(loop);
uv_rwlock_destroy(&loop->cloexec_lock);
return err;
}

if (uv_async_init(loop, &loop->wq_async, uv__work_done))
abort();
err = uv_async_init(loop, &loop->wq_async, uv__work_done);
if (err) {
uv__platform_loop_delete(loop);
uv_rwlock_destroy(&loop->cloexec_lock);
uv_mutex_destroy(&loop->wq_mutex);
return err;
}

uv__handle_unref(&loop->wq_async);
loop->wq_async.flags |= UV__HANDLE_INTERNAL;
Expand Down
20 changes: 14 additions & 6 deletions src/unix/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,24 @@ int uv_mutex_init(uv_mutex_t* mutex) {
pthread_mutexattr_t attr;
int err;

if (pthread_mutexattr_init(&attr))
abort();
err = pthread_mutexattr_init(&attr);
if (err) {
return -err;
}

if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK))
abort();
err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (err) {
phread_mutexattr_destroy(&attr);
return -err;
}

err = pthread_mutex_init(mutex, &attr);
if (err) {
phread_mutexattr_destroy(&attr);
return -err;
}

if (pthread_mutexattr_destroy(&attr))
abort();
err = pthread_mutexattr_destroy(&attr);

return -err;
#endif
Expand Down

0 comments on commit 0b2b66e

Please sign in to comment.