Menu

#468 TIP 285: Script Cancellation with Tcl_CancelEval

TIP Implementation
closed-accepted
7
2008-06-13
2006-06-02
No

Patch that implements a forthcoming TIP, entitled
"Script cancellation with Tcl_CancelEval" or something
similar.

Please test. I would like to get some feedback on this
patch so that any necessary changes can be made prior
to writing the formal TIP.

Discussion

1 2 > >> (Page 1 of 2)
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-06

    v2

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-06

    Logged In: YES
    user_id=113501

    Fixed interp deletion issue, slave interp support, and some
    issues with commands that block (after, vwait). Tweaked docs.

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-06
    • summary: Tcl_CancelEval --> Script Cancellation with Tcl_CancelEval
     
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-06

    Logged In: YES
    user_id=113501

    NOTE: This patch needs to be reviewed by kennykb and msofer.

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-09

    Logged In: YES
    user_id=113501

    Attaching updated (v3) patch that works around the
    Tcl_GetMaster (bug #1502071) issue.

    The previous (v2) patch demonstrates the issue.

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-12

    Logged In: YES
    user_id=113501

    The v4 patch is basically code complete. However, I'm still
    working on the documentation and test cases for [cancel].

    Quite a lot of fine tuning has been done to both the new
    code and the comments. The Tcl_CancelEval function now
    accepts a new flag TCL_CANCEL_UNWIND that allows the
    evaluation stack for the target interpreter to be completely
    unwound in the event of the pathological test cases like:

    while {1} { catch { while {1} {incr x} } }

    Here are some other test cases that work correctly with the
    v4 patch:

    # Tcl_CancelEval after delay test
    # returns TCL_ERROR "eval cancelled" or "eval unwound"
    # based on flags passed to Tcl_CancelEval
    after 30000

    ----------------------------------------------------------

    # Tcl_CancelEval vwait test
    # returns TCL_ERROR "eval cancelled" or "eval unwound"
    # based on flags passed to Tcl_CancelEval
    vwait forever

    ----------------------------------------------------------

    # Tcl_CancelEval very tight loop test
    # returns TCL_ERROR "eval cancelled" or "eval unwound"
    # based on flags passed to Tcl_CancelEval
    while {1} {}

    ----------------------------------------------------------

    # Tcl_CancelEval tight loop with body test
    # returns TCL_ERROR "eval cancelled" or "eval unwound"
    # based on flags passed to Tcl_CancelEval
    while {1} {incr x}

    ----------------------------------------------------------

    # Tcl_CancelEval with TCL_CANCEL_UNWIND test.
    # This can only be cancelled if the TCL_CANCEL_UNWIND
    # flag is used with Tcl_CancelEval. It will return
    # TCL_ERROR "eval unwound" in that case.
    while {1} { catch { while {1} {incr x} } }

    ----------------------------------------------------------

    # Tcl_CancelEval with TCL_CANCEL_UNWIND slightly more
    # pathological unwind test
    # returns TCL_ERROR "eval unwound"
    while {1} {
    catch {
    while {1} {
    catch {
    while {1} {incr x}
    }
    }
    }
    }

    ----------------------------------------------------------

    # Tcl_CancelEval slave interp test
    # returns TCL_ERROR "eval cancelled" or "eval unwound"
    # based on flags passed to Tcl_CancelEval
    interp create foo; foo eval { while {1} {incr x} }

    ----------------------------------------------------------

    # Tcl_CancelEval [subst] test
    # returns TCL_ERROR "eval cancelled" or "eval unwound"
    # based on flags passed to Tcl_CancelEval
    subst {[while {1} {incr x}]}

    ----------------------------------------------------------

    # Tcl_CancelEval with TCL_CANCEL_UNWIND pathological slave
    # interp test
    # returns TCL_ERROR "eval unwound"
    interp create foo; foo eval {
    while {1} {
    catch {
    while {1} {
    catch {
    while {1} {incr x}
    }
    }
    }
    }
    }

    ----------------------------------------------------------

    # basic [cancel] test
    # returns TCL_ERROR "eval cancelled"
    cancel

    ----------------------------------------------------------

    # basic [cancel] test with "--"
    # returns TCL_ERROR "eval cancelled"
    cancel --

    ----------------------------------------------------------

    # basic [cancel -unwind] test
    # returns TCL_ERROR "eval unwound"
    cancel -unwind --

    ----------------------------------------------------------

    # [cancel] test
    # returns TCL_ERROR "eval cancelled"
    catch {unset foo i}
    while {1} {
    incr i; lappend foo $i
    # pretend this is some complex
    # logic or Tk button command.
    if {$i > 3} then {cancel}
    }

    ----------------------------------------------------------

    # [cancel -unwind] test
    # returns TCL_ERROR "eval unwound"
    catch {unset foo i}
    while {1} {
    catch {
    while {1} {
    incr i; lappend foo $i
    # pretend this is some complex
    # logic or Tk button command.
    if {$i > 3} then {cancel -unwind}
    }
    }
    }

    ----------------------------------------------------------

    # recursion test
    # returns TCL_ERROR "eval cancelled"
    proc foo { i } {
    if {$i > 10} then {
    cancel
    } else {
    return [foo [expr {$i + 1}]]
    }
    }; foo 0

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-13

    Logged In: YES
    user_id=113501

    Updated (v5) patch supports a custom error message for
    script cancellation via Tcl_CancelEval and [cancel]. Also
    corrected mutex locking issue with CancelEvalProc.

    Here is a quick Tk example of [cancel] usage:

    ----------------------------------------------------

    package require Tk

    pack [text .clock] -side top -expand true \ -fill both -padx 10 -pady 10

    pack [button .go -padx 10 -pady 10 -text "Go" \ -command [list goButton]] \ -side left -padx 10 -pady 10
    pack [button .cancel -padx 10 -pady 10 -text "Cancel" \ -command [list cancelButton "clock stopped."]] \ -side right -padx 10 -pady 10

    proc goButton {args} {
    while {1} {
    catch {.clock delete 1.0 end}
    .clock insert end [clock format [clock seconds]]
    update
    }
    }

    proc cancelButton {msg} {
    cancel -unwind {} $msg
    }

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-06-18

    Logged In: YES
    user_id=113501

    Refactored error message handling to be simpler and more
    robust (v6).

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-07-11

    Logged In: YES
    user_id=113501

    Refreshed v6 patch against current HEAD (now v7).

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-08-31

    Logged In: YES
    user_id=113501

    A new patch revision is forthcoming...

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-10-12

    Logged In: YES
    user_id=113501

    Latest patch is against HEAD and cleans up a few things.

     
  • miguel sofer

    miguel sofer - 2006-10-12

    Logged In: YES
    user_id=148712

    An almost 1500 loc patch without a spec, man page or tests.
    I'm not up to guessing what it is supposed to do, sorry.
    When the forthcoming tip does come forth, I'll take a look.

     
  • miguel sofer

    miguel sofer - 2006-10-12
    • assigned_to: msofer --> nobody
     
  • Joe Mistachkin

    Joe Mistachkin - 2006-10-12

    Logged In: YES
    user_id=113501

    Sorry, I'm still working on the TIP, man pages, and tests.

    In the meantime, the code fully works. The basic
    functionality provided is the ability to cancel a script
    running in an interpreter from any thread in the process via
    Tcl_CancelEval or from within the interpreter itself via the
    [cancel] command. Some examples are provided below.

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-10-16
    • summary: Script Cancellation with Tcl_CancelEval --> TIP: Script Cancellation with Tcl_CancelEval
    • assigned_to: nobody --> msofer
     
  • Joe Mistachkin

    Joe Mistachkin - 2006-10-16

    Logged In: YES
    user_id=113501

    New v9 patch. More cleanup and consistency fixes, now
    complete with tests. Docs for Tcl_Canceled and
    Tcl_CancelEval complete. Still working on [cancel] docs.
    TIP has been submitted and should be forthcoming.

     
  • Joe Mistachkin

    Joe Mistachkin - 2006-10-16
    • summary: TIP: Script Cancellation with Tcl_CancelEval --> TIP 285: Script Cancellation with Tcl_CancelEval
     
  • Joe Mistachkin

    Joe Mistachkin - 2006-10-17

    Logged In: YES
    user_id=113501

    New v10 patch, various adjustments, corrected whitespace.

     
  • Joe Mistachkin

    Joe Mistachkin - 2007-07-24

    Logged In: YES
    user_id=113501
    Originator: YES

    Updated (v11) patch to latest HEAD.

    File Added: script_cancellation_11.zip

     
  • Joe Mistachkin

    Joe Mistachkin - 2007-12-13

    Logged In: YES
    user_id=113501
    Originator: YES

    Patch needs to be updated for latest HEAD. Also, the plan is to change the script visible command to [interp cancel] instead of [cancel].

     
  • Joe Mistachkin

    Joe Mistachkin - 2008-04-04

    Logged In: YES
    user_id=113501
    Originator: YES

    Patch updated to latest HEAD.

    Still need to change [cancel] to [interp cancel] and add docs for it.

    File Added: script_cancellation_12.zip

     
  • Joe Mistachkin

    Joe Mistachkin - 2008-04-04

    Logged In: YES
    user_id=113501
    Originator: YES

    Patch updated.

    Changed [cancel] to [interp cancel] and added appropriate docs.

    File Added: script_cancellation_13.zip

     
  • Joe Mistachkin

    Joe Mistachkin - 2008-04-04

    Logged In: YES
    user_id=113501
    Originator: YES

    Minor corrections.

    File Added: script_cancellation_14.zip

     
  • Joe Mistachkin

    Joe Mistachkin - 2008-04-05

    Logged In: YES
    user_id=113501
    Originator: YES

    More minor corrections.

    File Added: script_cancellation_15.zip

     
  • Joe Mistachkin

    Joe Mistachkin - 2008-04-05

    Logged In: YES
    user_id=113501
    Originator: YES

    Adding preliminary patch for Tk changes.

    File Added: tk_cancellation_1.zip

     
1 2 > >> (Page 1 of 2)