rpi/pisp: a camera stops delivering frames for good, with nothing logged, when a frame's CFE config buffer is dequeued last

Aperta
#369 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
18/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp, embedded-iot, linux
Ambito
embedded-iot

Direzione di ricerca

The fault is in the PiSP pipeline handler under src/libcamera/pipeline/rpi/pisp, where the readiness check tests the newest CFE job while the oldest job is the one run. Read the job queue and buffer dequeue code, then reproduce by checking the order of CFE node file descriptors and holding requests back. Done means a camera keeps delivering frames after several frames arrive with no request queued, with the test program showing no added latency.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

On a Raspberry Pi 5 one camera can stop delivering frames permanently. No error, no warning and no timeout is reported by libcamera, the camera stays in the Running state, queueRequest() keeps succeeding, and other cameras in the same process keep running normally. Only stopping and starting the camera brings the frames back.

The cause is in the PiSP pipeline handler. The check that decides whether a CFE job may be run looks at the newest job in the job queue, but the job that is then run is the oldest one. Whenever those two are not the same job, a frame that has everything it needs is left waiting, even though the Back End is idle and the application has a request queued.

Symptom

  • No request of that camera ever completes again. Requests queued afterwards are accepted and simply sit there.
  • Nothing is written to the libcamera log for that camera at any log level. In particular there is no dequeue timer expiry and no frontend timeout: the watchdog on the CFE image node only runs while buffers are queued on that node, and in this state none are.
  • The sensor keeps streaming. The CFE interrupt count of that camera keeps rising the whole time.
  • Stopping and starting the camera recovers it within a fraction of a second, but the process stays susceptible, because what makes it susceptible does not change while the camera manager lives.
  • Shortly before the stall the latency of that camera typically grows in steps of exactly one frame period and never comes back down. There may be no warning at all, though: in one case the camera went from normal operation to stalled in under a third of a second.

What happens

Every frame produces three CFE buffers: image, stats and config. The handler groups the buffers it dequeues into jobs, and a job counts as complete once it holds the image and the stats buffer. The config buffer is not part of that test, but it is still stored in the newest job.

So if the config buffer of a frame is dequeued after that frame's image and stats buffers, it finds the newest job already complete and opens a fresh job that contains nothing but itself. Between frames the newest job is therefore always incomplete and the readiness check is false. The only moment at which anything can be started is the instant between the dequeue of a frame's image and stats buffers and the dequeue of its config buffer, which comes around once per frame.

If the pipeline cannot start at that one moment, the ready job stays in the queue. A request queued by the application a few milliseconds later does not start it, and neither does the end of the previous Back End run: both find the newest job incomplete and return. The waiting job is started only by the arrival of the next frame, which then has to wait for the frame after it, and so on. The camera now runs one frame late and never catches up, and every further occurrence adds one more frame of permanent latency.

Each waiting job holds one of the CFE image buffers, of which there are four by default. After the fourth occurrence all four are parked in the job queue. The CFE then has nothing to capture into, so no image or stats buffer is ever dequeued again, so the newest job can never become complete, so nothing can ever be started again. That is the stall, and it is final until the camera is stopped.

With the config buffer dequeued second, or first, the newest job is complete between frames, so a late request or the end of the previous Back End run starts the oldest job straight away and any backlog drains. That is why this is normally invisible.

Conditions

Two things have to come together.

The config node must have the highest file descriptor number among that camera's CFE nodes. The three buffers of a frame become ready at essentially the same time, and the poll dispatcher services ready descriptors in ascending numerical order, so within a frame the buffers are dequeued in the order of the descriptor numbers of the three nodes. The nodes are opened in the order image, config, stats, so a process that gets three consecutive numbers has the config node in the middle and is unaffected. The config node ends up last when another thread of the same process closes a lower-numbered descriptor between the opening of the config node and the opening of the stats node. In a multi-threaded application that starts other I/O while the camera manager starts, this was the case for a given camera in 11 of 50 process starts, and once it happens it holds for the lifetime of the camera manager. For a sensor that also delivers embedded data the rule is the same, with the config node last of four.

The pipeline has to miss that one chance to start, four times. A miss happens when, at the moment a frame arrives, the application has no request queued, or the Back End is still busy with the previous frame. The four misses need not be related: they can accumulate over many minutes, or happen all at once, since a single gap in which four consecutive frames arrive with nothing queued is enough. At 30 fps that gap is only 100 to 133 ms. An application that is occasionally slow to hand requests back, or one running the Back End with an output large enough that a run takes longer than a frame period, produces such misses on its own.

An application that always has a request queued in time shows nothing even with the config node last, which is presumably why this has gone unnoticed. Conversely, with any other descriptor order the very same gap on the application side costs only a few delayed frames, and the pipeline catches up by itself.

Environment

Raspberry Pi 5 Model B Rev 1.1, Debian 13 (trixie), kernel 6.18.50+rpt-rpi-2712, two IMX219 modules. Reproduced with the stock libcamera 0.7.2+rpt20261009-1 packages and with main built from source. The relevant code is identical in next and upstream, and has not changed since the PiSP handler was first added. The rpi/vc4 handler is not affected, as there the queue that is tested is the queue the frame is taken from.

What was observed

With a small test program that uses nothing but libcamera, arranges for the config node to get the highest descriptor and then holds back all four requests for a given time, the behaviour is reproducible every time and scales exactly as described above: holding the requests back long enough for one, two or three frames to arrive with nothing queued adds one, two or three frame periods of latency permanently, and four frames stall the camera for good. The same program with the usual descriptor order, and the same program with the fix below, show no change in any of those cases. Keeping the Back End busy instead of holding requests back costs the affected camera about 40 % of its frame rate and some 20 ms of latency, again with no change for the other descriptor orders.

It was first found in a multi-threaded application. Three sessions in which one camera had the config node last ended with that camera stalled after 14, 18 and 22 minutes, each time in exactly the state described above: all four image buffers dequeued, the Back End idle, and requests queued later never starting anything. Sessions in which the same camera had the config node last but was never short of requests ran to the end, as did every session with any other descriptor order.

Lingua principale
C++
Stelle
493
Fork
205
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di raspberrypi/libcamera

Tutte le issue di raspberrypi/libcamera

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.