Skip to content

DictSupplementaryFileContainer refcount never incremented, delete_file never frees memory #495

Description

@zrgt

Summary

Severity: Critical
File: sdk/basyx/aas/adapter/aasx.py:861-917

Description

_store_refcount is designed to track how many _name_map entries reference the same content hash, so delete_file() can free the underlying bytes only when the last reference is removed. The refcount is never incremented, so files are never freed.

# add_file():
if hash not in self._store:
    self._store[hash] = data
    self._store_refcount[hash] = 0      # initialized to 0

# _assign_unique_name():
self._name_map[new_name] = (sha, content_type)
return new_name                          # refcount NEVER incremented

# delete_file():
self._store_refcount[hash] -= 1         # 0 → -1
if self._store_refcount[hash] == 0:     # -1 != 0, always False
    del self._store[hash]               # NEVER executed
    del self._store_refcount[hash]      # NEVER executed
del self._name_map[name]

Because _assign_unique_name() never increments _store_refcount, the count stays at 0 after any add_file(). Every delete_file() decrements to -1 and the equality check == 0 is never true, so _store[hash] and _store_refcount[hash] are never cleaned up. Every file ever added leaks indefinitely.

Fix

Increment _store_refcount[sha] += 1 inside _assign_unique_name() when a new _name_map entry is created (the first branch of the while True loop). Also decrement it (and skip the increment) inside the second branch when a duplicate name already maps to the same hash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsdkSomething to do with the `sdk` package

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions