Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when fetching video information #79

Closed
shadone opened this issue Jan 9, 2024 · 3 comments
Closed

Crash when fetching video information #79

shadone opened this issue Jan 9, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@shadone
Copy link
Contributor

shadone commented Jan 9, 2024

What happened?

The crash occurs on YouTubePlayerWebView+Evaluate.swift:147 on line .store(in: &self.cancellables)

When it crashes, the execution happens in background thread, I believe the issue is concurrent access to shared variable cancellables.

I am not sure that's the best solution, but adding @MainActor attribute to getInformation() solves the problem for me

    #if compiler(>=5.5) && canImport(_Concurrency)
    /// Retrieve the YouTubePlayer Information
    @MainActor // <<<< this solves the problem for me.
    func getInformation() async throws -> Information {
        try await withCheckedThrowingContinuation { continuation in
            self.getInformation(completion: continuation.resume)
        }
    }
    #endif

What are the steps to reproduce?

Here is a piece of code showing how I use YouTubePLayerView

struct VideoView: View {
    let player: YouTubePlayer

    @State private var title: String?
    @State private var duration: Double?

    init(youtubeUrl: URL) {
        var config = YouTubePlayer.Configuration()
        config.fullscreenMode = .system
        config.useModestBranding = true
        config.playInline = false

        player = YouTubePlayer(source: .url(youtubeUrl.absoluteString), configuration: config)
    }

    var body: some View {
        VStack {
            YouTubePlayerView(player)
                .frame(height: 192)
            if let title {
                Text(title)
                    .multilineTextAlignment(.leading)
                    .padding(EdgeInsets(top: 32, leading: 16, bottom: 0, trailing: 16))
                    .frame(maxWidth: .infinity, alignment: .leading)
            }
            Button("\(duration ?? 0)", systemImage: "play.fill") { }
                .buttonStyle(.play)
                .padding(EdgeInsets(top: 0, leading: 16, bottom: 16, trailing: 16))
                .frame(maxWidth: .infinity, alignment: .leading)
        }
        .task { @MainActor in
            async let info = player.getInformation()
            async let metadata = player.getPlaybackMetadata()
            title = try? await metadata.title
            duration = try? await info.duration
        }
    }

What is the expected behavior?

Not crash would be great :)

@shadone shadone added the bug Something isn't working label Jan 9, 2024
shadone added a commit to shadone/YouTubePlayerKit that referenced this issue Jan 9, 2024
I've got a crash when using async functions - as they were executed in
background thread. See SvenTiigi#79
@SvenTiigi
Copy link
Owner

Hi @shadone,

Thanks for your detailed bug report 👍

A fix is now available on the develop branch.
Please let me know if this resolves your issue.

@shadone
Copy link
Contributor Author

shadone commented Jan 9, 2024

I believe that solves it! I cannot reproduce the crash any more. Thanks for such a quick reply!

@shadone shadone closed this as completed Jan 10, 2024
@SvenTiigi
Copy link
Owner

The fix is now available with the latest release 1.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants