You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[camera_avfoundation] Errors during video recording causes inaccessible recording files to be saved to NSDocumentDirectory #166933
It pretends like a file is created in some temporary directory (where files will be deleted at some point) by using a function with name getTemporaryFilePathWithExtension. But that function actually uses NSDocumentDirectory and the path can look like "/var/mobile/Containers/Data/Application/{some_id}/Documents/camera/videos" which looks like a persistent directory specific for the app. So it is a good idea to delete generated videos on the dart side at some point (maybe good to at least document this?), otherwise it will eventually fill the entire storage and one would need to delete and reinstall the app. Problem is that the recorded file is obtained by calling stopVideoRecordingWithCompletion and if some error happens before this or during a call to it then it is not possible to obtain that file on the dart side. Currently a workaround could be to use some plugin like path_provider and use hard coded path generation in a similar way as camera_avfoundation does (for example to clear that folder in case of error), which is prone to problems if something changes. Maybe camera_avfoundation should really rather use some temporary folder or prevent these leaks by some other means.
Also when stopVideoRecordingWithCompletion fails it updates internal state in camera_avfoundation about recording to false but on the dart side in camera this is not updated. So trying to record something in this state will throw "Unhandled Exception: CameraException(A video recording is already started., startVideoRecording was called when a recording is already started.)" and on the other hand trying to stop recording throws "CameraException(Error -1008, Video is not recording!)".
@misos1 Thanks for the report.
Please elaborate the case by providing a runnable reproducible code sample that shows the reported behavior along with flutter doctor -v.
One possible way to make AVAssetWriter fail is to move the app into the background. There is sample code which does that. Of course this specific case has a simple workaround but there are also other causes for AVAssetWriter to fail. The point of this issue is that in general AVAssetWriter and so stopVideoRecordingWithCompletion can fail and when it fails it does not delete the partially written video file.
Run it in release mode. Wait some 10 seconds after each step. It outputs messages into the terminal (I could not see them when using flutter run --release, so in that case run it from within xcode or make it display text in the widget).
start video recording
move the app into the background
move the app into the foreground
stop video recording
it should print into terminal "files from camera" and there should be listed leaked files
This code sample should show that file is not deleted when stopVideoRecordingWithCompletion fails and that there is also mismatch in "isRecording" state between camera and camera_avfoundation (when you try to record another video after provided steps). Now you can also test with enabled handling of lifecycle states by uncommenting the line with WidgetsBinding.instance.addObserver(this) and it will still be leaking files. But this then shows a little different situation.
Again the point of this code sample is to just make stopVideoRecordingWithCompletion fail and show its behaviour in that case. This is not because of unexpected behavior by not handling lifecycle states. There are other causes when stopVideoRecordingWithCompletion may fail (but they may be harder to trigger, btw implementation of didChangeAppLifecycleState is not quite correct here - #167349).
Thanks for the update. Can you also share the relevant log from devtools that probably shows the leak while following the steps ? Also check if the leak occurs in profile mode or not.
Yes with flutter run --profile it also occurs and I can also see messages from the sample code. I can see the same messages also in devtools "Logging" section. Just to show how it looks when performing the mentioned steps (as I forgot to include that in the previous comment):
flutter: files from camera: []
// all steps were performed here
flutter: CameraException(IOError, AVAssetWriter could not finish writing!)
flutter: files from camera: [File: '/var/mobile/Containers/Data/Application/{some_id}/Documents/camera/videos/REC_0BFCF0CA-2B8B-41B3-8FEC-F989F7F71702.mp4']
Hi @misos1, I just tested this several times on an iPhone 16 pro max and I can't seem to reproduce the issue. Is there any other steps we can use to reproduce this?
Ensure it is run in release or profile mode, you wait at least 10 seconds after each step, by moving into the background I mean clicking on the home button so the home screen is visible, ensure you really can see messages from print. Try to uncomment the line with WidgetsBinding.instance.addObserver.
Thanks for the update. I tried the given code sample on iPhone 15 device running iOS 18.3.1 and followed the steps as mentioned running in profile mode with latest stable version, tried to wait 10 seconds after each step, ie, move the app to background, wait, then move the app to foreground, wait and then stop the recording, but I didn't see the reported log in the console, but based on the report, I'll keep the issue open for team's tracking.
2. Also when stopVideoRecordingWithCompletion fails it updates internal state in camera_avfoundation about recording to false but on the dart side in camera this is not updated. So trying to record something in this state will throw "Unhandled Exception: CameraException(A video recording is already started., startVideoRecording was called when a recording is already started.)" and on the other hand trying to stop recording throws "CameraException(Error -1008, Video is not recording!)".
changed the title [-][camera_avfoundation] camera may leak files if error happens after startVideoRecordingWithCompletion[/-][+][camera_avfoundation] Errors during video recording causes inaccessible recording files to be saved to `NSDocumentDirectory`[/+]on May 15, 2025
Activity
darshankawar commentedon Apr 11, 2025
@misos1 Thanks for the report.
Please elaborate the case by providing a runnable reproducible code sample that shows the reported behavior along with
flutter doctor -v.misos1 commentedon Apr 16, 2025
One possible way to make
AVAssetWriterfail is to move the app into the background. There is sample code which does that. Of course this specific case has a simple workaround but there are also other causes forAVAssetWriterto fail. The point of this issue is that in generalAVAssetWriterand sostopVideoRecordingWithCompletioncan fail and when it fails it does not delete the partially written video file.Run it in release mode. Wait some 10 seconds after each step. It outputs messages into the terminal (I could not see them when using
flutter run --release, so in that case run it from within xcode or make it display text in the widget).This code sample should show that file is not deleted when
stopVideoRecordingWithCompletionfails and that there is also mismatch in "isRecording" state betweencameraandcamera_avfoundation(when you try to record another video after provided steps). Now you can also test with enabled handling of lifecycle states by uncommenting the line withWidgetsBinding.instance.addObserver(this)and it will still be leaking files. But this then shows a little different situation.Again the point of this code sample is to just make
stopVideoRecordingWithCompletionfail and show its behaviour in that case. This is not because of unexpected behavior by not handling lifecycle states. There are other causes whenstopVideoRecordingWithCompletionmay fail (but they may be harder to trigger, btw implementation ofdidChangeAppLifecycleStateis not quite correct here - #167349).Code sample
Doctor output
darshankawar commentedon Apr 17, 2025
Thanks for the update. Can you also share the relevant log from devtools that probably shows the leak while following the steps ? Also check if the leak occurs in profile mode or not.
misos1 commentedon Apr 17, 2025
Yes with
flutter run --profileit also occurs and I can also see messages from the sample code. I can see the same messages also in devtools "Logging" section. Just to show how it looks when performing the mentioned steps (as I forgot to include that in the previous comment):danagbemava-nc commentedon Apr 22, 2025
Hi @misos1, I just tested this several times on an iPhone 16 pro max and I can't seem to reproduce the issue. Is there any other steps we can use to reproduce this?
misos1 commentedon Apr 22, 2025
Ensure it is run in release or profile mode, you wait at least 10 seconds after each step, by moving into the background I mean clicking on the home button so the home screen is visible, ensure you really can see messages from
print. Try to uncomment the line withWidgetsBinding.instance.addObserver.darshankawar commentedon Apr 28, 2025
Thanks for the update. I tried the given code sample on iPhone 15 device running iOS 18.3.1 and followed the steps as mentioned running in profile mode with latest stable version, tried to wait 10 seconds after each step, ie, move the app to background, wait, then move the app to foreground, wait and then stop the recording, but I didn't see the reported log in the console, but based on the report, I'll keep the issue open for team's tracking.
vashworth commentedon May 15, 2025
@misos1 Can you file a separate issue for this?
[-][camera_avfoundation] camera may leak files if error happens after startVideoRecordingWithCompletion[/-][+][camera_avfoundation] Errors during video recording causes inaccessible recording files to be saved to `NSDocumentDirectory`[/+][camera_avfoundation] ios saving path (#10832)
[camera_avfoundation] ios saving path (flutter#10832)