镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

The SChunk passed to Python codec/filter callbacks dangles once the array is freed #793

Open

Description

@FabianIsensee

Python codec and filter callbacks registered with blosc2.register_codec() / blosc2.register_filter() get an SChunk as their last argument (general_encoder, general_decoder, general_forward, general_backward in blosc2_ext.pyx). That object is a view over the C super-chunk with nothing that keeps the super-chunk alive. A callback that keeps it (e.g. to cache something per array) reads freed memory after the array is gone: wrong values, and under AddressSanitizer a heap-use-after-free in SChunk.nchunks.

import gc, numpy as np, blosc2
kept = []
blosc2.set_nthreads(1)
def forward(src, dst, meta, schunk):
    kept.append(schunk); dst[:] = src
def backward(src, dst, meta, schunk):
    dst[:] = src
blosc2.register_filter(250, forward, backward, "keep_schunk")
a = blosc2.asarray(np.arange(1000, dtype=np.int64), chunks=(100,), blocks=(50,),
                   cparams={"filters": [250], "filters_meta": [0], "nthreads": 1}, dparams={"nthreads": 1})
print(kept[-1].nchunks)                         # 10
del a; gc.collect(); _ = [bytearray(1 << 16) for _ in range(200)]
print(kept[-1].nchunks, list(kept[-1].meta))    # 4.14.1 and main: 0 []

The object is presumably only meant to be used during the call. If so, one option is to invalidate it when the callback returns (clear its pointer so that later use raises instead of reading freed memory), or at least to document that it must not be kept. Each callback also constructs a full blosc2.SChunk through its Python __init__ per call, which a lighter wrapper could avoid.

I used Claude Code (Anthropic) to find and reproduce this and draft the issue; I checked the repro.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions