Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Calling Tcl_DeleteEventSource from inside Tcl_DoOneEvent segfaults

Open055240025ab8553b45b39c5ef409286a7a4e8141Created 8 years agoUpdated 22 days ago
Descriptionplain2019-02-06 13:48:32
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.
Referenced by3
  • 197f909c42Fix [055240025a]: Tcl_DeleteEventSource called from a setup or check proc check-in2026-11-12 14:37:02
  • 9c4d1a15b7Fix [055240025a]: Tcl_DeleteEventSource called from a setup or check proc check-in2026-11-11 14:14:06
  • 97243eeba2Fix [055240025a]: Tcl_DeleteEventSource called from a setup or check proc freed the event source while Tcl_DoOneEvent or Tcl_ServiceAll was walking the list. During a traversal only mark the source deleted, and free it when the outermost traversal ends. New "testevent deletesource" for the tests. check-in2026-10-18 16:50:28
sebresplain2019-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.storchakawiki2026-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].

jan.nijtmans Ticket [055240025a]: 3 changes [History diff]2026-11-11 14:14:26
Log in (anonymous is fine) to add a comment.