Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2018-01-16
11:43 • Closed ticket [b1b2d13581]: Tcl_Eval causes SIGSEGV, Segmentation fault plus... artifact: 6484826e65 user: sebres
2018-01-09
21:37 • Ticket [b1b2d13581]: 3 changes artifact: 7e68601370 user: sebres
20:23 • Ticket [b1b2d13581]: 3 changes artifact: 9baab77cee user: spatel
18:04 • Ticket [b1b2d13581]: 4 changes artifact: 36cec2fa9e user: sebres
2018-01-08
22:04 • Ticket [b1b2d13581]: 3 changes artifact: e5a189f272 user: spatel
15:21 • New ticket [b1b2d13581]. artifact: 64f707116b user: spatel

Tcl_Eval causes SIGSEGV, Segmentation fault

Closed / Invalidb1b2d135818b64c11817013ea8828b298b08a079Created 9 years agoUpdated 9 years ago
Descriptionplain2018-01-08 15:21:28
I am using Tcl_ThreadQueueEvent() & Tcl_ThreadAlert() to execute TCL script from C++. The call back procedure uses Tcl_Eval() to call the TCL script. The program executes just fine for couple of times. Tipically third or fourth execution of the same program gets SIGSEGV: Segmentation fault.

I also tried using Tcl_EvalObjEx() instead of Tcl_Eval() in my callback procedure. But same result. Any help/hint would be much appreciated.
spatel Ticket [b1b2d13581]: 3 changes [History diff]2018-01-08 22:04:47
sebresplain2018-01-09 18:04:57
Do you consider the pre-requirements of multi-threaded tcl-execution?

- be sure you use threaded-tcl compiled;
- Tcl_Obj's are not shared over threads (belong only one thread at the same time);
Tcl does not have GIL (e. g. in opposite to python), so you should transfer the "ownership" of an object to calling thread (it is not usable in source thread) or use Tcl_DuplicateObj of it before submitting to another thread;
- if you've own complex Tcl_ObjType, you should create safe duplicate mechanism for it's duplication;
- execution of Tcl_EvalObjEx can release a tcl-object, if its refCount equal 0 before execution (always use Tcl_IncrRefCount before call it), so if you do Tcl_DecrRefCount hereafter, you can mistakenly free another object that you don't own;
- if you have some references in the Tcl_Obj's to several interpreter's (e. g. each thread have more as one interpreter), you should be in the context of this interpreter by the execution (and it is preserved as long as execution takes place);
- don't use objects of not thread-safe modules (like "tk") across multiple threads;
- you are basically familiar with multi-threaded development (synchronization primitives, etc.);

Intrinsically Tcl_ThreadQueueEvent() works fine and is's a part of many internal mechanisms of Tcl (notifier, IO, reflected channels, etc.). 
Additionally it works fine in tclthread-module (but e. g. also in several other modules of me), so I don't think it has any bug.

I believe you do possibly something wrong in your code.
It would be nice to have the call-stack on the exception, to do analyze what is wrong there.
Without it, it's almost impossible to find something...
spatelplain2018-01-09 20:23:19
Here is the stack:

1  DisplayText                           0x7ffff7b429c0 
2  TclServiceIdle                        0x7ffff77e5be6 
3  Tcl_DoOneEvent                        0x7ffff77c737d 
4  Tk_UpdateObjCmd                       0x7ffff7ac07cd 
5  TclNRRunCallbacks                     0x7ffff7705b77 
6  TclEvalEx                             0x7ffff7707cf0 
7  Tcl_EvalEx                            0x7ffff7708006 
8  Tcl_Eval                              0x7ffff7708025 
9  __tlc_thread_callback tcl_thread.h 21 0x7fffd93bb25c 
10 Tcl_ServiceEvent                      0x7ffff77c7037 
11 Tcl_DoOneEvent                        0x7ffff77c7275 
12 Tk_UpdateObjCmd                       0x7ffff7ac07cd 
13 TclNRRunCallbacks                     0x7ffff7705b77 
14 TclEvalEx                             0x7ffff7707cf0 
15 Tcl_EvalEx                            0x7ffff7708006 
16 Tcl_Eval                              0x7ffff7708025 
17 __tlc_thread_callback tcl_thread.h 21 0x7fffd93bb25c 
18 Tcl_ServiceEvent                      0x7ffff77c7037 
19 Tcl_DoOneEvent                        0x7ffff77c7339 
20 Tcl_VwaitObjCmd                       0x7ffff778c0c6 
21 TclNRRunCallbacks                     0x7ffff7705b77 
22 AfterProc                             0x7ffff77e5450 
23 TclServiceIdle                        0x7ffff77e5be6 
24 Tcl_DoOneEvent                        0x7ffff77c737d 
25 Tk_MainLoop                           0x7ffff7ac7e82 
26 Tk_MainEx                             0x7ffff7ad5df8 
27 main                                  0x4009cc       

Here is the code for the __tcl_thread_callback()

int __tlc_thread_callback(Tcl_Event* e, int flags) {
    // Convert the even to the derived struct so that
    // the interp and cmd can be extracted.
    Tcl_ThreadEvent* me = (Tcl_ThreadEvent*)e;

    // Call the actual cmd here.
    Tcl_Eval(me->interp, me->cmd);

    // Free the command since tcl won't free it.
    free(me->cmd);

    return 1;
}

This works for few times and than fails.
sebresplain2018-01-09 21:37:29
So I see tk here. 

1. If you cannot reproduce it without tk, then you should rather open the ticket by the tk-repository. This one is tcl-repository.

2. As I already wrote Tk is not thread-safe (resp. not completely).
So if you've 2 threads, you should organize the work as following:

  - main-thread (using Tk) sends "event" to 2nd-thread;
  - 2nd-thread (DON'T USE Tk) do something and answer (e. g. also with event)
    back to main-thread; Note you can use Tcl there, but not Tk.
  - main-thread "draws" the result with Tk.

Note, that only this (main) thread may use tk, but not the 2nd-thread.
Then segfaults are the best what will happen here (sometimes it is an retarded aftereffect, so exception occurs much later, thus no chance to find the wrong piece).

3. You said it's a C++ (and you call your C++ code somewhere from tcl-code) and the C++ exception handling used, you should necessarily catch the exception still in the same C++ call (before you returns to tcl-scope), I mean you should not traverse with the exception over some tcl-scope (it would be wrong).

c++_code_1 {
  ...
  c++_try {
    ...
    tcl_code { // Eval, NRE, Expr, Coroutine, etc...
      ...
      c++_code_2 {
        c++_try {
          ...
          c++_throw_except; // A allowed, B would be wrong!!!
          ...
        } c++_catch A {};
      }
    }
  } c++_catch B {};
}

Note: c++_code_2 (inside tcl-eval) CAN'T throw C++ exception of type "B".
sebresplain2018-01-16 11:43:26
Thus ticket closed as 3rd-party issue.
Let us know if (and why) it should be reopened.
Log in (anonymous is fine) to add a comment.