Tcl Source Code

View Ticket
Login
Ticket UUID: 055240025ab8553b45b39c5ef409286a7a4e8141
Title: Calling Tcl_DeleteEventSource from inside Tcl_DoOneEvent segfaults
Type: Bug Created on: 2019-02-06 13:48:32
Submitter: resuna Assigned to: jan.nijtmans
Subsystem: 04. Async Events Severity: Important
Priority: 5 Medium Last modified: 2026-11-11 14:14:26
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 9.1.0
Description:
When deleting the structures associated with a Tcl command (kafkatcl subscriber object) I needed to delete the Tcl event handler associated with it. Apparently if this is done done from a callback inside Tcl_DoOneEvent it later segfaults at tclnotify.c line 958 while it's walking the event check list that Tcl_DeleteEventSource removed the event from.
User Comments:
sebres added on 2019-02-06 14:46:28:
Hi Peter,

although and of course the SF is always bad...

But Tcl_DeleteEventSource does not removes the event, it removes the event source.

Basically Tcl_CreateEventSource is used to register new source of events and Tcl_DeleteEventSource will used later to clean-up it (at end of thread/unload module/something similar).
Both are normally not intended for usage like continuously create/delete.

So what exactly is the case, resp. in which callback it exactly is at the moment as deletion occurs?
Is it setupProc or checkProc (initially registered by Tcl_CreateEventSource) where you try to delete the source?

Anyway I think I know what is may be, so will try to fix it (I believe I had already fixed this in my event-perf-branch'es).

serhiy.storchaka added on 2026-10-18 16:52:47:

Still present in 9.1 (9.0 and 8.6 have the same loops): Tcl_DeleteEventSource() frees the source at once, while Tcl_DoOneEvent() or Tcl_ServiceAll() continue the traversal through its nextPtr, and a deleted source may still have its procs called.

Proposed fix in branch notifier-delete-source-in-loop: during a traversal the source is only marked deleted, and the outermost traversal frees it. Tests notify-3.1 to 3.4, with a new [testevent deletesource].