xjbeta / iina-plus

Extra danmaku support for iina. (iina 弹幕支持
GNU General Public License v3.0
1.13k stars 69 forks source link

[Fix] Resolve a crash while close multiple player windows simultaneously #225

Open doggy opened 3 months ago

doggy commented 3 months ago

HttpServer

        }, disconnected: { [weak self] session in
            Log("Websocket client disconnected.")
            self?.connectedItems.removeAll { $0.session == session
            }
            guard let items = self?.connectedItems else { return }
            self?.danmakus.removeAll { dm in
                let remove = !items.contains(where: { $0.url == dm.url })
                if remove {
                    dm.stop()
                }
                return remove
            }

The line which contains self?.danmakus leads a high probability app crash if you open more than 2 IINA windows and close them all simultaneously by pressing keyboard Q.

doggy commented 3 months ago

The fix PR is attached here. Already tested and verified on my M1 Mac.

The main reason for the crash is the multiple threads will send the disconnect event at same time but the list is not provide thread safety logic. There is a high probability of this happening when using the keyboard Q to exit multiple IINA windows.

As you might consider the crash also occurs when adding new elements to danmakus. Yes, in rare cases this can happen, so it is good to add this protection to loadNewDanmaku()

Please review my changes. If you have any questions, please reply and let me know. I will check this PR frequently recently.

BTW, I must say this is a very convenient app for watching live broadcasts! Use it every day and night! I am very grateful to the author for his continuous updates over the years!