Skip to content

py/gc: keep a heap area whose only live block is its first one - #11895

Open
lynt-smitka wants to merge 2 commits into
adafruit:mainfrom
MakerClassCZ:gc-sweep-first-block
Open

lynt-smitka wants to merge 2 commits into
adafruit:mainfrom
MakerClassCZ:gc-sweep-first-block

Conversation

@lynt-smitka

Copy link
Copy Markdown

The sweep frees a heap area when last_used_block == 0, taking that to mean the area is empty. But it is also 0 when only the first block is used. Then the area is freed while an object still lives in it.

Code to reproduce it in REPL:

import gc

store = [None] * 4000
k = 0
for k in range(len(store)):
    store[k] = (k,)
    gc.collect()
    if k and store[k - 1][0] != k - 1:
        print("tuple", k - 1, "now reads", store[k - 1], "- same address as the next one:", id(store[k - 1]) == id(store[k]))
        break
else:
    print("no corruption")

Before: tuple 411 now reads (412,) - same address as the next one: True
After: no corruption

Tested on a Feather ESP32-S3 TFT.

MicroPython master has the same line.

The code was written with the help of an LLM.

The sweep took last_used_block == 0 for "empty". It is also 0 when
block 0 is the only one in use, so such an area was given back to the
port with a live object in it.

@dhalbert dhalbert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, oh! Thanks for finding! It would be good to submit this upstream as well. I recently submitted another gc bug fix: micropython#19802, which could be a model. Note in that one I added a test (which had to be in coverage.c), so a test there or if possible in Python would be good. It may need to be in coverage.c for reproducibility.

@dhalbert

Copy link
Copy Markdown
Collaborator

My "Requested Change" is to add a test. Submitting upstream can be done later.

@lynt-smitka

Copy link
Copy Markdown
Author

@dhalbert A test is a bit complicated then I taught. The fix is only compiled with MICROPY_GC_SPLIT_HEAP_AUTO, which the unix build has off, and extra_coverage() is not run in CI here, so a test in coverage.c would not check anything yet. I'm trying a python test with the growing heap turned on in the unix coverage build instead.

The unix coverage build now lets the collector add and free heap areas,
as it does on the boards. It starts with one of its four areas and gets
the others on demand, so the total heap size is unchanged.

The new test fails without the fix in the previous commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants