Tk Source Code

View Ticket
Login
Ticket UUID: 97a7ddac250d34793f4f1c77085dd083d53a48d4
Title: X11 accessibility (ATK) is not thread-safe: crash when Tk is used in several threads
Type: Bug Version: 9.1b1
Submitter: serhiy.storchaka Created on: 2026-09-24 13:06:42
Subsystem: 67. Unix Window Operations Assigned To: aku
Priority: 5 Medium Severity: Severe
Status: Open Last Modified: 2026-09-24 17:41:56
Resolution: None Closed By: nobody
    Closed on:
Description:

The ATK support in unix/tkUnixAccessibility.c (TIP 733, new in 9.1) keeps its state in process-wide variables (tk_root_accessible, toplevel_accessible_objects, tk_to_atk_map, acc_context) without any locking, and TkAtkAccessibility_Init() runs for every Tk interpreter. When Tk is loaded in several threads at once, they modify the same GLib hash table and objects concurrently.

Four threads each running load libtcl9tk9.1.so Tk and then creating and destroying frames (no fonts involved) crash with:

#0 g_type_check_instance_is_fundamentally_a (libgobject)
#1 g_object_unref (libgobject)
#3 g_hash_table_insert (libglib)
#4 TkAtkAccessibility_Init
#5 TkpInit
#6 Initialize
#7 Tcl_LoadObjCmd

or abort with heap corruption ("corrupted size vs. prev_size", "malloc(): unaligned tcache chunk detected"). 9.0 (no ATK code) does not crash.

A mutex around the global state would not be enough: GLib's default main context is iterated from every thread's event loop, so AT-SPI requests could run in any thread and access Tk windows of other threads. Since AT-SPI treats the process as one application with one root, I suggest enabling accessibility only in one thread (the first one that initializes Tk) and using the stub ::tk::accessible commands in the others.

User Comments: kevin_walzer added on 2026-09-24 17:41:56:
Ok, thank you for the update. The advice I’ve often followed with Tk and threads is Don’t Do That, because of cases like this.

serhiy.storchaka added on 2026-09-24 17:04:18:

Correction: 9.0 crashes too when Tk is initialized in many threads at once (heap corruption detected in Xlib's XOpenIM). Only the crashes in TkAtkAccessibility_Init and GLib are new in 9.1.

A patch that enables ATK only in the first thread removes the ATK crashes, but the older crash prevents proper testing, so I will look into it first.


kevin_walzer added on 2026-09-24 14:51:32:
Can you provide a patch that activates the empty commands on non-main threads?

serhiy.storchaka added on 2026-09-24 13:26:51:

On X11 and Windows, Tk has supported one Tk interpreter per thread for a long time (each thread has its own display connection); only macOS requires the main thread. It is used with the Thread package, and there are open tickets about it ([6e3c4fb23f], [b298b92ba7]). 9.0 runs the same test without crashing.

Documenting would leave a heap corruption in a configuration that worked before. The fix does not need to make accessibility itself multi-threaded: enabling it only in the first thread that initializes Tk, and using the stub ::tk::accessible commands in other threads, would be enough.


kevin_walzer added on 2026-09-24 13:20:09:
Tk should only be run on the main thread. The accessibility design follows this principle. Is what you are testing here a realistic scenario?  Shouldn’t we just document this instead?