tk icon indicating copy to clipboard operation
tk copied to clipboard

Ruby crashes during `require "tk"` on Mac OS 15

Open rongcuid opened this issue 11 months ago • 3 comments

I am on Mac OS 15.2 M2, using Ruby 3.4.1 which is installed from chruby, and it seems that #33 has reappeared:

irb(main):001> require "tk"
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow should only be instantiated on the main thread!'
*** First throw call stack:
(
        0   CoreFoundation                      0x000000018df12e80 __exceptionPreprocess + 176
        1   libobjc.A.dylib                     0x000000018d9facd8 objc_exception_throw + 88
        2   CoreFoundation                      0x000000018df37534 _CFBundleGetValueForInfoKey + 0
        3   AppKit                              0x0000000191a1ac4c -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 260
        4   AppKit                              0x0000000191a1ab3c -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48
        5   libtk8.6.dylib                      0x0000000127b0bb5c TkMacOSXMakeRealWindowExist + 496
        6   libtk8.6.dylib                      0x0000000127b0b85c TkWmMapWindow + 56
        7   libtk8.6.dylib                      0x0000000127a7cffc MapFrame + 76
        8   libtcl8.6.dylib                     0x000000012784450c TclServiceIdle + 84
        9   libtcl8.6.dylib                     0x0000000127828660 Tcl_DoOneEvent + 296
        10  libtk8.6.dylib                      0x0000000127aff81c TkpInit + 712
        11  libtk8.6.dylib                      0x0000000127a76030 Initialize + 2372
        12  tcltklib.bundle                     0x00000001273b0584 ruby_tk_stubs_init + 12
        13  tcltklib.bundle                     0x00000001273b4a08 ip_init + 852
        14  ruby                                0x0000000104e41aec vm_call_cfunc_with_frame_ + 240
        15  ruby                                0x0000000104e25d64 vm_exec_core + 9248
        16  ruby                                0x0000000104e22430 rb_vm_exec + 492
        17  ruby                                0x0000000104e48398 rb_call0 + 888
        18  ruby                                0x0000000104d1d574 rb_class_new_instance_pass_kw + 64
        19  ruby                                0x0000000104e41aec vm_call_cfunc_with_frame_ + 240
        20  ruby                                0x0000000104e25d64 vm_exec_core + 9248
        21  ruby                                0x0000000104e22430 rb_vm_exec + 492
        22  ruby                                0x0000000104e368c4 vm_invoke_proc + 712
        23  ruby                                0x0000000104df68f0 thread_do_start_proc + 668
        24  ruby                                0x0000000104df5fbc thread_start_func_2 + 796
        25  ruby                                0x0000000104df5b38 nt_start + 248
        26  libsystem_pthread.dylib             0x000000018ddb82e4 _pthread_start + 136
        27  libsystem_pthread.dylib             0x000000018ddb30fc thread_start + 8
)
libc++abi: terminating due to uncaught exception of type NSException
[1]    39903 abort      irb

rongcuid avatar Jan 12 '25 19:01 rongcuid

Just to confirm, does this crash happen on Ruby 3.3?

jeremyevans avatar Jan 12 '25 20:01 jeremyevans

Yes, it occurs on 3.3 as well

rongcuid avatar Jan 12 '25 20:01 rongcuid

@rongcuid , does this only happens within irb? I had this problem when running require 'tk'.

My workaround was, in the bin/console, to import/do proper use of Tk in the main thread and let irb run in another thread. It was quite slow but it worked.

I've documented something in this blog post (pt-BR) https://computaria.gitlab.io/blog/2024/09/23/ruby-gem-tcl-tk

jeffque avatar May 17 '25 03:05 jeffque