Skip to content

[BUG] gst-nvevent: every stream reset stays stored on every pad it crosses, so memory grows and buffer pushes slow down #151

Backlog

Description

@sergeykad

Describe the bug

gst_nvevent_new_stream_reset(source_id) names the event's structure nv-stream-reset-<source>-<counter>, with a counter that grows for every event made in the process (CREATE_EMPTY_GST_STRUCT in sources/libs/gstnvdshelper/gst-nvevent.c). The event type is sticky and GST_EVENT_TYPE_STICKY_MULTI (gst-nvevent.h). GStreamer stores one sticky-multi event per type and structure name on each pad (store_sticky_event in gstpad.c), so a new reset never replaces the one before it: every pad the reset crosses keeps every reset for as long as the pad is active.

Each stored reset costs memory, and each later buffer push walks the pad's stored events, so pushes get slower with every reset. An application that resets the tracker before each file (one appsrc fed many short files, nvtracker behind nvstreammux) sends one reset per file. In our 30 minute runs of such a pipeline, at about 50 files per second, process memory grew about 5 MB per minute (about 1.5 KB per file) and the files answered per minute fell by a quarter without a tracker and by half with one. With the reset not sent, a 10 minute run of the same pipeline grew 0.4 MB per minute and kept its rate.

The legacy nvstreammux makes a new reset of its own for each reset a sink pad receives (gst_nvevent_new_stream_reset (pad_data->pad_id) in gstnvstreammux.cpp), again with a new counter name. So the pads after the muxer store every reset too, whatever name the application's reset had. With a reset sent under one fixed name, the appsrc pad and the muxer's sink pad stored 1 reset after 300 were sent, the muxer's src pad and the pads after it stored 300, and nvtracker still removed the stream for each of the 300. The new muxer (USE_NEW_NVSTREAMMUX=yes) does the same (gst_nvevent_new_stream_reset (id) in gstnvstreammux_pads.cpp), with the same counts. An application cannot work around it with a pad probe: a src pad stores a sticky event before its probes run, so the muxer's src pad keeps every reset it made.

Neither FLUSH_START and FLUSH_STOP nor a new STREAM_START removes the stored resets: in GStreamer 1.24 they remove only EOS, STREAM_GROUP_DONE, SEGMENT and TAG events, and the legacy muxer does not pass flush events on. Only deactivating the pad clears them.

The comment on the macro says the names are unique so that events from different pads do not overwrite each other in the sticky events array behind a leaky queue. A name with the source id but no counter would still keep the events of different sources apart.

Steps/Code to reproduce

The script needs no GPU and no model: an appsrc ! fakesink pipeline, the reset made by libnvdsgst_helper through ctypes, one reset before each buffer. It prints how long the first and the last 1,000 resets took, how many resets the fakesink sink pad stores, and the process memory before and after. With the argument fixed it sends the same event type and source-id field under one structure name, as the control.

Save as repro.py:

import ctypes, sys, time

gst = ctypes.CDLL("libgstreamer-1.0.so.0")
app = ctypes.CDLL("libgstapp-1.0.so.0")
nv = ctypes.CDLL("/opt/nvidia/deepstream/deepstream/lib/libnvdsgst_helper.so")
P, U, I = ctypes.c_void_p, ctypes.c_uint, ctypes.c_int
for lib, name, res, args in [
    (gst, "gst_parse_launch", P, [ctypes.c_char_p, P]), (gst, "gst_bin_get_by_name", P, [P, ctypes.c_char_p]),
    (gst, "gst_element_get_static_pad", P, [P, ctypes.c_char_p]), (gst, "gst_element_set_state", I, [P, I]),
    (gst, "gst_element_send_event", I, [P, P]), (gst, "gst_buffer_new_allocate", P, [P, ctypes.c_size_t, P]),
    (gst, "gst_pad_get_sticky_event", P, [P, U, U]), (gst, "gst_mini_object_unref", None, [P]),
    (gst, "gst_event_new_custom", P, [U, P]), (gst, "gst_structure_from_string", P, [ctypes.c_char_p, P]),
    (app, "gst_app_src_push_buffer", I, [P, P]), (nv, "gst_nvevent_new_stream_reset", P, [U]),
]:
    getattr(lib, name).restype, getattr(lib, name).argtypes = res, args

STREAM_RESET = (404 << 8) | 2 | 4 | 8 | 16  # GST_NVEVENT_STREAM_RESET from gst-nvevent.h
RESETS = 20000
fixed_name = len(sys.argv) > 1 and sys.argv[1] == "fixed"

def rss_mb():
    return int(next(l for l in open("/proc/self/status") if l.startswith("VmRSS")).split()[1]) / 1024

def stored(pad):
    n = 0
    while (event := gst.gst_pad_get_sticky_event(pad, STREAM_RESET, n)):
        gst.gst_mini_object_unref(event)
        n += 1
    return n

gst.gst_init(None, None)
pipeline = gst.gst_parse_launch(b"appsrc name=src caps=video/x-raw,format=GRAY8,width=16,height=16,framerate=30/1 "
                                b"format=time is-live=true block=true max-buffers=1 ! fakesink name=sink sync=false", None)
src = gst.gst_bin_get_by_name(pipeline, b"src")
pad = gst.gst_element_get_static_pad(gst.gst_bin_get_by_name(pipeline, b"sink"), b"sink")
gst.gst_element_set_state(pipeline, 4)  # PLAYING
rss_before, marks = rss_mb(), {}
for i in range(RESETS):
    if i in (0, 1000, RESETS - 1000):
        marks[i] = time.monotonic()
    if fixed_name:  # the same event type and field, with one structure name per source
        event = gst.gst_event_new_custom(STREAM_RESET, gst.gst_structure_from_string(
            b"nv-stream-reset-0, source-id=(uint)0;", None))
    else:
        event = nv.gst_nvevent_new_stream_reset(0)
    gst.gst_element_send_event(src, event)
    app.gst_app_src_push_buffer(src, gst.gst_buffer_new_allocate(None, 256, None))
end = time.monotonic()
time.sleep(1)
print(f"first 1000 resets: {(marks[1000] - marks[0]) * 1000:.0f} ms, "
      f"last 1000 resets: {(end - marks[RESETS - 1000]) * 1000:.0f} ms")
print(f"{'one fixed name' if fixed_name else 'gst_nvevent_new_stream_reset'}: {RESETS} resets sent, "
      f"{stored(pad)} stored on the fakesink sink pad, RSS {rss_before:.1f} MB -> {rss_mb():.1f} MB")
gst.gst_element_set_state(pipeline, 1)  # NULL
docker run --rm -v "$PWD":/repro --entrypoint python3 ghcr.io/nvidia/deepstream:9.2-triton-multiarch /repro/repro.py
docker run --rm -v "$PWD":/repro --entrypoint python3 ghcr.io/nvidia/deepstream:9.2-triton-multiarch /repro/repro.py fixed

Output:

first 1000 resets: 23 ms, last 1000 resets: 527 ms
gst_nvevent_new_stream_reset: 20000 resets sent, 20000 stored on the fakesink sink pad, RSS 16.4 MB -> 26.2 MB
first 1000 resets: 8 ms, last 1000 resets: 8 ms
one fixed name: 20000 resets sent, 1 stored on the fakesink sink pad, RSS 16.4 MB -> 16.6 MB

Expected behavior

A pad stores one stream reset per source, and a new reset of a source replaces the stored one, as it does for the control above. Memory and push time then stay flat however many resets an application sends. For example, gst_nvevent_new_stream_reset could name the structure by the source id alone (nv-stream-reset-<source>), and both muxers' own resets would then follow, since they are made by the same function. We checked this by loading a replacement gst_nvevent_new_stream_reset with that name (same type and source-id field) through LD_PRELOAD: with appsrc, the legacy muxer and nvtracker, every pad stored 1 reset after 5,000 were sent, and nvtracker still removed the stream for each of them. gst_nvevent_new_stream_eos and the other events built with the same macro store the same way.

Environment details

  • Environment location: Docker on bare-metal (Ubuntu 26.04.1 LTS host, RTX A4000 Laptop GPU, driver 615.71.09)
  • Method of DeepStream install: Docker, ghcr.io/nvidia/deepstream:9.2-triton-multiarch (digest sha256:94a49674eb475f0416ce49742430cfbd644258b4e6512b09c76ddd90164c6dc7), DeepStream 9.2.0, GStreamer 1.24.2, TensorRT 10.16.0.72, CUDA 13.2

Additional context

We send a reset before each file to clear the tracker's state for that stream (#150). The order of the reset in the legacy muxer is #149.

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