Skip to content

[BUG] pyservicemaker: freeing a Pipeline deadlocks when its appsrc Feeder thread is inside BufferProvider.generate #135

Open

Description

@sergeykad

Describe the bug

When a Pipeline with an appsrc Feeder is freed, the process can deadlock. The Feeder binding is destroyed with the GIL held, and DataFeeder::~DataFeeder() joins the feeder thread (data_feeder.cpp). If that thread is inside BufferProvider.generate and in a call that released the GIL (a timed wait on a queue, a sleep, file or network I/O), it needs the GIL to return. It never gets it, the join never returns, and the process never exits. stop() and wait() have both returned by then.

A feeder thread that waits in the C++ loop ends normally. This is the same class of fault as #99.

Steps/Code to reproduce bug

Save the script below as repro_feeder_join.py and run it in the stock image. The pipeline is appsrc ! fakesink and does no GPU work.

docker run --rm --gpus all -v "$PWD":/repro --entrypoint bash ghcr.io/nvidia/deepstream:9.2-triton-multiarch \
  -c "pip install -q --break-system-packages pyyaml && python3 /repro/repro_feeder_join.py inside; echo exit code \$?"

inside: generate sleeps 3 s and the pipeline is freed meanwhile. The process stops at del pipeline in 7 of 7 runs. A faulthandler watchdog prints the stacks after 10 s:

Pipeline feeder-join stopped
pipeline stopped; freeing the pipeline
Timeout (0:00:10)!
Thread 0x00007dbd7db036c0 (most recent call first):
  File "/repro/repro_feeder_join.py", line 48 in generate

Thread 0x00007dbd8701f5c0 (most recent call first):
  File "/repro/repro_feeder_join.py", line 74 in main

outside: generate returns an empty Buffer() first, so the feeder thread has left Python. The pipeline is freed and the script exits 0 in 8 of 8 runs.

In our application gdb shows the same two threads: the main thread in std::thread::join() under DataFeeder::~DataFeeder(), called from pybind11's dealloc, and the feeder thread in PyEval_RestoreThread under PyBufferProvider::generate.

repro_feeder_join.py
r"""Freeing a Pipeline whose appsrc Feeder thread is inside BufferProvider.generate deadlocks the process.

The pipeline is appsrc -> fakesink. When the Pipeline is freed, the Feeder binding's destructor joins the feeder
thread while the main thread holds the GIL. If the feeder thread is at that moment inside Python, in `generate`, and
blocked in a call that releases the GIL (here `time.sleep`), it needs the GIL to go on. It never gets it, and the
join never returns.

  repro_feeder_join.py inside   generate sleeps 3 s; the Pipeline is freed while the feeder sleeps in it
  repro_feeder_join.py outside  generate returns an empty Buffer() (end of stream) at once; the Pipeline is freed
                                after the feeder has left Python

Run in the stock image (pyyaml first, because `import pyservicemaker` needs it). The pipeline does no GPU work;
`--gpus all` is there because the binding needs the driver's libcuda to import:

  docker run --rm --gpus all -v "$PWD":/repro --entrypoint bash ghcr.io/nvidia/deepstream:9.2-triton-multiarch \
    -c "pip install -q --break-system-packages pyyaml && python3 /repro/repro_feeder_join.py inside; echo exit code \$?"

Expected (both modes): "pipeline freed", "done", exit code 0.
Actual with `inside`: stop() and wait() finish ("Pipeline feeder-join stopped"), then the process stops after
"freeing the pipeline". All Python threads freeze, so faulthandler's native watchdog prints "Timeout (0:00:10)!" with
the main thread at the `del pipeline` line and the feeder thread in `generate`, then exits with code 1. Without the
watchdog the process never exits.
With `outside`: "pipeline freed", "done", exit code 0.
"""

import faulthandler
import gc
import sys
import threading
import time

from pyservicemaker import Buffer, BufferProvider, Feeder, Pipeline

SLEEP_IN_GENERATE_SEC = 3.0
WATCHDOG_SEC = 10


class Feed(BufferProvider):
    def __init__(self, inside: bool) -> None:
        super().__init__()
        self.inside = inside
        self.entered = threading.Event()
        self.left = threading.Event()

    def generate(self, size: int) -> Buffer:
        self.entered.set()
        if self.inside:
            time.sleep(SLEEP_IN_GENERATE_SEC)
            return Buffer([0] * 16)
        self.left.set()
        return Buffer()


def main(mode: str) -> None:
    feed = Feed(inside=mode == "inside")
    pipeline = Pipeline("feeder-join")
    pipeline.add("appsrc", "src", {"caps": "application/octet-stream", "format": 3})
    pipeline.add("fakesink", "sink", {"sync": False})
    pipeline.link("src", "sink")
    pipeline.attach("src", Feeder("feeder", feed), tips="need-data/enough-data")
    waiter = threading.Thread(target=pipeline.wait)
    pipeline.start()
    waiter.start()
    feed.entered.wait()
    print("feeder entered generate", flush=True)
    if not feed.inside:
        feed.left.wait()
        time.sleep(0.5)  # lets the feeder thread return from generate and end on the end of stream
    # Prints every thread's stack and exits if the stop or the free does not finish in time.
    faulthandler.dump_traceback_later(WATCHDOG_SEC, exit=True)
    pipeline.stop()
    waiter.join()
    print("pipeline stopped; freeing the pipeline", flush=True)
    del pipeline
    gc.collect()
    faulthandler.cancel_dump_traceback_later()
    print("pipeline freed", flush=True)


if __name__ == "__main__":
    if len(sys.argv) != 2 or sys.argv[1] not in ("inside", "outside"):
        sys.exit("usage: repro_feeder_join.py inside|outside")
    main(sys.argv[1])
    print("done", flush=True)

Expected behavior

Freeing the pipeline returns. For example, the Feeder_ binding (buffer.h) could use a holder whose deleter releases the GIL around delete, so the join cannot block a thread that is returning from generate.

Environment details

  • Docker on bare metal (Ubuntu 26.04.1 LTS host, RTX A4000 Laptop GPU, driver 615.71.09)
  • ghcr.io/nvidia/deepstream:9.2-triton-multiarch (digest sha256:94a49674eb475f0416ce49742430cfbd644258b4e6512b09c76ddd90164c6dc7)

Additional context

A service that feeds files through appsrc and stops on SIGTERM hangs at exit until it is killed. Our workaround: make generate return before the pipeline is freed, and wait for that with the GIL released.

Activity

  1. wyw1267 commented on Nov 23, 2026

    @wyw1267
    Collaborator

    Thank you for the detailed report and reproducer. We confirmed the GIL deadlock: Feeder_ is destroyed while the main thread holds the GIL, and DataFeeder::~DataFeeder() joins a worker that may need the GIL to return from BufferProvider.generate().

    We also found that eos_ was used for both provider EOS and worker shutdown. A non-empty buffer returned during teardown could reset eos_ to false and prevent the worker from exiting.

    You can try the patch below and we will fix this issue in the future.
    issue-135-feeder-shutdown.patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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