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

Torrent completed event before torrent completed #4

Open
mirceaciu opened this issue Apr 10, 2019 · 7 comments
Open

Torrent completed event before torrent completed #4

mirceaciu opened this issue Apr 10, 2019 · 7 comments

Comments

@mirceaciu
Copy link

First some context on my setup:
I use OpenTracker (tracker) and Transmission(seeder) as docker containers on an cloud. I upload video files to that cloud and add them as torrents to Transmission which will start seeding. I get a magnet url of that torrent and sent it to my android app that uses this project.

I am using a singleton instance of this torrent module for a React Native bridge(less important)

I implemented the events interface

init {
        // Init using context argument
        this.reactContext = reactContext
        this.torrentSession = TorrentSession(torrentSessionOptions)

        this.torrentSession.listener = object : TorrentSessionListener {
            override fun onAddTorrent(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "started a torrent ${torrentHandle.name()}")
            }

            override fun onTorrentFinished(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "torrent completed ${torrentHandle.name()}")
            }

            override fun onPieceFinished(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "down: ${torrentSessionStatus.bytesDownloaded} of ${torrentSessionStatus.bytesWanted}")
                Log.i(thorLoggerName, "progress ${torrentSessionStatus.progress}")
            }
        }
    }

When I add a new torrent to download the onAddTorrent fires.

After that I will expect to see some logging from the onPieceFinished event then eventually a onTorrentFinished.

What happens is that:

  • onAddTorrent fires after torrent is started;
  • onTorrentFinished fires as soon as Transmissions sees the peer (soon after onAddTorrent);
  • some time passes;
  • torrent starts downloading and multiple onPieceFinished fire;
  • eventually download completes with another onTorrentFinished;

On first onTorrentFinished file has 0mb. On second onTorrentFinished file was properly downloaded and can be played back.

To overcome this I had to do a double check in the onTorrentFinished event

            override fun onTorrentFinished(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "torrent completed ${torrentHandle.name()}")

                if(torrentSessionStatus.bytesDownloaded == torrentSessionStatus.bytesWanted){
                    // know for sure that torrent downloaded
                    emit("fileName", torrentHandle.name(), "thor_completed")
                }
            }

Is this expected? Note that I am using magnet torrents.

@masterwok
Copy link
Owner

Hi @mirceaciu,

Thanks for reporting this. I don't think this behavior should be expected. According to the documentation, https://libtorrent.org/manual.html the torrent_finished_alert should only be emitted once, on download completion:

torrent_finished_alert
This alert is generated when a torrent switches from being a downloader to a seed. It will only be generated once per torrent. It contains a torrent_handle to the torrent in question.

I suspect the problem is occurring, here. I vaguely recall adding the call to resume in relation to downloads started using magnet links (it's been awhile since I've worked on this library). This might be triggering the finished alert when the torrent is added.

I'm glad you were able to find a low cost work-around. I will try to find time to do some research and figure out what's going on sometime over the weekend. If you figure out what's causing it sooner than that, pull requests are always welcome.

Are you able to reproduce the issue when using a torrent file?

@mirceaciu
Copy link
Author

Unfortunately my Java/Kotlin skills are limited to basics as I just started using this language so it's unlikely that I will be able to do PR.

I will test using torrent files and maybe some other torrent providers (other trackers and stuff). Maybe that will help.

@masterwok
Copy link
Owner

It seems that this issue occurs for downloads started with both a torrent and a magnet.

Without the call call to the resume method both fail to start. I feel like this call to resume on torrent added shouldn't be required and that I'm doing something wrong.

I've opened a ticket, here to try to get some insight as to what might be causing this and why downloads aren't starting as expected.

@sidsaxena0
Copy link

Hi, i am facing similar issue and when i try to resume a torrent which was partially downloaded earlier, instead of resuming or restarting it just don't do anything however i see method onTorrentResumed method is getting called. Is this happening because of this issue or i am doing something wrong.
I just start torrent normally but it won't work.

  torrentSession
                    .get()
                    ?.start(ExoApplication.getContext(), magnetUri)

Please help if there is any workaround for this.

@masterwok
Copy link
Owner

@sidsaxena0 What you're seeing doesn't seem related to this issue. Are you using a magnet or torrent file to start and resume the download? Do you have your torrent session in a foreground service? Does the demo application included in this repository successfully resume for you?

@sidsaxena0
Copy link

@masterwok I am using magnet link for start and resume download and yes demo application of the project is working fine. Even if i kill the app it starts again from the point where it left but not in case of my code. Can you please help?

@ghost
Copy link

ghost commented Aug 31, 2019

@sidsaxena0 onTorrentResume() is called once when a torrent is added to a session or when you resume downloading a torrent after you have paused it. This can help you better: https://libtorrent.org/reference-Core.html#save_resume_data()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants