Discussion:
Crash in Itcl when using Iwidgets in multiple threads
Add Reply
Petro Kazmirchuk
2024-08-23 12:29:53 UTC
Reply
Permalink
Minimal example:

package require Thread

set thread_script {
package require Iwidgets
iwidgets::pushbutton .btn -text [thread::id]
pack .btn
after 4000 [list thread::release [thread::id]]
thread::wait
}

for {set i 0} {$i<1000} {incr i} {
thread::create -preserved $thread_script
after 500
}

This code runs 8 threads in parallel each showing an iwidget (pushbutton
or anything else, doesn't matter) and after ~1 minute it crashes with:

Unhandled exception at 0x00007FF8846DA6E4 (itcl424t.dll) in
tclsh86t.exe: 0xC0000005: Access violation reading location
0x0000000000000018.

Windows, Tcl 8.6.14, Itcl 4.2.4, Itk 4.2.6, Iwidgets 4.1.
The same crash happens when using the Itk written in pure Tcl:
https://chiselapp.com/user/rene/repository/itk/home

That's why I'm convinced the problem is in Itcl rather than Itk, and the
stack trace points to Itcl as well. Probably, there is a mutex missing
somewhere.

I didn't try to reproduce it on Linux.

I understand that both Iwidgets and Itcl are legacy that is barely
maintained, that's why I'm not raising a bug in Fossil that very few
people would notice. What I'm asking instead from a much wider audience
here is for a workaround. Is it possible to change my code somehow to
avoid the crash? Of course, preserving the basic multi-threaded
architecture.

The original code is a huge framework that can't be rewritten to
separate GUI into one main thread.

Thanks in advance,
Petro
Harald Oehlmann
2024-08-23 12:52:43 UTC
Reply
Permalink
Post by Petro Kazmirchuk
package require Thread
set thread_script {
    package require Iwidgets
    iwidgets::pushbutton .btn -text [thread::id]
    pack .btn
    after 4000 [list thread::release [thread::id]]
    thread::wait
}
for {set i 0} {$i<1000} {incr i} {
    thread::create -preserved $thread_script
    after 500
}
This code runs 8 threads in parallel each showing an iwidget (pushbutton
Unhandled exception at 0x00007FF8846DA6E4 (itcl424t.dll) in
tclsh86t.exe: 0xC0000005: Access violation reading location
0x0000000000000018.
Windows, Tcl 8.6.14, Itcl 4.2.4, Itk 4.2.6, Iwidgets 4.1.
https://chiselapp.com/user/rene/repository/itk/home
That's why I'm convinced the problem is in Itcl rather than Itk, and the
stack trace points to Itcl as well. Probably, there is a mutex missing
somewhere.
I didn't try to reproduce it on Linux.
I understand that both Iwidgets and Itcl are legacy that is barely
maintained, that's why I'm not raising a bug in Fossil that very few
people would notice. What I'm asking instead from a much wider audience
here is for a workaround. Is it possible to change my code somehow to
avoid the crash? Of course, preserving the basic multi-threaded
architecture.
The original code is a huge framework that can't be rewritten to
separate GUI into one main thread.
Thanks in advance,
Petro
Petro,
if you look here:
https://core.tcl-lang.org/itcl/timeline?udc=1&ss=v&n=&y=all&advm=0
ITCL has commits two days ago. So, there is some care.
It is a bundled package.
So it would be great to file a bug report.

Thanks,
Harald
Petro Kazmirchuk
2024-08-23 14:18:51 UTC
Reply
Permalink
Post by Harald Oehlmann
Post by Petro Kazmirchuk
package require Thread
set thread_script {
     package require Iwidgets
     iwidgets::pushbutton .btn -text [thread::id]
     pack .btn
     after 4000 [list thread::release [thread::id]]
     thread::wait
}
for {set i 0} {$i<1000} {incr i} {
     thread::create -preserved $thread_script
     after 500
}
This code runs 8 threads in parallel each showing an iwidget
(pushbutton or anything else, doesn't matter) and after ~1 minute it
Unhandled exception at 0x00007FF8846DA6E4 (itcl424t.dll) in
tclsh86t.exe: 0xC0000005: Access violation reading location
0x0000000000000018.
Windows, Tcl 8.6.14, Itcl 4.2.4, Itk 4.2.6, Iwidgets 4.1.
https://chiselapp.com/user/rene/repository/itk/home
That's why I'm convinced the problem is in Itcl rather than Itk, and
the stack trace points to Itcl as well. Probably, there is a mutex
missing somewhere.
I didn't try to reproduce it on Linux.
I understand that both Iwidgets and Itcl are legacy that is barely
maintained, that's why I'm not raising a bug in Fossil that very few
people would notice. What I'm asking instead from a much wider
audience here is for a workaround. Is it possible to change my code
somehow to avoid the crash? Of course, preserving the basic
multi-threaded architecture.
The original code is a huge framework that can't be rewritten to
separate GUI into one main thread.
Thanks in advance,
Petro
Petro,
https://core.tcl-lang.org/itcl/timeline?udc=1&ss=v&n=&y=all&advm=0
ITCL has commits two days ago. So, there is some care.
It is a bundled package.
So it would be great to file a bug report.
Thanks,
Harald
ok done:
https://core.tcl-lang.org/itcl/tktview/0e55608dbd661663d7efeb4dc91c1c815ab5ba6e

fingers crossed :)
Schelte
2024-08-23 14:41:27 UTC
Reply
Permalink
Post by Petro Kazmirchuk
This code runs 8 threads in parallel each showing an iwidget (pushbutton
Unhandled exception at 0x00007FF8846DA6E4 (itcl424t.dll) in
tclsh86t.exe: 0xC0000005: Access violation reading location
0x0000000000000018.
As far as I know, Tk isn't thread safe. So your approach is fraught with
danger to begin with. It would be better to have one thread for the GUI
and separate threads to do the work, if needed.


Schelte.
undroidwish
2024-08-25 08:37:44 UTC
Reply
Permalink
Post by Petro Kazmirchuk
...
That's why I'm convinced the problem is in Itcl rather than Itk, and the
stack trace points to Itcl as well. Probably, there is a mutex missing
somewhere.
Your hint was valuable to track down the problem. Meanwhile sebres
found a much better solution using the Tcl_Obj infrastructure, see the
respective ticket and commit in the itcl repo.
Post by Petro Kazmirchuk
I didn't try to reproduce it on Linux.
I understand that both Iwidgets and Itcl are legacy that is barely
maintained, that's why I'm not raising a bug in Fossil that very few
people would notice. What I'm asking instead from a much wider audience
here is for a workaround. Is it possible to change my code somehow to
avoid the crash? Of course, preserving the basic multi-threaded
architecture.
Now for the multi-threading GUI thing: IMO with the itcl fix you should
be fine on Win32. Linux (= X11) is a completely different story and in
its current state indeed not suitable for this approach. MacOS is a full
stop in this regard by design.

For X11 I've used your code snippet to tweak a setup which works for me,
see this check-in https://www.androwish.org/home/info/9b0450622edec074

The final trick was to re-use X display connections. Otherwise the
XOpenDisplay()/XCloseDisplay() in a multi-threaded program quickly
uncovers inconsistencies in Xlib/Xrender/Xft. Some more additional
locking in Tk's Xft font module (libxft isn't thread safe) was
necessary, too.

And BTW undroidwish with its X11 emulation layer ran your snippet
OOTB with the itcl fix.

BR,
Christian
undroidwish
2024-08-25 08:44:11 UTC
Reply
Permalink
Post by Petro Kazmirchuk
...
The original code is a huge framework that can't be rewritten to
separate GUI into one main thread.
If you can live with a non libxft build of Tk on X11 platforms
chances are that it's working provided the itcl fix is used.

Thus, I would start with a stock Tk built with --disable-xft.

HTH,
Christian

Loading...