Skip to content

pipeline: rpi: pisp: Check the oldest CFE job in tryRunPipeline() - #370

Open
solomai wants to merge 1 commit into
raspberrypi:nextfrom
solomai:next
Open

solomai wants to merge 1 commit into
raspberrypi:nextfrom
solomai:next

Conversation

@solomai

@solomai solomai commented Nov 22, 2026

Copy link
Copy Markdown

Reported in #369, which has the full analysis and a standalone reproducer.

Problem

tryRunPipeline() asks cfeJobComplete() whether a CFE job can be run. cfeJobComplete() tests the newest job in cfeJobQueue_ (back()), but the job that is then run is the oldest one (front()). A complete job at the head of the queue is left waiting whenever the newest job is incomplete, even with the pipeline idle and a request queued.

This is the normal state of the queue whenever a frame's CFE config buffer is dequeued after its image and stats buffers. The config buffer is not part of the completeness test, so it finds the newest job complete and opens a new one that holds nothing else. Until the next frame arrives cfeJobComplete() is false, so a job can only be started in the instant between the dequeue of a frame's image/stats buffers and the dequeue of its config buffer: once per frame. If the pipeline cannot start at that instant, the job waits for the next frame, which then waits for the one after it — one frame of latency, gained for good, on every occurrence.

Each waiting job holds a CFE image buffer. With the four allocated by default, the fourth occurrence leaves all of them in cfeJobQueue_: the CFE has nothing to capture into, no buffers are dequeued any more, the newest job can never complete and tryRunPipeline() returns early from then on. Requests are never completed again and nothing is logged, because the dequeue watchdog on the CFE image node only runs while buffers are queued on it, so the frontend timeout does not fire and the camera stays in the running state.

When it happens

The order in which a frame's three buffers are dequeued follows the file descriptor numbers of the CFE nodes: the buffers become ready together and EventDispatcherPoll handles ready descriptors in ascending order. The nodes are opened as image, config, stats, so a process with consecutive descriptors dequeues the config buffer second and never sees this. If other threads open and close descriptors while the camera manager starts, the config node can end up with the highest of the three, and the config buffer is then dequeued last for the lifetime of the process; in one multi-threaded application this was the case for a given camera in 11 of 50 process starts. Such a process runs normally as long as a request is queued and the pipeline is idle whenever a frame arrives, but an application with no request queued for four consecutive frames (a gap of 100 to 133 ms at 30 fps) stops the camera for good.

Fix

Test the job that is about to be run. cfeJobComplete() now takes the job to test: cfeBufferDequeue() passes the newest job, as before, and tryRunPipeline() the oldest. A complete job at the head of the queue runs as soon as the pipeline is idle and a request is available, whatever the state of the newest job, so a backlog drains as soon as requests come back.

Testing

Raspberry Pi 5, kernel 6.18.50+rpt-rpi-2712, two IMX219 modules, one viewfinder stream, the default four requests, 30 fps. The reproducer from #369 holds back all four requests as they complete and queues them again a given time later; latency is request completion time minus SensorTimestamp.

Requests held back Frames with no request Before, config node last After
10 ms 0 no change no change
45 / 78 / 111 ms 1 / 2 / 3 latency +1 / +2 / +3 frames, for good no change
145 ms 4 camera stopped (7 of 7) no change
300 ms 4, then 5 frames dropped by the CFE camera stopped (7 of 7) no change

With the usual descriptor order every row is "no change", before and after.

"Before" is main at 670581c, the stock 0.7.2+rpt20261009-1 packages and a 0.7.2+rpt20260817 build; "after" is 670581c with this change (48 trials, no frame delivered out of order in 26,763). With a busy Back End instead (640x480 sensor mode at 200 fps, 3840x2160 output), the camera with the config node last delivered 66 frames per second before the change and 114 after, the same as with the usual order.

pisp.cpp is identical on main and next, so the change applies to both. utils/checkstyle.py reports no issue.

tryRunPipeline() uses cfeJobComplete() to decide whether a CFE job can
be run, but cfeJobComplete() tests the newest job in cfeJobQueue_ while
the job that is then run is the oldest one. A job that has everything it
needs is left waiting whenever the newest job is incomplete.

That is the state of the queue whenever a frame's CFE config buffer is
dequeued after its image and stats buffers. The config buffer is not
part of the completeness test, so it finds the newest job complete and
opens a new one that holds nothing else. A job that could not start when
its frame arrived, because no request was queued or the Back End was
still busy, then has to wait for the next frame, which in turn waits for
the one after it. The camera gains one frame of latency on every such
occasion and never loses it.

Each waiting job holds a CFE image buffer. Once all four are in the
queue the CFE has nothing to capture into, no further buffers are
dequeued, the newest job can never complete and the camera stops for
good. Nothing is reported, as the dequeue watchdog of the CFE image node
only runs while buffers are queued on it.

Fix this by testing the job that is about to be run. cfeJobComplete()
now takes the job to test: cfeBufferDequeue() passes the newest job and
tryRunPipeline() the oldest.

Fixes: 841ef2b ("pipeline: rpi: Add support for Raspberry Pi 5")
Link: raspberrypi#369
Signed-off-by: solomai <solomaj@gmail.com>
@solomai
solomai changed the base branch from main to next November 22, 2026 11:29
@naushir

naushir commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thank you for this fix. I will submit the change to the upstream libcamera mailing list. However, I will adjust the commit message to be less verbose.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants