Commits

Tim van der Molen committed 04230c5

Prevent a potential mutex deadlock.

The deadlock occurs when screen_prompt_end() locks screen_curses_mtx and then
attempts to lock option_tree_mtx while at the same time player_print_status()
or player_print_track() attempts to lock them in reverse order.

The deadlock is reproducible by repeatedly opening and closing a prompt while
playing back a track until the timing is right. The best chance of success is
with Linux and the libao null driver.

It seems the best solution is to ensure the screen.c functions do not attempt
to hold these mutexes simultaneously.

  • Participants
  • Parent commits 63d3b5b

Comments (0)

Files changed (1)

 	int	 attr;
 	chtype	 cattr;
 
-	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
 	for (i = 0; i < NELEMENTS(screen_objects); i++) {
 		attr = option_get_attrib(screen_objects[i].option_attr);
 		cattr = A_NORMAL;
 			if (attr & screen_attribs[j].attrib)
 				cattr |= screen_attribs[j].curses_attrib;
 
+		XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
 		screen_objects[i].attr = cattr;
+		XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
 	}
-	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
 }
 
 static void
 	if (!screen_have_colours)
 		return;
 
-	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
 	for (i = 0; i < NELEMENTS(screen_objects); i++) {
 		bg = screen_get_colour(screen_objects[i].option_bg,
 		    COLOUR_BLACK);
 		fg = screen_get_colour(screen_objects[i].option_fg,
 		    COLOUR_WHITE);
 
+		XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
 		if (init_pair(screen_objects[i].colour_pair, fg, bg) == OK)
 			screen_objects[i].attr |=
 			    (chtype)COLOR_PAIR(screen_objects[i].colour_pair);
+		XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
 	}
-	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
 }
 
 void
 screen_configure_cursor(void)
 {
+	int show;
+
+	show = option_get_boolean("show-cursor");
 	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
-	if (option_get_boolean("show-cursor"))
-		(void)curs_set(1);
-	else
-		(void)curs_set(0);
-
+	(void)curs_set(show);
 #ifdef SCREEN_HAVE_NETBSD_CURSES_BUGS
 	/*
 	 * NetBSD's curs_set() does not let the cursor-visibility change take
 void
 screen_prompt_end(void)
 {
+	int show;
+
+	show = option_get_boolean("show-cursor");
 	screen_status_clear();
 	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
-	if (!option_get_boolean("show-cursor"))
+	if (!show)
 		(void)curs_set(0);
 	(void)move(screen_view_selected_row, 0);
 	(void)refresh();