animebook / animebook.github.io

In-browser video player for learning Japanese with subtitles
https://animebook.github.io
MIT License
268 stars 30 forks source link

Animbook stop creating cards and freeze #53

Closed kermanzimo closed 2 years ago

kermanzimo commented 2 years ago

After certain amount of cards created, it lag/freezes so I can not create cards anymore, pressing "E" or "+" doesnt help, it dont have any reaction. Did not see any error in console, so not sure what the cause, I did have exact same problem on my other computer so I guess its inside chrome plugin itself. I usually get few lines audio to my anki cards, so may be it cant handle multiple lines? I dont know but hope can be solved because its not very conviniet to reload the page every time it crash (every 10 minites almost) Also cant watch anything longer than 30 minutes, videos linger than that simply lag so badly that cant use the website anymore. Hope it can have some improvements because I really like the project and its simplicity compare to all other ways to learn languge

soamsy commented 2 years ago

I have an update in the works for the lag with large videos.

I've encountered the Anki add button freezing issue, but it's tricky to diagnose. My suspicion is that it has to do with ffmpeg creating a bazillion worker threads and never cleaning them up, since at some point after creating too many threads Chrome kills the extension's process. I remember seeing this in the past, but not nearly as extreme as it is now. Right now if I create a flashcard it spins up 50 ffmpeg_worker.js threads, which definitely wasn't happening last year (before it was like 2 threads per card). image This leads me to believe Chrome maybe changed something performance related to WebAssembly recently (?), exacerbating the issue, or maybe it's due to me testing on a different computer with a new CPU and ffmpeg allocating threads accordingly.

Regardless, the best approach here is to get ffmpeg to actually clean up after itself.

soamsy commented 2 years ago

ffmpeg-wasm attempted to address this issue with threads never getting cleaned up in https://github.com/ffmpegwasm/ffmpeg.wasm/issues/136

The idea was to add an exit method, and then if you wanted to clean up all the threads you'd exit, and then re-instantiate ffmpeg. I tried to go for this approach, but ended up with the same problem as described in https://github.com/emscripten-core/emscripten/issues/17363 where ffmpeg would run once, but after exit, it wouldn't run again even if you re-created the ffmpeg core and everything. So, I'm not certain the 'exit' fix really fixed anything, since the ffmpeg-wasm demo still creates a thread every time you run it. The author of the emscripten case above did find a band-aid solution though, by manually inserting code to cancel the PThreads once they were done.

The issue seems to stem from emscripten not really supporting calling a main method more than once, and ffmpeg-wasm digging into emscripten internals to do it anyway. It mostly works, at least until you need to call ffmpeg dozens of times in a row like we do. https://github.com/emscripten-core/emscripten/issues/14312 has more info on this, and some options for getting around it.

soamsy commented 2 years ago

So, I tried it again, and the canonical way of cleaning up threads using Module.exit() in ffmpeg-wasm does work, but I was just not re-initializing everything after exit() in my first experiment (hence why flashcard recording would freeze on multiple runs). This required no changes to things like EXIT_RUNTIME=1, MODULARIZE=1, etc. in the ffmpeg.wasm-core compiler flags. You can use what's on master there and you should be able to clean up things if you exit after every run.

Only problem is: taking down the entire emscripten runtime and re-initializing it makes recording flashcards about 2x slower, which I don't want. There's probably a lot of speed benefits to having a giant pool of threads available every time ffmpeg runs, and I'd rather not sacrifice that just because 1 extra thread gets left hanging after recording every flashcard. I'd rather only deal with that one extra thread if possible.

soamsy commented 2 years ago

Ended up doing exactly that, and had to expose some Emscripten internals to deal with the extra PThread left hanging around. The latest release of the extension shouldn't have these freezing/speed issues anymore. If you see anything else feel free to let me know.