Coder Social home page Coder Social logo

Comments (3)

soren121 avatar soren121 commented on April 28, 2024

This issue has affected me as well, though the length of time before the error occurs varies wildly: sometimes it happens instantly upon program start, sometimes it takes several minutes, sometimes longer.

from picamera.

waveform80 avatar waveform80 commented on April 28, 2024

Okay, finally gotten to the bottom of this and a fix will be forthcoming shortly. But it's probably worth documenting as it's an interesting one...

The issue is indeed caused by a race condition, and it's been there since day one. However, being a race condition it requires specific timing for the bug to manifest. In the case of earlier versions of picamera (and I think even in raspistill too) the main thread is sufficiently fast in disabling the background encoder thread that the bug had little or no chance of appearing.

One extra "if" statement was introduced in release 1.0 and it seems this was just enough to push the main thread into being slow enough for the bug to appear. Specifically, once the camera is set to capture, the encoder callback is repeatedly until a full frame has been captured. Once a full frame has been captured, it appears the encoder callback is called once more (for what purpose I'm not sure, but it's not for writing anything to the file). Only the main thread can disable the port (shutting down the background encoder thread), so once the encoder callback notices the end of the frame it wakes up the main thread (via a semaphore) to disable the port.

In the vast majority of cases, the main thread wakes up quickly, disables the port, and everything is fine. But one in every few thousand calls (in picamera 1.0), the main thread is a little slow and the extraneous post-frame-end call to the encoder callback (mentioned above), is already running when the port gets disabled. Crucially, the port has to be disabled immediately before the call to mmal_port_send_buffer because before this is a check that the port is enabled (which must pass in order for the call to mmal_port_send_buffer to occur). This is the location of the race condition.

To fix the race condition I've done a couple of things. Firstly, we no longer check whether the port is enabled when recycling the buffers - we just assume it. Secondly, we use the internal flag in the encoder ("stopped") to ensure that, after frame-end is encountered, any subsequent calls to the encoder's callback are ignored. As the flag is only set within the encoder thread, or when the encoder thread is guaranteed not to be running, no race condition is introduced.

The test suite is currently running (mostly to make sure I haven't broken any edge cases as this is a change to the base encoder class) and if it passes, I'll push the fix for anyone desperate to get it early. Otherwise, it'll be in a release just before the weekend (hopefully with a couple of other minor bits if I can get them done in time).

from picamera.

waveform80 avatar waveform80 commented on April 28, 2024

Well, as it turned out I did break a couple of things (split recording for one). They're fixed now but I didn't quite make the release before the weekend. Instead there should be one later today after I've knocked off a couple of doc related tasks

from picamera.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.