Tcl Source Code

View Ticket
Login
2026-10-20
18:53 • Ticket [67769bcad3] Tcl_RegisterConfig idempotency issue status still Open w... artifact: c81fd5da52 user: serhiy.storchaka
18:52
Fix [67769bcad3]: a second Tcl_RegisterConfig call for the same package merges its keys instead of l... leaf check-in: d9ca7f22fe user: serhiy.storchaka tags: registerconfig-twice
2026-05-07
18:11 • New ticket [67769bcad3] Tcl_RegisterConfig idempotency issue. artifact: 645b1b02c8 user: anonymous

Ticket UUID: 67769bcad3b177aaf9474f8d0a07c2242a18a7f1
Title: Tcl_RegisterConfig idempotency issue
Type: Bug Created on: 2026-05-07 18:11:05
Submitter: anonymous Assigned to: nobody
Subsystem: 54. Configuration Reporting Severity: Minor
Priority: 5 Medium Last modified: 2026-10-20 18:53:13
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:
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
User Comments:
serhiy.storchaka added on 2026-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 [d9ca7f22fe|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.