Skip to content

Potential deadlock, with the unreleased once_init_lock #1183

@jenny-cheung

Description

@jenny-cheung

Dear developers:
I found the lock may be unreleased after the function return. Thank you for your checking!

pthread_mutex_lock(&once_init_lock);

int
evthread_use_pthreads_with_flags(int flags)
{
        ...;

	pthread_mutex_lock(&once_init_lock); //HERE
	if (once_init == 1) {
		pthread_mutex_unlock(&once_init_lock);
		return 0;
	}

	if (pthread_mutexattr_init(&attr_default)) 
		goto error;

	/* Set ourselves up to get recursive locks. */
	if (pthread_mutexattr_init(&attr_recursive))
		goto error;
	if (pthread_mutexattr_settype(&attr_recursive, PTHREAD_MUTEX_RECURSIVE))
		goto error;

	if (flags & EVTHREAD_PTHREAD_PRIO_INHERIT) {
#ifdef EVENT__HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
		/* Set up priority inheritance */
		if (pthread_mutexattr_setprotocol(&attr_default, PTHREAD_PRIO_INHERIT))
			goto error;
		if (pthread_mutexattr_setprotocol(&attr_recursive, PTHREAD_PRIO_INHERIT))
			goto error;
#else
		goto error;
#endif
	}

	evthread_set_lock_callbacks(&cbs);
	evthread_set_condition_callbacks(&cond_cbs);
	evthread_set_id_callback(evthread_posix_get_id);
	once_init = 1;

	return 0;             // the lock onece_init_lock may be unreleased
error:
	pthread_mutex_unlock(&once_init_lock);
	return -1;
}


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions