“Too many open files” after opening videos

I’m seeing a bug in my video player where after creating a bunch of video objects, opening any subsequent files will result in a file x not found: Too many open files error.

I find that this happens exactly after the 64th video opened. The problem is that in my case, I open videos multiple times throughout the app’s life. And it looks like these accumulate and never actually close the file.

Am I understanding this correctly? If so, how does one close a video object? (I tried setting it to nil but it makes no difference.)

Here’s a basic program that recreates this bug. (Create an Assets folder with at least 64 pdv files in it. You can get a sample pdv file here and duplicate it as many times as needed.)

import "CoreLibs/graphics"

-- List files within a folder that contains at least 64 videos.
local folder = "Assets/"
local files = playdate.file.listFiles(folder)
for _, path in ipairs(files) do
	local video, videoerr = playdate.graphics.video.new(folder .. path)
	print(path, video, videoerr)
-- Any other file open after that will result in a “Too many files open” error.
local img, imgerr = playdate.graphics.image.new("SystemAssets/card")
print(img, imgerr)
local info, infoerr = playdate.file.open("pdxinfo")
print(info, infoerr)

function playdate.update()
1 Like

Since changing filesystems a while back we now have a limit of 64 simultaneous open files. I udpated fileplayer to avoid leaving filehandles open when we saw problems there due to the change, but I didn't think about videos. It doesn't look like it'll be a simple matter to delay opening files there, but I'll file it and maybe something will come to me. In the mean time, running a garbage collection cycle with collectgarbage("collect") before opening the additional files should cause those video objects to get cleaned up and their files closed.

Thank you! I added collectgarbage("collect") after the loop in my example above and that did it.