rdkcentral / Lightning

Lightning - The WPE UI Framework for developing Apps and UX
Apache License 2.0
188 stars 109 forks source link

Double texture freeing bug #529

Closed philippe-wm closed 3 months ago

philippe-wm commented 5 months ago

Issue

We found that Lightning occasionally tries to double-release textures.

The problem is that when freeing a texture, this updates the Stage total used memory: https://github.com/rdkcentral/Lightning/blob/dev/src/tree/TextureManager.mjs#L164

Unfortunately ANY total memory usage update can cause an immediate GC: https://github.com/rdkcentral/Lightning/blob/dev/src/tree/Stage.mjs#L452

This can cause a race condition where a texture release can cause a GC run re-releasing the texture before it has finished the initial release.

Workaround

Prevent Stage to run GC when updating the total memory with a negative delta:

const _addMemoryUsage = stage.addMemoryUsage;
stage.addMemoryUsage = (delta) => {
  if (delta < 0) {
    stage._usedMemory += delta;
  } else {
    _addMemoryUsage.apply(stage, [delta]);
  }
};

Possibly related issues: #492, #520, #521