Artifact bc0e405fa06100f50a248c81655b655ac3bed0da67c2c35b9d62b47fab1df02d:
- File typeparse/tests_tclsh/t68_lset_reference.tcl — part of check-in [d03339047e] at 2026-10-23 11:38:18 on branch tip-764_tcl90 — Addressing "lset leak via TclLsetList (Claude)" Shared a file, used Claude Docs, executed 29 commands. The `lset` leak has been fixed (v15o, in `typeparse.zip`). Along the way, I also corrected a type-loss issue in `lpop` and a minor leak occurring during the management of typed variables. The leak was more extensive than anticipated. It affected any `lset` operation targeting a single index within a typed list—such as the common case `lset L 0 9`, whether compiled or not—resulting in a leak of approximately 170 bytes per call (17 MB over 100,000 calls in v15). The root cause lies within Tcl 9.0 itself. The core lacks a consistent convention regarding the reference returned by an abstract list’s `setElementProc`: * `TclLsetList` (used for `lset L 0 9` and `lset L {0} 9`) as well as uncompiled `lset` operations with multiple indices add a reference; therefore, the returned value must not carry an additional reference. * `INST_LSET_FLAT` (for compiled `lset M 0 1 9`) and `lpop L i` use the value as-is, implying it must already carry a reference. Our `AnnotListSetElement` followed the second convention, which caused the leak. I initially attempted the reverse—aligning the core with our approach—but Tcl’s own test abstract list follows the first convention, causing `abstractlist.test` to crash. The fix: adopting a single convention—the first one. * `AnnotListSetElement` now returns the value without an added reference. * `INST_LSET_FLAT` and `lpop` (with indices) now add the reference they expect to be present. Also discovered (and already present in v15): * `lpop L` (without an index) caused the variable to lose its type; `type get $L` would return empty. It would modify the value of a typed list in place; the variable would then hold an untyped value, effectively losing its contract. A typed list is now copied before any modification, consistent with the behavior of `lset`. There was a minor memory leak during the handling of a retyped value (`tclVar.c`): when a value—arriving without a reference—was replaced by its retyped version, the original value was never freed. Additionally, an improperly targeted cleanup operation occurred in the same section: if a contract was rejected, the code would target the intermediate value instead of the caller’s original value. Tests against v15: The new test `t68_lset_reference` measures memory usage (Linux only; it reports "no /proc" on other systems). In v15, it showed a leak in four scenarios, as well as a type loss during `lpop`; now, there are no leaks or type losses. The project's tests and examples show no new differences compared to v15, and all `.out` outputs pass. The Tcl test suites—`lset*`, `lpop`, `list*`, `cmdIL`, `abstractlist`, `lseq`, `set*`, `var`, `upvar`, `append*`, `incr*`, and `trace`—result in 1,903 passes, 84 skips, and zero failures, exactly as in v15. `valgrind` shows no issues. (user: Florentis, size: 1231) [annotate] [blame] [check-ins using]
# t68 : lset / lpop sur une liste typée : pas de référence en trop (fuite), et # lpop garde le type de la variable. Mesure de mémoire sous Linux seulement. proc rss {} { if {[catch {open /proc/self/status} f]} {return -1} set d [read $f]; close $f; regexp {VmRSS:\s+(\d+)} $d -> k; return $k } proc mesure {label body} { proc __b {} "set {list.int}L 1 2 3 4; set {list.list.int}M {1 2} {3 4} for {set k 0} {\$k < 100000} {incr k} {$body}" __b set a [rss]; __b; set b [rss] if {$a < 0} {puts [format "%-28s (pas de /proc)" $label]; return} puts [format "%-28s %s" $label [expr {$b - $a < 2000 ? "pas de fuite" : "FUITE [expr {$b-$a}] Ko"}]] } mesure "lset L 0 9" {lset L 0 9} mesure "lset L {1} 8" {lset L {1} 8} mesure "eval lset" {eval {lset L 2 7}} mesure "lset M 0 1 9" {lset M 0 1 9} mesure "eval lset M 1 0 8" {eval {lset M 1 0 8}} mesure "lpop + lappend" {lpop L; lappend L 4} mesure "lpop M 0 + lset" {lpop M 0; lset M 0 end+1 2} set {list.int}L 1 2 3 4 foreach s {{lset L 0 9} {lset L {1} 8} {lpop L} {lpop L 0} {eval {lpop L}}} { eval $s; puts [format "%-28s %-8s %s" $s $L [type get $L]] } puts [catch {set L(0) x} m]\ $m