From 413ccb8c319e2f71d3b041a962f48cea2e57b760 Mon Sep 17 00:00:00 2001 From: yummybomb <19238148+yummybomb@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:05:26 +0000 Subject: [PATCH] Remove the bus watch when a pipeline is destroyed The watch added for every pipeline was never removed. It holds a reference to the pipeline's bus, so each destroyed pipeline left its bus and the two sockets of the bus's control socket pair behind. --- server/pkg/gst/gst.c | 6 ++++++ server/pkg/gst/gst_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/server/pkg/gst/gst.c b/server/pkg/gst/gst.c index f1cc9a14a..468a2688b 100644 --- a/server/pkg/gst/gst.c +++ b/server/pkg/gst/gst.c @@ -216,6 +216,12 @@ void gstreamer_pipeline_destory(GstPipelineCtx *ctx) { // set null state gst_element_set_state(GST_ELEMENT(ctx->pipeline), GST_STATE_NULL); + // the watch holds a reference to the bus, which would keep it and its + // control sockets after the pipeline is gone + GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(ctx->pipeline)); + gst_bus_remove_watch(bus); + gst_object_unref(bus); + if (ctx->appsink) { gst_object_unref(ctx->appsink); ctx->appsink = NULL; diff --git a/server/pkg/gst/gst_test.go b/server/pkg/gst/gst_test.go index 52ac0bf2b..80c64c423 100644 --- a/server/pkg/gst/gst_test.go +++ b/server/pkg/gst/gst_test.go @@ -1,6 +1,7 @@ package gst import ( + "os" "strings" "testing" "time" @@ -95,6 +96,38 @@ func TestCreatePipelineReleasesLockBeforeSlowCUDAProbe(t *testing.T) { } } +func TestDestroyClosesPipelineFiles(t *testing.T) { + openFiles := func() int { + entries, err := os.ReadDir("/proc/self/fd") + if err != nil { + t.Fatalf("count open files: %v", err) + } + return len(entries) + } + + runPipeline := func() { + p, err := CreatePipeline("fakesrc num-buffers=1 ! fakesink") + if err != nil { + t.Fatalf("CreatePipeline() error = %v", err) + } + p.Play() + p.Destroy() + } + + // the first pipeline opens files that GStreamer keeps for the process + runPipeline() + + const pipelines = 10 + before := openFiles() + for i := 0; i < pipelines; i++ { + runPipeline() + } + + if left := openFiles() - before; left > 0 { + t.Fatalf("%d pipelines left %d files open", pipelines, left) + } +} + func TestNVENCFailureDetail(t *testing.T) { t.Parallel()