Tcl Source Code

View Ticket
Login
Ticket UUID: 990453
Title: leaking mutexes on Unix
Type: Bug Created on: 2004-07-13 20:45:10
Submitter: mistachkin Assigned to: andreas_kupries
Subsystem: 49. Threading Severity:
Priority: 7 High Last modified: 2004-08-25 18:44:38
Status: Closed Closed by: andreas_kupries
Resolution: Fixed Closed on: 2004-07-15 22:04:56
Version: obsolete: 8.4.6
Description:
The code in TclpFinalizeMutex on Unix currently reads:

    if (pmutexPtr != NULL) {
ckfree((char *)pmutexPtr);
*mutexPtr = NULL;
    }

It should read:

    if (pmutexPtr != NULL) {
pthread_mutex_destroy(pmutexPtr);
ckfree((char *)pmutexPtr);
*mutexPtr = NULL;
    }

This change fixes quite a few memory leaks in HEAD.  I
believe this fix should be backported to
core-8-4-branch as well.
User Comments:
andreas_kupries added on 2004-07-16 05:04:55:
Logged In: YES 
user_id=75003

Accepted. Conditions and Attrs use their destroy function as
well, and all the docs I find make the desotry complement to
init, which is used for mutexes. Committed fix to 8.4 and head.

dossy added on 2004-08-25 18:44:38:
Logged In: YES 
user_id=21885

The dangerous thing about this is that pthread_mutex_destroy
() on some platforms will cause a crash if the mutex is 
locked.  So, destroying mutexes introduces a kind of "race 
condition" -- the thread that wants to destroy the mutex 
cannot guarantee that another thread doesn't have the 
mutex locked without locking the mutex itself, but once it's 
locked the mutex, it cannot destroy it because now the 
mutex is locked.

See AOLserver Bug #810799 for a discussion on this.  
Destroying a locked mutex definitely crashes Linux (not sure if 
it's LinuxThreads or NPTL or both) but doesn't seem to crash 
on Solaris, but the manpages for both indicate that a mutex 
should be unlocked before executing pthread_mutex_destroy
().