Skip to content
Tcl Source Code View Ticket
Login

View Ticket

coro+proc+ns deadlock

Closed / Fixed3111059fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 16 years ago
Descriptionplain2010-11-17 21:46:46
As found by coldstore, simplified by dkf and ferrieux: the following leaks, a lot

while 1 {
    namespace eval foo {
      proc bar {} yield
      coroutine coro bar
      namespace delete ::foo
    }
}
msofer Ticket [3111059fff]: 1 change [History diff]2010-11-18 04:47:24
msoferplain2010-11-18 05:19:53
At least the namespace, proc and coro structs are all being leaked. This should not happen, the sequence going something like this when "namespace delete" is called:
  1. Tcl_DeleteNamespace calls Tcl_DeleteCommandFromToken on [coro]'s cmdPtr
  2. Tcl_DeleteCommandFromToken set cmdPtr->flags|=CMD_IS_DELETED and calls cmdPtr->deleteProc (== DeleteCoroutine)
  3. DeleteCoroutine sees that coroPtr is suspended and: (a) pushes a callback for RewindCoroutine, (b) runs all callbacks
  4. RewindCoroutine sets corPtr->eePtr->rewind=1 and resumes the coroutine
  5. on resuming, TEBC sees the rewind flag and returns an uncatchable error: the nre stack is wound down to the bottom of the coroutine
  6. the last callback on the coros execEnv is NRCoroutineExitCallback, coroutine deletion proceeds 
  7. ...

Problem seems to be before or around step 5, as the proc should be freed when it returns.
ferrieuxplain2010-11-18 05:39:14
In that sequence, what role does the [proc] play ?
Indeed, the same system with a non-proc coro _doesn't_ leak:

while 1 {
  namespace eval foo {
    coroutine coro yield
    namespace delete ::foo
  }
}
ferrieuxplain2010-11-18 05:50:25
Yay ! DeleteCoroutine is *not* called in the leaky case.
ferrieuxplain2010-11-18 05:55:17
In fact, in Tcl_DeleteNamespace the coroutine test 

if (cmdPtr->nreProc == NRInterpCoroutine) {

fails, because the value of cmdPtr->nreProc at this point is

       TclNRInterpProc

Is that simply a typo ?
ferrieuxplain2010-11-18 06:09:33
This is consistent with the proc/nonproc observation: the leak is correlated with the presence of a proc in the coro. As though the TclNRInterpProc was somehow "hiding" the underlying NRInterpCoroutine...
ferrieuxplain2010-11-18 07:03:29
As a confirmation, the following doesn't leak:

proc gbar {} yield
while 1 {
   namespace eval foo {
     coroutine coro ::gbar
     namespace delete ::foo
   }
}

So, the key is the proc belonging to the NS, not the coro resume command. The leak is thus a consequence of the global delayed-NS-teardown policy. The fix, as Colin mentions on the chat, is probably to insist on applying the coro special case (sync teardown) in that case.
ferrieuxplain2010-11-18 07:36:01
OK, after a bit of explanation by Miguel: that test tries to loop over commands, zapping coros. The TclNRInterpProc value is a red herring, meaning just that the scan sees the 'bar' proc. However, the bug is that 'coro' is not found.

In this case this seems to be a flaw in the hashtable scanning logic used here:
   - first entry extracted is the proc 'bar'
   - since it is a proc, nothing is done to it
   - but the iteration step does "entryPtr = entryPtr->nextPtr" which is the next IN THE BUCKET
   - here it was alone in its bucket, which ends the loop prematurely.

Fix: probably use something like search.nextEntry instead of entryPtr->nextPtr.
msoferplain2010-11-18 07:46:25
allow_comments - 1

Spot on. Fixed in head
dkfplain2010-11-18 17:11:12
Confirmed. Added some tests I had (to oo.test) that neatly spotted the issue.
dgpplain2010-11-18 22:09:19
allow_comments - 0

Any backporting needed for this?
msoferplain2010-11-18 22:12:01
No: the code is a special casing for coroutines which do not exist in previous versions
msoferplain2010-11-18 22:12:02
allow_comments - 1
Log in (anonymous is fine) to add a comment.