Skip to content
Tcl Source Code View Ticket
Login

View Ticket

unset traces enable calling an object command mid-destruction

Closed / Duplicate708422a066c634e914c10fdf21bac97f239d602fCreated 2 years agoUpdated 2 years ago
Descriptionmarkdown2024-09-04 04:13:52

unset traces can inject arbitrary code right in the middle of destroying an object. In this halfway state, the object command exists, [info object isa object] says it's valid, but the object namespace is gone. Deleting the object namespace is what triggers the unset trace.

The following code dies with SIGSEGV inside TclOOGetCallContext (tclOOCall.c:1024) when it does oPtr->selfCls->classChainCache != NULL because oPtr->selfCls is NULL.

oo::class create foo {
    variable x
    constructor {} {
        trace add variable x unset [list apply {{self ns args} {
            puts [list args -> $args]
            puts [list info object isa object $self -> [info object isa object $self]]
            puts [list namespace exists $ns -> [namespace exists $ns]]
            $self test
        }} [self] [self namespace]]
    }
    method test {} {}
}
[foo new] destroy

The above prints:

args -> {::oo::Obj12::x {} unset}
info object isa object ::oo::Obj12 -> 1
namespace exists ::oo::Obj12 -> 0
Segmentation fault (core dumped)

For now, my workaround is to check for namespace existence in my unset trace, allowing me to distinguish between variables being unset for normal reasons and variables being unset because the object is being destroyed.

andy Ticket [708422a066]: 3 changes [History diff]2024-09-04 04:15:01
oehharwiki2024-09-09 08:46:19

TTT meeting today accknowledged this bug but does not want to delay 9.0.0 for this. It is planned to address this in 9.0.1.

Harald

dkfmarkdown2024-09-16 14:58:29

This is very tricky. The problem is that once the namespace itself has started to go, lookup of things gets very very messy. I'd not thought about using unset traces to put callbacks in at that point; I need to extend the battery of tests for destruction sequences a bit more.

I'm guessing that the resolution will be to make $self test fail to dispatch with an error once destruction has advanced that far... but I'm not sure about that. That there will be some sort of error is pretty much certain... except it might not be easily observable (or maybe it will if you have the event loop going). Prefer destructors; they get run at a time when things are more reliable. When run via the destroy method, they can even report Tcl errors correctly. (The object is still destroyed, but you get a nice error.)


Thank you for reporting this!

dkfwiki2024-09-16 15:58:21

Seems to be a duplicate of [7842f33a5c], which wasn't fixed in 8.6.14 but has been fixed since.

But I like the test case.

dkf Closed ticket [708422a066]. [History diff]2024-09-17 08:36:44
Log in (anonymous is fine) to add a comment.