Skip to contentSkip to conversation

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

PATCH Applied Haiyang He 2 participants 4 messages Mar 31, 2025 – Apr 2, 2025

lore
Share & tools
lore

Haiyang He

author
#0 lore raw

PATCH v3 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 uses the pthread_setcancelstate() function to set the thread state to PTHREAD_CANCEL_DISABLE, ensuring that the thread does not respond to cancellation requests while holding the buffer_lock.

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

#0  __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52
#1  0x0000007ff7f43cd8 in __GI___pthread_mutex_lock (mutex=0x7ff7fcaed8 <buffer_lock>) at pthread_mutex_lock.c:80
#2  0x0000007ff7fa3038 in release_buffer () from /usr/xenomai/lib/libcobalt.so.2
#3  0x0000007ff7f413c0 in __nptl_deallocate_tsd () at pthread_create.c:301
#4  0x0000007ff7f4156c in start_thread (arg=0x7ff7fa7430 <cobalt_thread_trampoline>) at pthread_create.c:488
#5  0x0000007ff7bcc49c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0

---

Patch v2:
- Use the pthread_setcancelstate() function to set the thread state
to PTHREAD_CANCEL_DISABLE.

Patch v3:
- Add backtrace output in gdb.

 lib/cobalt/printf.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/cobalt/printf.c +7 -0
index 0aa5940c6..bd2d7de56 100644
@@ -482,10 +482,17 @@ const char *rt_print_buffer_name(void)
 /* *** Deferred Output Management *** */
 void rt_print_flush_buffers(void)
 {
+	int old_state;
+
 	cobalt_thread_relax();
+
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
+
 	pthread_mutex_lock(&buffer_lock);
 	print_buffers();
 	pthread_mutex_unlock(&buffer_lock);
+
+	pthread_setcancelstate(old_state, NULL);
 }
 
 static void release_buffer(struct print_buffer *buffer)
Signature · 1 line
2.17.1
author
#1 lore raw
Quoted 66 lines · Haiyang He

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 uses the pthread_setcancelstate() function to set the thread state to PTHREAD_CANCEL_DISABLE, ensuring that the thread does not respond to cancellation requests while holding the buffer_lock.

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

#0  __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52
#1  0x0000007ff7f43cd8 in __GI___pthread_mutex_lock (mutex=0x7ff7fcaed8 <buffer_lock>) at pthread_mutex_lock.c:80
#2  0x0000007ff7fa3038 in release_buffer () from /usr/xenomai/lib/libcobalt.so.2
#3  0x0000007ff7f413c0 in __nptl_deallocate_tsd () at pthread_create.c:301
#4  0x0000007ff7f4156c in start_thread (arg=0x7ff7fa7430 <cobalt_thread_trampoline>) at pthread_create.c:488
#5  0x0000007ff7bcc49c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0

---

Patch v2:
- Use the pthread_setcancelstate() function to set the thread state to PTHREAD_CANCEL_DISABLE.

Patch v3:
- Add backtrace output in gdb.

lib/cobalt/printf.c | 7 +++++++

1 file changed, 7 insertions(+)
lib/cobalt/printf.c +2 -0
index 0aa5940c6..bd2d7de56 100644
@@ -482,10 +482,17 @@ const char *rt_print_buffer_name(void)
 /* *** Deferred Output Management *** */
 void rt_print_flush_buffers(void)
 {
+	int old_state;
+
 	cobalt_thread_relax();

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

Quoted 10 lines
lib/cobalt/printf.c +5 -0
+
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
+
 	pthread_mutex_lock(&buffer_lock);
 	print_buffers();
 	pthread_mutex_unlock(&buffer_lock);
+
+	pthread_setcancelstate(old_state, NULL);
 }
 

When I try to do more test, I find this:

When print_buffers() calls fwrite(), fwrite() checks whether the __flags2 of the

filestream stdout has the _IO_FLAGS2_NOTCANCEL bit set. By default, this bit is

not set, so __libc_enable_asynccancel is called. This causes PTHREAD_CANCEL_DISABLE

to become ineffective, resulting in the thread still exiting unexpectedly.

This is not the finally fix though. Any ideas? Shall we use write() instead of fwrite() inside print_buffers()?

Haiyang.

Quoted 3 lines
lib/cobalt/printf.c +0 -0
 static void release_buffer(struct print_buffer *buffer)
Signature · 1 line
2.17.1
author
#2 lore raw
Quoted 84 lines · Haiyang He
Quoted 66 lines · Haiyang He

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 uses the pthread_setcancelstate() function to set the thread state to PTHREAD_CANCEL_DISABLE, ensuring that the thread does not respond to cancellation requests while holding the buffer_lock.

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

#0  __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52
#1  0x0000007ff7f43cd8 in __GI___pthread_mutex_lock (mutex=0x7ff7fcaed8 <buffer_lock>) at pthread_mutex_lock.c:80
#2  0x0000007ff7fa3038 in release_buffer () from /usr/xenomai/lib/libcobalt.so.2
#3  0x0000007ff7f413c0 in __nptl_deallocate_tsd () at pthread_create.c:301
#4  0x0000007ff7f4156c in start_thread (arg=0x7ff7fa7430 <cobalt_thread_trampoline>) at pthread_create.c:488
#5  0x0000007ff7bcc49c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0

---

Patch v2:
- Use the pthread_setcancelstate() function to set the thread state to PTHREAD_CANCEL_DISABLE.

Patch v3:
- Add backtrace output in gdb.

lib/cobalt/printf.c | 7 +++++++

1 file changed, 7 insertions(+)
lib/cobalt/printf.c +2 -0
index 0aa5940c6..bd2d7de56 100644
@@ -482,10 +482,17 @@ const char *rt_print_buffer_name(void)
 /* *** Deferred Output Management *** */
 void rt_print_flush_buffers(void)
 {
+	int old_state;
+
 	cobalt_thread_relax();

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

Quoted 10 lines
lib/cobalt/printf.c +5 -0
+
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
+
 	pthread_mutex_lock(&buffer_lock);
 	print_buffers();
 	pthread_mutex_unlock(&buffer_lock);
+
+	pthread_setcancelstate(old_state, NULL);
 }
 

When I try to do more test, I find this:

When print_buffers() calls fwrite(), fwrite() checks whether the __flags2 of the

filestream stdout has the _IO_FLAGS2_NOTCANCEL bit set. By default, this bit is

Quoted 9 lines

not set, so __libc_enable_asynccancel is called. This causes PTHREAD_CANCEL_DISABLE

to become ineffective, resulting in the thread still exiting unexpectedly.

This is not the finally fix though. Any ideas? Shall we use write() instead of fwrite() inside print_buffers()?

Haiyang.

Please ignore above comments. Actually this test results caused by a wrong branch which I have not applied above patch v3.

In a word, this patch pass all my test.

Please have a review, thanks!

Haiyang.

Quoted 3 lines
Quoted 3 lines
lib/cobalt/printf.c +0 -0
 static void release_buffer(struct print_buffer *buffer)
Signature · 1 line
2.17.1
Applied
#3 lore raw
In reply to Haiyang He
Quoted 67 lines · Haiyang He

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 uses the pthread_setcancelstate() function to set the thread state to PTHREAD_CANCEL_DISABLE, ensuring that the thread does not respond to cancellation requests while holding the buffer_lock.

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

#0  __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52
#1  0x0000007ff7f43cd8 in __GI___pthread_mutex_lock (mutex=0x7ff7fcaed8 <buffer_lock>) at pthread_mutex_lock.c:80
#2  0x0000007ff7fa3038 in release_buffer () from /usr/xenomai/lib/libcobalt.so.2
#3  0x0000007ff7f413c0 in __nptl_deallocate_tsd () at pthread_create.c:301
#4  0x0000007ff7f4156c in start_thread (arg=0x7ff7fa7430 <cobalt_thread_trampoline>) at pthread_create.c:488
#5  0x0000007ff7bcc49c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0

---

Patch v2:
- Use the pthread_setcancelstate() function to set the thread state
to PTHREAD_CANCEL_DISABLE.

Patch v3:
- Add backtrace output in gdb.

 lib/cobalt/printf.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/cobalt/printf.c +3 -0
index 0aa5940c6..bd2d7de56 100644
@@ -482,10 +482,17 @@ const char *rt_print_buffer_name(void)
 /* *** Deferred Output Management *** */
 void rt_print_flush_buffers(void)
 {
+	int old_state;
+
 	cobalt_thread_relax();
+

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

Quoted 10 lines
lib/cobalt/printf.c +4 -0
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
+
 	pthread_mutex_lock(&buffer_lock);
 	print_buffers();
 	pthread_mutex_unlock(&buffer_lock);
+
+	pthread_setcancelstate(old_state, NULL);
 }
 
 static void release_buffer(struct print_buffer *buffer)

Thanks, applied.

Jan