Tcl Source Code

View Ticket
Login
2026-10-17
08:47 • Ticket [cc592ec70d] vwait not NRE-enabled: C stack busy status still Open wi... artifact: f62e981572 user: serhiy.storchaka
08:47
Fix [cc592ec70d]: a second yieldto after one that failed with "C stack busy" panicked with "tailcall... check-in: d10cadf3ca user: serhiy.storchaka tags: core-8-6-branch
2021-06-20
08:55 • Ticket [cc592ec70d] vwait not NRE-enabled: C stack busy status still Open wi... artifact: f072aa19c6 user: pooryorick
06:41 • Ticket [f9800d52bd] vwait is not NRE-enabled, and yieldto cannot find the right ... artifact: 6ae4458216 user: pooryorick
2018-04-16
18:16 • Ticket [cc592ec70d] vwait not NRE-enabled: C stack busy status still Open wi... artifact: 2febdac6e9 user: sebres
12:25 • Ticket [cc592ec70d]: 3 changes artifact: c4a840ca8a user: sebres
10:02 • Ticket [cc592ec70d]: 3 changes artifact: b0d446b8c9 user: sebres
2018-04-14
09:28 • New ticket [cc592ec70d]. artifact: 4ebc6c35fd user: pooryorick

Ticket UUID: cc592ec70de012dff030d61affb4b8d62da6cfb6
Title: vwait not NRE-enabled: C stack busy
Type: Bug Created on: 2018-04-14 09:28:50
Submitter: pooryorick Assigned to: nobody
Subsystem: - New Builtin Commands Severity: Severe
Priority: 5 Medium Last modified: 2026-10-17 08:47:42
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:

The following script,

coroutine c2 ::apply {{} {
    yield
}}
 
coroutine c1 ::apply [list {} {
    after 0 {
        puts [list in coroutine [info coroutine]]
        yieldto c2
    }
    vwait forever
}]

results in the error,

cannot yield: C stack busy

User Comments:
sebres added on 2018-04-16 10:02:31:

The problem is here not really the NRE-capability as is. So if vwait get compiled variant it could be interrupted within execute of compiled code. But the question is why it should be - in your example the vwait should wait for variable "forever". But the execution of "yieldto c2" should interrupt the "normal" flow... And in this case either stop the vwait process? Or continue it after exit from c2 (weird for me)?

To explain what do you expect, just try to rewrite it with "while", like this:

set ::forever 1
coroutine c2 ::apply {{} {
    yield
    puts [list exit from coroutine [info coroutine]]
}}
 
coroutine c1 ::apply [list {} {
    set c {
        puts [list in coroutine [info coroutine]]
        yieldto c2
    }
    while {$::forever} {
        if {$c ne ""} $c
        set c ""
    }
    puts [list exit from coroutine [info coroutine]]
}]

In above-mentioned example the flow is interrupted and endless wait is interrupted:

in coroutine ::c1
exit from coroutine ::c2
Would it expected in your opinion?

Just as notice to me: another still worse example (producing segfault ATM):

coroutine c2 ::apply {{} {
    yield
    puts [list interim in coroutine [info coroutine]]
    yieldto c1
    puts [list exit from coroutine [info coroutine]]
    set ::forever 1
}}
 
coroutine c1 ::apply [list {} {
    after 0 {
        puts [list in coroutine [info coroutine]]
        yieldto c2
    }
    after 1 {
        puts [list in coroutine [info coroutine]]
        yieldto c2
    }
    vwait ::forever
}]


sebres added on 2018-04-16 12:25:51:

BTW. I assume that "TclNRCoroutineActivateCallback" (resp. inst_yield_to_invoke) are not NRE-safe (and should be fixed regardless of the vwait-repair).

Currently any not compiled execution of yield (in body of something not compiled) raises a segfault resp. a panic "tailcall cannot find the right splicing spot: should not happen!" after resume from any "cannot yield: C stack busy"...

coroutine c2 ::apply {{} {
    yield
    puts [list exit from coroutine [info coroutine]]
}}
 
coroutine c1 ::apply [list {} {
  foreach i {0 1} {
    puts [catch {
      time {
        puts [list in coroutine [info coroutine]]
        yieldto c2
      } 1
    } res opt]--$res--$opt
  }
}]


sebres added on 2018-04-16 18:16:47:

Another problem with yield across vwait/update etc is - event-cycle does not belong to the single interpreter.

Theoretically one thread can have multiple interpreters (for example I myself have such systems e. g. with N workers * K interpreters), so despite I don't use coroutines resp. NRE-tailcalls across main-cycle, I would not like to use compiled vwait/update version, because it may cause unexpected (or rather unclear) flow-interruptions within single vwait's across multiple interpreters.

Let alone it may not help in such case: I don't know how good such constructs will work within different TEBCResume segments. I'll try to explain:

  I1 -> vwait ...
     ... DoOneEvent -> any event of I2
        I2 -> update ...
           ... DoOneEvent -> I1-yield is due 
               **BOOM (C stack busy) **
So instead of NRE-callback resp. compiled version of vwait/update I can imagine a new interp-state like yieldInProgress, which can freeze execution of the interpreter (yield callback TclNRCoroutineActivateCallback) until the C call stack of it gets the same stackLevel. So it may be something like this:

TEBCResume:
  I1 -> vwait ...
     ... DoOneEvent -> I1-yield is due:
           if C stack is busy:
               set I1.yieldInProgress to current callBack and return TCL_YIELD.
           ...
     ... if I1.yieldInProgress add callbacks and return to TEBCResume.
  <-
  execute yieldInProgress callbacks (we can, because interp == I1 and C-stack is clear)
  continue vwait-callbacks (e. g. wait further / timeouts, etc.).


serhiy.storchaka added on 2026-10-17 08:47:42:

"Cannot yield: C stack busy" from a coroutine in an event handler under [vwait] is the same in all versions: vwait runs the event loop by nested C calls, and making it NRE-aware would be a larger design change. The panic "tailcall cannot find the right splicing spot" after a failed yieldto (sebres' scripts in the comments) was fixed for 9.0 in [eb50e83ee1] ([f9800d52bd]) and is now backported to core-8-6-branch in [d10cadf3ca], tests coroutine-7.13 and coroutine-7.14.