Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Recursive [namespace delete] causes a segfault

Closed / Fixed24d7f1a69572ef507638447eb87ccce0c9d489b9Created 361 days agoUpdated 225 days ago
Descriptionmarkdown2025-12-06 22:56:14

Dear TCL Team,

might be an artifial problem, but this code below makes TCL crash:

namespace eval rpc {
    namespace ensemble create -parameters name
    namespace export create

    proc create {name args} {
        variable _unique
        if {$name == "#auto"} {set name "rpc[incr _unique]"}
        namespace eval ::$name {
            namespace ensemble create
            namespace export init
            trace add command [namespace current] delete [namespace current]::delete
            proc init args {puts doneinit}
            proc delete {oldName newName op} {
                puts cleanup
                namespace delete [namespace current]
            }
        }
        ::${name}::init {*}$args
        return $name
    }
}

That's how you call it:

rpc r create

Now a second time:

rpc r create

boom

Thank You!

Referenced by1
  • 655eb6343fProposed solution for [24d7f1a695]: Recursive [namespace delete] causes a segfault check-in2025-12-08 10:53:46
pointsmanplain2025-12-07 15:04:17
Confirmed and confirmed for 9.0.1 also. The stack trace with 9.0.1 (as reported by valgrind) is:

Invalid read of size 8
   at 0x489B624: TclCreateObjCommandInNs (tclBasic.c:2861)
   by 0x48A4457: TclNRCreateCommandInNs (tclBasic.c:8668)
   by 0x49F1320: Tcl_ProcObjCmd (tclProc.c:214)
   by 0x4897943: procObjCmd (tclBasic.c:308)
   by 0x489DBFB: Dispatch (tclBasic.c:4640)
   by 0x489DC5F: TclNRRunCallbacks (tclBasic.c:4656)
   by 0x489D510: Tcl_EvalObjv (tclBasic.c:4395)
   by 0x489FC98: TclEvalEx (tclBasic.c:5495)
   by 0x49C0D2E: Tcl_FSEvalFileEx (tclIOUtil.c:1793)
   by 0x49D23D1: Tcl_MainEx (tclMain.c:404)
   by 0x1091D3: main (tclAppInit.c:94)
 Address 0x110 is not stack'd, malloc'd or (recently) free'd


Process terminating with default action of signal 11 (SIGSEGV)
 Access not within mapped region at address 0x110
   at 0x489B624: TclCreateObjCommandInNs (tclBasic.c:2861)
   by 0x48A4457: TclNRCreateCommandInNs (tclBasic.c:8668)
   by 0x49F1320: Tcl_ProcObjCmd (tclProc.c:214)
   by 0x4897943: procObjCmd (tclBasic.c:308)
   by 0x489DBFB: Dispatch (tclBasic.c:4640)
   by 0x489DC5F: TclNRRunCallbacks (tclBasic.c:4656)
   by 0x489D510: Tcl_EvalObjv (tclBasic.c:4395)
   by 0x489FC98: TclEvalEx (tclBasic.c:5495)
   by 0x49C0D2E: Tcl_FSEvalFileEx (tclIOUtil.c:1793)
   by 0x49D23D1: Tcl_MainEx (tclMain.c:404)
   by 0x1091D3: main (tclAppInit.c:94)
chwplain2025-12-07 19:48:02
What about this patch?

---snip---
Index: generic/tclNamesp.c
==================================================================
--- generic/tclNamesp.c
+++ generic/tclNamesp.c
@@ -2538,14 +2538,21 @@
 TclEnsureNamespace(
     Tcl_Interp *interp,
     Tcl_Namespace *namespacePtr)
 {
     Namespace *nsPtr = (Namespace *) namespacePtr;
+    Tcl_Namespace *newNsPtr;
+
     if (!(nsPtr->flags & NS_DYING)) {
-	return namespacePtr;
+    	return namespacePtr;
     }
-    return Tcl_CreateNamespace(interp, nsPtr->fullName, NULL, NULL);
+    newNsPtr = Tcl_CreateNamespace(interp, nsPtr->fullName, NULL, NULL);
+    if (newNsPtr == NULL) {
+    	nsPtr->flags &= ~(NS_DYING|NS_KILLED);
+	newNsPtr = (Tcl_Namespace *) nsPtr;
+    }
+    return newNsPtr;
 }
 

 /*
  *----------------------------------------------------------------------
  *

---snip---
jan.nijtmanswiki2025-12-08 10:58:31

Put in a [branch] now. Please test (everyone who want/can). On first sight, it looks good to me.

Thanks!

chwplain2025-12-10 04:57:31
More and more my impression is that TclEnsureNamespace() introduced this
regression. Despite its name this function does not ensure that the
given namespace is kept, since in case it got the NS_DYING flag it
usually cannot be recreated, and the function ends up with returning NULL,
i.e. no namespace at all.

Given that TclEnsureNamespace()'s sole call site is in tclBasic.c IMO
this function should be ditched once and for good.
chwplain2025-12-10 05:37:02
Please forget my last comment, it would re-introduce the
test failure in basic-15.2 which got fixed by TclEnsureNamespace()
according to ticket [0e4d88b6502ac8fe0ccc912c2bd2ef4c16279837].

Sorry for the noise!
apnadkarnimarkdown2026-04-20 16:54:24

A simpler illustration of the same issue, just to reduce the noise

 namespace eval ns {proc p {} {}; proc q {} {}; namespace delete [namespace current]; proc p {} {}; proc q {} {}}

Before even attempting a fix, I'm not clear on what the semantics should be when (a) a namespace is deleted in the middle of its execution and (b) when it is recreated from within its own eval.

apnadkarnimarkdown2026-04-21 11:53:06

Proposed fix in bug-24d7f1a6-apn. Passes test and valgrind.

Note the fix results in the namespace being recreated after deletion. I am not entirely sure that is the desirable behavior but that is how the current (non-crashing) test cases behave so I have preserved that.

apnadkarniplain2026-04-22 11:12:00
Fixed for 8.6, 9.0 and 9.1.
dgpplain2026-04-22 16:09:00
Namespace lifetime management has been fertile ground for crashes and bugs.
It is hard to get right, and often hard to even define what "right" is.

For whatever benefit it may bring to future developers, this particular
segfault originates in checkin [4039b2b895].  Apply the patch to that
checkin and it fixes the issue back then as well.
Log in (anonymous is fine) to add a comment.