Skip to content

Commit

Permalink
audio: Fix memory leak when converting GST sample tags. (Fixes mopi…
Browse files Browse the repository at this point in the history
…dy#1827)

Embedded cover art, and other tags exposed to us as type `Gst.Sample`, were
causing memory leaks when converted to plain Python types using
`GstBuffer.extract_dup()`. `extract_dup()` expects the caller to free the
memory it allocates but older versions of the python3-gi don't seem to do
this. The workaround is to access samples using `GstMemory` methods instead.

This issue was present on Buster 10 systems (python-gi 3.30.4) but not on
Ubuntu 20.04 (python-gi 3.36.0).

Mopidy memory usage after scanning 2861 tracks:
Without fix: 163.2MB
With fix: 54.1MB
  • Loading branch information
kingosticks committed Aug 23, 2020
1 parent 889c8a8 commit a527bbe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ v3.1.0 (UNRELEASED)
implemented by playback providers that want to use GStreamer's download
buffering strategy for their URIs. (PR: :issue:`1888`)

- Audio: Fix memory leak when converting GStreamer `sample` type tags. (Fixes:
:issue:`1827`, PR: :issue:`1929`)


v3.0.2 (2020-04-02)
===================
Expand Down
11 changes: 10 additions & 1 deletion mopidy/audio/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ def _extract_sample_data(sample):
buf = sample.get_buffer()
if not buf:
return None
return buf.extract_dup(0, buf.get_size())
# Fix for https://github.com/mopidy/mopidy/issues/1827
mem = buf.get_all_memory()
if not mem:
return None
success, info = mem.map(Gst.MapFlags.READ)
if not success:
return None
data = info.data
mem.unmap(info)
return data


# TODO: split based on "stream" and "track" based conversion? i.e. handle data
Expand Down

0 comments on commit a527bbe

Please sign in to comment.