Skip to contentSkip to conversation

lib/cobalt: Fix a deadlock when release_buffer() attempts to re-acquire buffer_lock

PATCH Superseded Ethan 2 participants 5 messages Mar 29, 2025 – Mar 30, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ethan

author
#0 lore raw

PATCH 1/1 lib/cobalt: Fix a deadlock when release_buffer() attempts to re-acquire buffer_lock

The release_buffer() function is the destructor function for the thread-specific data (TSD) associated with buffer_key. If the current thread is executing rt_print_flush_buffers() and has already acquired buffer_lock, and is then requested to exit via pthread_cancel(), the release_buffer() function will be invoked. A deadlock can occur when release_buffer() attempts to re-acquire buffer_lock.

This patch changes the buffer_lock from PTHREAD_MUTEX_DEFAULT to PTHREAD_MUTEX_ERRORCHECK type, as it supports deadlock detection. If the lock is successfully acquired, it will return 0. If the same thread attempts to re-lock a mutex that it has already locked, it will return EDEADLK. This approach prevents the deadlock issue.

The deadlock can be observed as gdb shown. The thread "slave" will block when calling pthread_mutex_lock() in release_buffer() while attempting to re-acquire buffer_lock again. This also causes cobalt_print to remain waiting indefinitely for buffer_lock.

(gdb) info thread

  Id   Target Id                                        Frame
* 1    Thread 0x7ff7ff4010 (LWP 1654) "hello"           0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78
  2    Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf"   __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
    at lowlevellock.c:52
  3    Thread 0x7ff72791c0 (LWP 1658) "master"          0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
    f=<optimized out>) at genops.c:393

4 Thread 0x7ff72581c0 (LWP 1659) "slave" (Exiting) __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread 0x7ff72581c0 (LWP 1659))]
52 lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2

---
 lib/cobalt/printf.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
lib/cobalt/printf.c +11 -2
index 0aa5940c6..87ed989f4 100644
@@ -82,6 +82,7 @@ static struct timespec syncdelay;
 static pthread_mutex_t buffer_lock;
 static pthread_cond_t printer_wakeup;
 static pthread_key_t buffer_key;
+static pthread_mutexattr_t attr;
 static pthread_key_t cleanup_key;
 static pthread_t printer_thread;
 static atomic_long_t *pool_bitmap;
@@ -498,6 +499,12 @@ static void release_buffer(struct print_buffer *buffer)
 
 	pthread_setspecific(buffer_key, NULL);
 
+	/* The type of buffer_lock is PTHREAD_MUTEX_ERRORCHECK, which supports
+	 * deadlock detection. If the lock is successfully acquired, it will return
+	 * 0; If the same thread attempts to re-lock a mutex that it has already
+	 * locked, it will return EDEADLK. This approach prevents the deadlock
+	 * issue.
+	 */
 	pthread_mutex_lock(&buffer_lock);
 
 	print_buffers();
@@ -666,7 +673,7 @@ void cobalt_print_init_atfork(void)
 	}
 
 	/* re-init to avoid finding it locked by some parent thread */
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutex_init(&buffer_lock, &attr);
 
 	while (*pbuffer) {
 		if (*pbuffer == my_buffer)
@@ -723,7 +730,9 @@ void cobalt_print_init(void)
 		rt_print_init_inner(buffer, __cobalt_print_bufsz);
 	}
 done:
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+	pthread_mutex_init(&buffer_lock, &attr);
 	pthread_key_create(&buffer_key, (void (*)(void*))release_buffer);
 	pthread_key_create(&cleanup_key, do_cleanup);
 	pthread_cond_init(&printer_wakeup, NULL);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 76 lines · Ethan

The release_buffer() function is the destructor function for the thread-specific data (TSD) associated with buffer_key. If the current thread is executing rt_print_flush_buffers() and has already acquired buffer_lock, and is then requested to exit via pthread_cancel(), the release_buffer() function will be invoked. A deadlock can occur when release_buffer() attempts to re-acquire buffer_lock.

This patch changes the buffer_lock from PTHREAD_MUTEX_DEFAULT to PTHREAD_MUTEX_ERRORCHECK type, as it supports deadlock detection. If the lock is successfully acquired, it will return 0. If the same thread attempts to re-lock a mutex that it has already locked, it will return EDEADLK. This approach prevents the deadlock issue.

The deadlock can be observed as gdb shown. The thread "slave" will block when calling pthread_mutex_lock() in release_buffer() while attempting to re-acquire buffer_lock again. This also causes cobalt_print to remain waiting indefinitely for buffer_lock.

(gdb) info thread

  Id   Target Id                                        Frame
* 1    Thread 0x7ff7ff4010 (LWP 1654) "hello"           0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78
  2    Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf"   __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
    at lowlevellock.c:52
  3    Thread 0x7ff72791c0 (LWP 1658) "master"          0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
    f=<optimized out>) at genops.c:393

4 Thread 0x7ff72581c0 (LWP 1659) "slave" (Exiting) __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread 0x7ff72581c0 (LWP 1659))]
52 lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2

---
 lib/cobalt/printf.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
lib/cobalt/printf.c +8 -1
index 0aa5940c6..87ed989f4 100644
@@ -82,6 +82,7 @@ static struct timespec syncdelay;
 static pthread_mutex_t buffer_lock;
 static pthread_cond_t printer_wakeup;
 static pthread_key_t buffer_key;
+static pthread_mutexattr_t attr;
 static pthread_key_t cleanup_key;
 static pthread_t printer_thread;
 static atomic_long_t *pool_bitmap;
@@ -498,6 +499,12 @@ static void release_buffer(struct print_buffer *buffer)
 
 	pthread_setspecific(buffer_key, NULL);
 
+	/* The type of buffer_lock is PTHREAD_MUTEX_ERRORCHECK, which supports
+	 * deadlock detection. If the lock is successfully acquired, it will return
+	 * 0; If the same thread attempts to re-lock a mutex that it has already
+	 * locked, it will return EDEADLK. This approach prevents the deadlock
+	 * issue.
+	 */
 	pthread_mutex_lock(&buffer_lock);
 
 	print_buffers();
@@ -666,7 +673,7 @@ void cobalt_print_init_atfork(void)
 	}
 
 	/* re-init to avoid finding it locked by some parent thread */
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutex_init(&buffer_lock, &attr);
 
 	while (*pbuffer) {
 		if (*pbuffer == my_buffer)

lib/cobalt/printf.c void cobalt_print_init(void) · line 734

Quoted 6 lines
lib/cobalt/printf.c +2 -1
@@ -723,7 +730,9 @@ void cobalt_print_init(void)
 		rt_print_init_inner(buffer, __cobalt_print_bufsz);
 	}
 done:
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

Good catch! But why not using PTHREAD_MUTEX_RECURSIVE, rather then triggering an error that is not handled and therefore happens to let the caller continue?

Jan

Quoted 4 lines
lib/cobalt/printf.c +1 -0
+	pthread_mutex_init(&buffer_lock, &attr);
 	pthread_key_create(&buffer_key, (void (*)(void*))release_buffer);
 	pthread_key_create(&cleanup_key, do_cleanup);
 	pthread_cond_init(&printer_wakeup, NULL);
#2 lore raw
Quoted 74 lines · Jan Kiszka
Quoted 73 lines · Ethan

The release_buffer() function is the destructor function for the thread-specific data (TSD) associated with buffer_key. If the current thread is executing rt_print_flush_buffers() and has already acquired buffer_lock, and is then requested to exit via pthread_cancel(), the release_buffer() function will be invoked. A deadlock can occur when release_buffer() attempts to re-acquire buffer_lock.

This patch changes the buffer_lock from PTHREAD_MUTEX_DEFAULT to PTHREAD_MUTEX_ERRORCHECK type, as it supports deadlock detection. If the lock is successfully acquired, it will return 0. If the same thread attempts to re-lock a mutex that it has already locked, it will return EDEADLK. This approach prevents the deadlock issue.

The deadlock can be observed as gdb shown. The thread "slave" will block when calling pthread_mutex_lock() in release_buffer() while attempting to re-acquire buffer_lock again. This also causes cobalt_print to remain waiting indefinitely for buffer_lock.

(gdb) info thread

  Id   Target Id                                        Frame
* 1    Thread 0x7ff7ff4010 (LWP 1654) "hello"           0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78
  2    Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf"   __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
    at lowlevellock.c:52
  3    Thread 0x7ff72791c0 (LWP 1658) "master"          0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
    f=<optimized out>) at genops.c:393

4 Thread 0x7ff72581c0 (LWP 1659) "slave" (Exiting) __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread 0x7ff72581c0 (LWP 1659))]
52 lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2

---
 lib/cobalt/printf.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
lib/cobalt/printf.c +8 -1
index 0aa5940c6..87ed989f4 100644
@@ -82,6 +82,7 @@ static struct timespec syncdelay;
 static pthread_mutex_t buffer_lock;
 static pthread_cond_t printer_wakeup;
 static pthread_key_t buffer_key;
+static pthread_mutexattr_t attr;
 static pthread_key_t cleanup_key;
 static pthread_t printer_thread;
 static atomic_long_t *pool_bitmap;
@@ -498,6 +499,12 @@ static void release_buffer(struct print_buffer *buffer)
 
 	pthread_setspecific(buffer_key, NULL);
 
+	/* The type of buffer_lock is PTHREAD_MUTEX_ERRORCHECK, which supports
+	 * deadlock detection. If the lock is successfully acquired, it will return
+	 * 0; If the same thread attempts to re-lock a mutex that it has already
+	 * locked, it will return EDEADLK. This approach prevents the deadlock
+	 * issue.
+	 */
 	pthread_mutex_lock(&buffer_lock);
 
 	print_buffers();
@@ -666,7 +673,7 @@ void cobalt_print_init_atfork(void)
 	}
 
 	/* re-init to avoid finding it locked by some parent thread */
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutex_init(&buffer_lock, &attr);
 
Quoted 14 lines

lib/cobalt/printf.c void cobalt_print_init(void) · line 734

Quoted 9 lines
lib/cobalt/printf.c +2 -1
 	while (*pbuffer) {
 		if (*pbuffer == my_buffer)
@@ -723,7 +730,9 @@ void cobalt_print_init(void)
 		rt_print_init_inner(buffer, __cobalt_print_bufsz);
 	}
 done:
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

Good catch! But why not using PTHREAD_MUTEX_RECURSIVE, rather then triggering an error that is not handled and therefore happens to let the caller continue?

In fact, this would need more work because we would also have to ensure that we can actually re-enter the locked sections. It is therefore better to disable cancellation while running release_buffer.

Jan

author
#3 lore raw
Quoted 90 lines · Jan Kiszka
Quoted 74 lines · Jan Kiszka
Quoted 73 lines · Ethan

The release_buffer() function is the destructor function for the thread-specific data (TSD) associated with buffer_key. If the current thread is executing rt_print_flush_buffers() and has already acquired buffer_lock, and is then requested to exit via pthread_cancel(), the release_buffer() function will be invoked. A deadlock can occur when release_buffer() attempts to re-acquire buffer_lock.

This patch changes the buffer_lock from PTHREAD_MUTEX_DEFAULT to PTHREAD_MUTEX_ERRORCHECK type, as it supports deadlock detection. If the lock is successfully acquired, it will return 0. If the same thread attempts to re-lock a mutex that it has already locked, it will return EDEADLK. This approach prevents the deadlock issue.

The deadlock can be observed as gdb shown. The thread "slave" will block when calling pthread_mutex_lock() in release_buffer() while attempting to re-acquire buffer_lock again. This also causes cobalt_print to remain waiting indefinitely for buffer_lock.

(gdb) info thread

  Id   Target Id                                        Frame
* 1    Thread 0x7ff7ff4010 (LWP 1654) "hello"           0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78
  2    Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf"   __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
    at lowlevellock.c:52
  3    Thread 0x7ff72791c0 (LWP 1658) "master"          0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
    f=<optimized out>) at genops.c:393

4 Thread 0x7ff72581c0 (LWP 1659) "slave" (Exiting) __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread 0x7ff72581c0 (LWP 1659))]
52 lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2

---
 lib/cobalt/printf.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
lib/cobalt/printf.c +8 -1
index 0aa5940c6..87ed989f4 100644
@@ -82,6 +82,7 @@ static struct timespec syncdelay;
 static pthread_mutex_t buffer_lock;
 static pthread_cond_t printer_wakeup;
 static pthread_key_t buffer_key;
+static pthread_mutexattr_t attr;
 static pthread_key_t cleanup_key;
 static pthread_t printer_thread;
 static atomic_long_t *pool_bitmap;
@@ -498,6 +499,12 @@ static void release_buffer(struct print_buffer *buffer)
 
 	pthread_setspecific(buffer_key, NULL);
 
+	/* The type of buffer_lock is PTHREAD_MUTEX_ERRORCHECK, which supports
+	 * deadlock detection. If the lock is successfully acquired, it will return
+	 * 0; If the same thread attempts to re-lock a mutex that it has already
+	 * locked, it will return EDEADLK. This approach prevents the deadlock
+	 * issue.
+	 */
 	pthread_mutex_lock(&buffer_lock);
 
 	print_buffers();
@@ -666,7 +673,7 @@ void cobalt_print_init_atfork(void)
 	}
 
 	/* re-init to avoid finding it locked by some parent thread */
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutex_init(&buffer_lock, &attr);
 
Quoted 14 lines

lib/cobalt/printf.c void cobalt_print_init(void) · line 734

Quoted 9 lines
lib/cobalt/printf.c +2 -1
 	while (*pbuffer) {
 		if (*pbuffer == my_buffer)
@@ -723,7 +730,9 @@ void cobalt_print_init(void)
 		rt_print_init_inner(buffer, __cobalt_print_bufsz);
 	}
 done:
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

Good catch! But why not using PTHREAD_MUTEX_RECURSIVE, rather then triggering an error that is not handled and therefore happens to let the caller continue?

Quoted 5 lines

In fact, this would need more work because we would also have to ensure that we can actually re-enter the locked sections. It is therefore better to disable cancellation while running release_buffer.

Jan

1. PTHREAD_MUTEX_RECURSIVE
It is difficult to detect how many times a PTHREAD_MUTEX_RECURSIVE lock has been locked. Thus, it is impossible to determine how many times to call pthread_mutex_unlock to release it.

2. PTHREAD_MUTEX_ERRORCHECK
I was thinking that PTHREAD_MUTEX_ERRORCHECK can inherently detect whether the lock has already been acquired by the same thread. This is an existing feature that, to some extent, is very suitable for preventing deadlocks caused by a thread attempting to acquire the same mutex multiple times.

3. To disable cancellation,how about this way:

lib/cobalt/printf.c +2 -0
index 87ed989f4..403829a2f 100644
@@ -484,9 +484,11 @@ const char *rt_print_buffer_name(void)
 void rt_print_flush_buffers(void)
 {
        cobalt_thread_relax();
+       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
        pthread_mutex_lock(&buffer_lock);
        print_buffers();
        pthread_mutex_unlock(&buffer_lock);
+       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
 }
 
 static void release_buffer(struct print_buffer *buffer)

I havn't verified if PTHREAD_CANCEL_DISABLE could fix this issue. I need more time.

Ethan

#4 lore raw
Quoted 96 lines · aspirestro
Quoted 94 lines · Jan Kiszka
Quoted 74 lines · Jan Kiszka
Quoted 73 lines · Ethan

The release_buffer() function is the destructor function for the thread-specific data (TSD) associated with buffer_key. If the current thread is executing rt_print_flush_buffers() and has already acquired buffer_lock, and is then requested to exit via pthread_cancel(), the release_buffer() function will be invoked. A deadlock can occur when release_buffer() attempts to re-acquire buffer_lock.

This patch changes the buffer_lock from PTHREAD_MUTEX_DEFAULT to PTHREAD_MUTEX_ERRORCHECK type, as it supports deadlock detection. If the lock is successfully acquired, it will return 0. If the same thread attempts to re-lock a mutex that it has already locked, it will return EDEADLK. This approach prevents the deadlock issue.

The deadlock can be observed as gdb shown. The thread "slave" will block when calling pthread_mutex_lock() in release_buffer() while attempting to re-acquire buffer_lock again. This also causes cobalt_print to remain waiting indefinitely for buffer_lock.

(gdb) info thread

  Id   Target Id                                        Frame
* 1    Thread 0x7ff7ff4010 (LWP 1654) "hello"           0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78
  2    Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf"   __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
    at lowlevellock.c:52
  3    Thread 0x7ff72791c0 (LWP 1658) "master"          0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
    f=<optimized out>) at genops.c:393

4 Thread 0x7ff72581c0 (LWP 1659) "slave" (Exiting) __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread 0x7ff72581c0 (LWP 1659))]
52 lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2

---
 lib/cobalt/printf.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
lib/cobalt/printf.c +8 -1
index 0aa5940c6..87ed989f4 100644
@@ -82,6 +82,7 @@ static struct timespec syncdelay;
 static pthread_mutex_t buffer_lock;
 static pthread_cond_t printer_wakeup;
 static pthread_key_t buffer_key;
+static pthread_mutexattr_t attr;
 static pthread_key_t cleanup_key;
 static pthread_t printer_thread;
 static atomic_long_t *pool_bitmap;
@@ -498,6 +499,12 @@ static void release_buffer(struct print_buffer *buffer)
 
 	pthread_setspecific(buffer_key, NULL);
 
+	/* The type of buffer_lock is PTHREAD_MUTEX_ERRORCHECK, which supports
+	 * deadlock detection. If the lock is successfully acquired, it will return
+	 * 0; If the same thread attempts to re-lock a mutex that it has already
+	 * locked, it will return EDEADLK. This approach prevents the deadlock
+	 * issue.
+	 */
 	pthread_mutex_lock(&buffer_lock);
 
 	print_buffers();
@@ -666,7 +673,7 @@ void cobalt_print_init_atfork(void)
 	}
 
 	/* re-init to avoid finding it locked by some parent thread */
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutex_init(&buffer_lock, &attr);
 
Quoted 14 lines

lib/cobalt/printf.c void cobalt_print_init(void) · line 734

Quoted 9 lines
lib/cobalt/printf.c +2 -1
 	while (*pbuffer) {
 		if (*pbuffer == my_buffer)
@@ -723,7 +730,9 @@ void cobalt_print_init(void)
 		rt_print_init_inner(buffer, __cobalt_print_bufsz);
 	}
 done:
-	pthread_mutex_init(&buffer_lock, NULL);
+	pthread_mutexattr_init(&attr);
+	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);

Good catch! But why not using PTHREAD_MUTEX_RECURSIVE, rather then triggering an error that is not handled and therefore happens to let the caller continue?

In fact, this would need more work because we would also have to ensure that we can actually re-enter the locked sections. It is therefore better to disable cancellation while running release_buffer.

Quoted 6 lines
Quoted 1 line

Jan

1. PTHREAD_MUTEX_RECURSIVE
It is difficult to detect how many times a PTHREAD_MUTEX_RECURSIVE lock has been locked. Thus, it is impossible to determine how many times to call pthread_mutex_unlock to release it.

Indeed.

Quoted 6 lines

2. PTHREAD_MUTEX_ERRORCHECK
I was thinking that PTHREAD_MUTEX_ERRORCHECK can inherently detect whether the lock has already been acquired by the same thread. This is an existing feature that, to some extent, is very suitable for preventing deadlocks caused by a thread attempting to acquire the same mutex multiple times.

Detection would work, but we may also leave the state that the lock is suppose to protect half-backed behind when we re-run logic under the lock.

Quoted 8 lines

3. To disable cancellation,how about this way:

lib/cobalt/printf.c +0 -0
index 87ed989f4..403829a2f 100644

lib/cobalt/printf.c const char *rt_print_buffer_name(void) · line 491

Quoted 8 lines
lib/cobalt/printf.c +2 -0
@@ -484,9 +484,11 @@ const char *rt_print_buffer_name(void)
 void rt_print_flush_buffers(void)
 {
        cobalt_thread_relax();
+       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
        pthread_mutex_lock(&buffer_lock);
        print_buffers();
        pthread_mutex_unlock(&buffer_lock);
+       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

We likely want to save/restore the cancellation state here.

Jan

Quoted 8 lines
lib/cobalt/printf.c +0 -0
 }
 
 static void release_buffer(struct print_buffer *buffer)

I havn't verified if PTHREAD_CANCEL_DISABLE could fix this issue. I need more time.

Ethan