Potential deadlock issue with fork() in uClibc 0.9.27

Steve Turner steve.turner at pacemicro.com
Thu Jul 27 00:29:11 PDT 2006


I believe this issue is only of concern if MALLOC_STANDARD is used along with __UCLIBC_HAS_THREADS__ defined:

I have encountered a situation where the child process created from a fork() can hang forever. A stack trace of the hung child is as follows:


__fork() in libpthread/linuxthreads/ptfork.c
__pthread_reset_main_thread() in libpthread/linuxthreads/pthread.c
free() in libc/stdlib/malloc-standard/free.c
__pthread_mutex_lock()


__pthread_reset_main_thread() calls free() to free the thread manager stack "__pthread_manager_thread_bos". free() tries to take the mutex used to protect the malloc heap. The hang occurs when another process happens to have taken the malloc mutex at the time the child is forked. Because the child process is the only process that runs, the malloc mutex is never released to allow the child to continue execution.

The solution is to take the malloc mutex before the fork and release it after the fork. A patch file which fixes this issue is attached. It's not that elegant because I have had to #include the private header "../../libc/stdlib/malloc-standard/malloc.h" in order to use the same LOCK and UNLOCK macros that malloc-standard uses. Probably not very portable this, But I am sure you'll get the gist.

The patch is for uClibc 0.9.27 but libpthread/linuxthreads/ptfork.c is identical in uClibc 0.9.28.

system() is also affected by this issue, since it calls fork().


 <<uclibc-fork-fix.patch>> 


Regards,

-- 
Steve Turner
Senior Software Engineer
Pace Micro Technology plc
 
Bringing Technology Home

Tel:    +44 (0)1274 538244
Fax:   +44 (0)1274 538199
E-Mail: steve.turner at pacemicro.com <mailto:steve.turner at pacemicro.com> 
Victoria Road, Saltaire, West Yorkshire, BD18 3LF. United Kingdom.   www.pacemicro.com <http://www.pace.co.uk>  

This E-mail and any attachments hereto are strictly confidential and intended solely for the addressee. If you are not the intended addressee please notify the sender by return and delete the message. You must not disclose, forward or copy this E-mail or attachments to any third party without the prior consent of the sender.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: uclibc-fork-fix.patch
Type: application/octet-stream
Size: 1149 bytes
Desc: not available
Url : http://busybox.net/lists/uclibc/attachments/20060727/10168e27/uclibc-fork-fix.obj


More information about the uClibc mailing list