Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 71000ce189a693d9967055962c2053cc95e61c2b255ca0ce17f96b2150cd636f
Ticket: 534172ff5b7f0e64f11a0ae6ba83ca1b359f73c6
Crash in DeleteReflectedChannelMap (introduced via tcllib 1.21)
User & Date: anonymous on 2023-02-21 09:44:38
Changes

  1. icomment:
    The problem appears to be the order of the callback execution during Tcl_FinalizeThread():
    
    Tcl_FinalizeThread() calls DeleteThreadReflectedChannelMap() which frees the thread-channel map. 
    A little bit later, Tcl_Release() calls DeleteInterpProc(), 
    which in turn calls DeleteReflectedChannelMap(), 
    which tries at the end to access the thread map (which was freed before).
    
    * DeleteThreadReflectedChannelMap is the thread exit handler for reflected channels,
    * DeleteReflectedChannelMap is the handler for Tcl_SetAssocData for an interp.
    
    So, in the crash situation, we see the following call pattern:
    
       Tcl_FinalizeThread
       DeleteThreadReflectedChannelMap ThreadMap 0x7f2d801c8c20
       DeleteThreadReflectedChannelMap FREE ThreadMap 0x7f2d801c8c20
       Tcl_Release 0x7f2d80001230
       ...
       Tcl_Release 0x7f2d80001230
       DeleteReflectedChannelMap interp 0x7f2d80001230 ThreadMap 0x7f2d801c8c20
    
    The following fix is a possible way to avoid the crash. The fix clears the ThreadSpecificData
    for the thread-channel map after freeing it.  Since the thread-channel map 
    always accessed via GetThreadReflectedChannelMap() it might be recreated again.
    Therefore, DeleteReflectedChannelMap() will find an existing, but empty thread-channel map.
    
    
    --- ../generic/tclIORChan.c-orig	2023-02-21 10:11:06.826441438 +0100
    +++ ../generic/tclIORChan.c-fixed	2023-02-21 10:10:02.101654796 +0100
    @@ -2795,6 +2795,10 @@
     	Tcl_DeleteHashEntry(hPtr);
         }
         ckfree(rcmPtr);
    +    { 
    +        ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
    +        tsdPtr->rcmPtr=NULL;
    +    }
     }
     
    
     static void
    
    An expert on reflected channels should check, if there might be potential 
    problem s due to the "channel forwarding", which I did not try to 
    understand in details.
    
    Another way would be to change the deletion order (delete the thread map 
    after the deletion of the interps...) but such a change has the potential 
    to cause collateral damage.
    
  2. login: "anonymous"
  3. mimetype: "text/plain"
  4. username: "gustafn"