Tk Source Code

View Ticket
Login
2026-10-26
18:43 • Ticket [a0c1a9c6] The fn RemoveFromBaseChunk is not thread safe status still Closed with 3 other changes artifact: 0a2ac4fc user: serhiy.storchaka
18:20 • Closed ticket [a0c1a9c6]. artifact: a6dfc48a user: oehhar
2026-10-06
12:58
Fix [a0c1a9c6c6]: Make the base chunk of the text layout thread-specific check-in: 7d292334 user: jan.nijtmans tags: core-8-6-branch
10:54
Fix [a0c1a9c6c6]: Make the base chunk of the text layout thread-specific check-in: 97d5c4b3 user: jan.nijtmans tags: core-9-0-branch
10:37
Fix [a0c1a9c6c6]: Make the base chunk of the text layout thread-specific check-in: 2320e601 user: jan.nijtmans tags: trunk, main
2026-09-29
13:09 • Ticket [a0c1a9c6] The fn RemoveFromBaseChunk is not thread safe status still Open with 3 other changes artifact: a9d4f86c user: serhiy.storchaka
13:07 • Add attachment a0c1a9c6c6_sample.tcl to ticket [a0c1a9c6] artifact: 75163853 user: serhiy.storchaka
2026-09-24
12:51 • Closed ticket [0ec93368]: --disable-bidi does not disable base chunks and drawing in context on X11 plus 8 other changes artifact: a7692287 user: jan.nijtmans
11:54 • Ticket [a0c1a9c6] The fn RemoveFromBaseChunk is not thread safe status still Open with 2 other changes artifact: 0c25cb51 user: serhiy.storchaka
11:53 • Ticket [a0c1a9c6]: 3 changes artifact: dcfcc2b3 user: serhiy.storchaka
2024-12-12
08:49 • New ticket [a0c1a9c6]. artifact: 01a436ac user: anonymous

Ticket UUID: a0c1a9c6c632be8b2272bb84eed1ae93da49c74c
Title: The fn RemoveFromBaseChunk is not thread safe
Type: Bug Version: 8.6.15, 8.6.18, 9.0.5, 9.1b1
Submitter: anonymous Created on: 2024-12-12 08:49:46
Subsystem: 18. [text] Assigned To: nobody
Priority: 5 Medium Severity: Severe
Status: Closed Last Modified: 2026-10-26 18:43:30
Resolution: Fixed Closed By: oehhar
    Closed on: 2026-10-26 18:20:33
Description:
tkTextDisp.c:8157
If TK_LAYOUT_WITH_BASE_CHUNKS is defined (deafult on macosx),
the fn CharUndisplayProc may invoke the function RemoveFromBaseChunk.

tkTextDisp.c:9094
The fn RemoveFromBaseChunk stores the pointer to the Tcl_DString into a global variable baseCharChunkPtr defined in tkTextDisp.c:478:
/* TODO: Thread safety */
static TkTextDispChunk *baseCharChunkPtr = NULL;

tkTextDisp.c:8965
If the Tk library was compiled with --enable-threads (default on macosx) the fn FreeBaseChunk may free baseCharChunkPtr and set it to NULL.

tkTextDisp.c:9110
The freed Tcl_DString is passed to Tcl_DStringSetLength and causes a crash: EXC_BAD_ACCESS (SIGSEGV)
KERN_INVALID_ADDRESS at 0x0000000000000024

Proposed fix:
Disable TK_LAYOUT_WITH_BASE_CHUNKS when compiled with --enable-threads
User Comments: serhiy.storchaka added on 2026-10-26 18:43:30:

Also in [97d5c4b3f8] (9.0) and [7d292334dc] (8.6).


oehhar added on 2026-10-26 18:20:33:

Merged to main by Jan by [2320e601].

Closed.

Thanks for all, Harald


serhiy.storchaka added on 2026-09-29 13:09:29:

The attached script a0c1a9c6c6_sample.tcl reproduces the crash. It needs the Thread package, and on X11 a build with bidi rendering. Several threads relayout text widgets at the same time, and it prints "ok" if it survives:

wish a0c1a9c6c6_sample.tcl ?nthreads? ?seconds?

On Linux/X11 with 4 threads, trunk crashed in all 40 runs, in TkTextCharLayoutProc. Branch text-basechunk-threads passed all 40 runs.

Xft on its own is not thread safe: threads crash in XftGlyphExtents when they use the same font. So the script sets up the threads one at a time and gives each its own font family.


serhiy.storchaka added on 2026-09-24 11:53:03:

Confirmed, and it is now worse on 9.1: since [8ce823d20c] base chunks are also used on Windows and X11, not only on macOS. On X11, four threads laying out text widgets crash in TkTextCharLayoutProc every time; 9.0 does not crash there.

Fix on text-basechunk-threads: keep baseCharChunkPtr in thread-specific data. Disabling base chunks in threaded builds is not needed. The same change should apply to 8.6 and 9.0 for macOS (untested).


Attachments: