Tcl_RegisterConfig idempotency issue
Open67769bcad3b177aaf9474f8d0a07c2242a18a7f1Created 209 days agoUpdated 43 days ago
Descriptionplain2026-05-07 18:11:05
I get the feeling that TIP59's Tcl_RegisterConfig doesn't see much use, but I rely on it to provide stubs-exporting package info to stubs-consuming jit compiled c code, to make available the info on the exporting package's library, header and other info not reliably available otherwise. I could invent my own protocol for that but Tcl_RegisterConfig seems like the intended mechanism for that.
The issue is that (in contradiction to the Tcl_RegisterConfig documentation), the supplied values *ARE* copied (which is ok, and better for my use case), but calling a second time wipes out the existing registration (after seemingly trying to merge the new info with it):
1. A fresh QCCD *cdPtr_2 is allocated.
2. Phase I retrieves the existing pkgDict from pDB, mutates it (or its duplicate if shared) by adding the call's new key/value
pairs, and writes it back: pDB[pkgName] = pkgDict now contains the merged old + new entries.
3. Phase II calls Tcl_CreateObjCommand("::demopkg::pkgconfig", ..., cdPtr_2, QueryConfigDelete).
4. Because the command already exists, TclCreateObjCommandInNs (tclBasic.c:2896) deletes the old Command via
Tcl_DeleteCommandFromToken, which synchronously fires QueryConfigDelete for the old cdPtr_1.
5. That delete proc runs (tclConfig.c:324-338):
Tcl_DictObjRemove(NULL, pDB, pkgName); // removes the merged dict we just put in!
Tcl_DecrRefCount(pkgName);
Tcl_Free(cdPtr->encoding);
Tcl_Free(cdPtr);
6. The merged sub-dict's refcount drops to zero, freeing all the byte-array value copies and string-key copies from both the first
and second calls.
7. The new command is then created bound to cdPtr_2, but pDB has no entry for pkgName any more.
Could the sequence be changed so that the second Tcl_RegisterConfig call does the unsurprising thing and merges in the passed config keys?
Verified in 8.6 and 9.0
Referenced by1
d9ca7f22feFix [67769bcad3]: a second Tcl_RegisterConfig call for the same package merges its keys instead of losing the package. check-in2026-10-20 18:52:09
serhiy.storchakawiki2026-10-20 18:53:13
Confirmed on main, 9.0 and 8.6; your analysis is right: creating the new pkgconfig command deletes the old one, and its QueryConfigDelete removes the just merged package dict.
Proposed fix in branch [registerconfig-twice]: keep a reference to the merged dict and put it back after the command is created. RegConfig.3 now says that a second call adds its keys (and replaces the values of existing ones). New test pkgconfig-3.1, using a new testregisterconfig command.
Log in (anonymous is fine) to add a comment.