32-bit build broken (test lseq-bug-54329e39c7 fails)
Breakage appears to start with [this] commit: Upgrade \[lappend\] so it bytecode compiles directly with expansion of arguments
ed591cc6bb[9a2351225c] Alternate, portable way of testing for [foreach] and [lseq] interacting badly check-in2025-06-06 08:39:542456cb07e8Proposed fix for [9a2351225c]: 32-bit build broken No longer needed check-in2025-06-03 16:48:41
Proposed fix [here], simply by reverting the commit that caused it.
Can be re-applied after the problem is fixed.
(moved from duplicate ticket)
Github CI shows the following test failure:
```
==== lseq-bug-54329e39c7 does not cause memory bloat FAILED
==== Contents of test case:
set l [lseq 1000000]
proc p l {foreach x $l {}}
set premem [memusage]
p $l
set postmem [memusage]
expr {[string match *purify* [tcl::build-info]] || ($postmem - $premem < 10) ? 1 : ($postmem - $premem)}
---- Result was:
36
---- Result should have been (exact matching):
1
==== lseq-bug-54329e39c7 FAILED
```
The configuration it fails on is Linux gcc `configure CFLAGS=-m32 CPPFLAGS=-m32 LDFLAGS=-m32 --disable-64bit`. Other Linux configurations are aborted when this run fails so it is not clear if they would also fail.
The failures started happening somewhere after [0ae5d5f12e847c50](https://core.tcl-lang.org/tcl/info/0ae5d5f12e847c50). Unfortunately since fossil->github CI updates are batched, and I cannot reproduce the failure on my system, I cannot tell when it started happening.
It is also possible that the test itself is flawed. It checks for increase in memory usage and it is possible the new byte code uses a little more than expected on this test. But that is pure speculation on my part.
The problem is more likely in the INST_LAPPEND_LIST implementation, which that code causes to become more exercised.
OTOH, why is a change to the lappend compiler causing a memory leak in code that doesn't (appear) to call that command?! That makes no sense...
@dkf, do you have (or can you work on) a better fix? trunk shouldn't be in the broken state for more than a few days.
Have you confirmed that that commit fixes anything? I don't see that tag being run. (I also cannot run locally in that mode.)
Hmm, the memusage command called by that test has a resource leak. The fd is never closed.
dkf> OTOH, why is a change to the `lappend` compiler causing a memory leak in code that doesn't (appear) to call that command?! That makes no sense... Yes, that is exactly what I wondered too. Also, it may not be a memory *leak* per se but rather more memory used (that gets freed eventually when the lseq goes out of scope). Iirc, it internally builds a Tcl_Obj array on a Tcl_ListObjGetElements call. That test was intended to ensure that does not happen on simple iteration. It's hard to see why lappend compilation changes would cause that to happen. dkf> Have you confirmed that that commit fixes anything? I don't see that tag being run. I guess that tag was pushed too late for the current CI runs. The first CI run that failed included multiple fossil commits so difficult to tell immediately which one caused the failure. It is possible one of the other check-ins in that run caused the failure. I guess we will know after the next set of runs.
Without knowing the page size, it's hard to say just how much is used extra. Or if that's a problem. (At a guess, it might be deciding to convert the arith-series to a base list, but I don't see why. Checking for that would be easy to add to the test.)
But I'm very concerned that you're blaming changes to lappend for alterations in memory usage of code that doesn't touch that command, especially without evidence that the backout of the change you're blaming resolves the problem (because those builds just aren't even being scheduled to run despite having what appears to be the right tag). I just don't see how that commit could possibly cause this; it makes no sense at all.
The only other significant commit in prior to the breakage is the TIP 716 merge but it's even harder to imagine how that might have broken that test!
Ah, I see what the problem is. We've hit the usage limits.
I'm going to have to disable most workflow jobs, especially on Windows, to free up resources for future testing. You have been told about this before. Only Linux may have large numbers of jobs, precisely because it is the cheapest option.
I prefer a 'real' solution over a simple backout. But if the trunk stays 'broken' for more than a few days, we have to do something.
It makes no sense to me either.
I don't know why the 'core-*' tags didn't build. That would have given proof which commit was to 'blame'
I completely agree that this is serious. I'm just stuck on 64-bit Windows where I really cannot run that config myself. ;-)
Assuming I've not made a catastrophic blunder, I've turned down the size of the build matrices for the tags, which will start to recover resources. (Computed build matrices are a new departure for me.)
Evidence that I do have:
% eval {
set l [lseq 1000000]
proc p l {foreach x $l {}}
}
% repr $l
value is a arithseries with a refcount of 4, object pointer at 0x23b521a96b8, internal representation 0x23b521a97b8:0x0, no string representation
% memory info
total mallocs 171310
total frees 157962
current packets allocated 13348
current bytes allocated 873015
maximum packets allocated 13580
maximum bytes allocated 913761
% p $l
% repr $l
value is a arithseries with a refcount of 4, object pointer at 0x23b521a96b8, internal representation 0x23b521a97b8:0x0, no string representation
% memory info
total mallocs 3173083
total frees 3159678
current packets allocated 13405
current bytes allocated 876104
maximum packets allocated 13580
maximum bytes allocated 913761
% proc p2 l {foreach x $l {set y [memory info]};return $y}
% p2 $l
total mallocs 148173686
total frees 148160185
current packets allocated 13501
current bytes allocated 880819
maximum packets allocated 13580
maximum bytes allocated 913761
% unset l
% memory info
total mallocs 148174291
total frees 148160792
current packets allocated 13499
current bytes allocated 881069
maximum packets allocated 13580
maximum bytes allocated 913761
This shows that we're not getting excess memory usage at least on this platform.
Looking at what the failing test does, reworking to use memory info is probably the best option anyway. The test is looking to verify that foreach doesn't force an arithmetic series object to fully instantiate... which is good, but it's doing it by using a proxy measure that could be confounded by other effects, to which it thus becomes very sensitive. The fact that it's using [tcl::build-info purify] as a condition to make the test meaningless is a sign that things are not healthy.
The proof that there's nothing bad happening in the interaction between foreach and lseq is in this (with p1 and p2 pre-declared/executed to ensure the bytecode's already there):
% proc p1 l {foreach x $l {}}
% proc p2 l {foreach x [list 0 {*}$l] {}}
% p1 1
% p2 1
% memory info
total mallocs 146292
total frees 133476
current packets allocated 12816
current bytes allocated 818998
maximum packets allocated 13019
maximum bytes allocated 853114
% p1 [lseq 1 1000000]; memory info
total mallocs 3146737
total frees 3133899
current packets allocated 12838
current bytes allocated 820140
maximum packets allocated 13019
maximum bytes allocated 853114
% p2 [lseq 1 1000000]; memory info
total mallocs 6147179
total frees 6134325
current packets allocated 12854
current bytes allocated 9201511
maximum packets allocated 3012872
maximum bytes allocated 141202167
Notably, the expansion of the sequence to make a standard list (plus one element) results in a vastly higher peak memory usage, as expected.
And I'm concerned that there's so much memory still allocated in this case. There appears to be a leak!
@dkf - trying your p1/p2 with a valgrind build shows no memory leaks, and no difference between 9.0.1 and the trunk. So I don't think it is a leak in terms of lost memory. Nevertheless, that max memory allocated is strange because even if the lseq fully instantiated, it should not go THAT high even if the string form has to be generated along the way. As an aside, there are several places in the TEBCresume where abstract lists are instantiated via TclListObjElements where they need not be; it's on my todo list once things settle down. The other mildly curious thing is that the 64-bit runs do not fail (well, they are aborted because the 32-build failed but logs show they ran successfully). Of course, memory management in 64-bit could be quite different.
Building now gives:
<pre>
Invalid workflow file: .github/workflows/win-build.yml#L122
The workflow is not valid. .github/workflows/win-build.yml (Line: 122, Col: 17): Unrecognized named-value: 'need'. Located at position 10 within expression: fromJson(need.plan.outputs.gcc) .github/workflows/win-build.yml (Line: 122, Col: 17): Unexpected value '${{ fromJson(need.plan.outputs.gcc) }}'
</pre>
Building now gives:
Invalid workflow file: .github/workflows/win-build.yml#L122
The workflow is not valid. .github/workflows/win-build.yml (Line: 122, Col: 17): Unrecognized named-value: 'need'. Located at position 10 within expression: fromJson(need.plan.outputs.gcc) .github/workflows/win-build.yml (Line: 122, Col: 17): Unexpected value '${{ fromJson(need.plan.outputs.gcc) }}'
@apnadkarni p2 is designed to force things to a list; by comparing it with p1, I can see that p1 isn't converting. The number of allocs is going to be large in both cases because there's all those temporary items for the members of the sequence, but they should all be released again and cause no problems.
I'm much more concerned by the "current bytes allocated" figure in the
p2case; the memory (either from the sequence or its copy) shouldn't be still being held. OTOH, it's strictly beyond the scope of the original test. New test case?Out of scope of this issue.
I think we can rewrite the failing test to use memory info and get a more reliable result that works across all platforms and doesn't have that "interesting" purify exception. I suspect we'll need to run it in a subprocess for that to really work, as we're not just interested in leaks but also in peak allocations.
@jan.nijtmans Fixed.
I've suggested an alternate test here: branch: lseq-no-bloat-test. It uses the peak memory usage data out of memory info in a fresh interpreter, so it should be pretty reliable (and not sensitive to the OS). If I alter the code to force a conversion of the sequence to a list (à la p2) then it shows up very visibly.
I am ok with the alternate test if you think it is more suitable. My preference for statm was simply because it did not require a --enable-symbols=mem build. On the other hand, as you say it is platform specific so there's that. I'm still mystified as to why it triggered though...
I'm still mystified as to why it triggered though...
It's a puzzler to me too.
About the only possibility I can think of would be if things somehow triggered a new tranche of underlying memory blocks to be allocated for Tcl_Alloc and/or TclStackAlloc, but if that's what's being seen then it's small wonder that it isn't triggering for 64-bit (which probably uses different block sizes). I don't know if that fits the description of there being 36 more pages in use; I don't even know how much space 36 pages is on that system.
But if that's it, then it's not a bug and not indicative of the lseq failure the test is looking for, which shows up as needing space for a million new Tcl_Obj instances (minimum 24MB on 32-bit systems). I certainly don't believe that pages are as large as 650kB on any system deployed anywhere...
I believe this is now resolved... by changing how we do that test to use the peak memory usage field from memory info; that's more stable, and runs on more platforms.
The fundamental problem was I couldn't correlate a 36 page increase in memory usage to anything in the changes between a passing and failing test. It was all too far into the realm of "I guess that's maybe not a bug". It sure wasn't due to allocating array of a million pointers to back a list...
Run case "$GITHUB_REF_NAME" in
case "$GITHUB_REF_NAME" in
"main" | "core-9-0-branch")
cat >>$GITHUB_OUTPUT <<'EOF'
gcc=<<'EOJ'
{
"config": [
"",
"CFLAGS=-DTCL_NO_DEPRECATED=1",
"--disable-shared",
"--disable-zipfs",
"--enable-symbols",
"--enable-symbols=mem",
"--enable-symbols=all",
"CFLAGS=-ftrapv",
"CFLAGS=-m32 CPPFLAGS=-m32 LDFLAGS=-m32 --disable-64bit"
]
}
EOJ
EOF
;;
*)
cat >>$GITHUB_OUTPUT <<'EOF'
gcc=<<'EOJ'
{
"config": [
"",
"--enable-symbols=all",
"CFLAGS=-m32 CPPFLAGS=-m32 LDFLAGS=-m32 --disable-64bit"
]
}
EOJ
EOF
;;
esac
shell: /usr/bin/bash -e {0}
/home/runner/work/_temp/e2249492-f7ea-4ba7-9b51-9b9f2394f87a.sh: line 34: warning: here-document at line 3 delimited by end-of-file (wanted `EOF')
/home/runner/work/_temp/e2249492-f7ea-4ba7-9b51-9b9f2394f87a.sh: line 35: syntax error: unexpected end of file
Error: Process completed with exit code 2.
I was already fixing that. It's now sorted (and tested in a private repo, so I know it work).