tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

How to Set a Timeout for TdApi.DownloadFile in Java with tdlib? #3017

Closed cdxf closed 1 week ago

cdxf commented 2 months ago

I'm working with tdlib in Java and using the TdApi.DownloadFile method with synchronous = true to download files. However, I've encountered an issue where the process sometimes gets stuck indefinitely.

Is there a way to set a timeout for this API call to prevent it from hanging forever? If not, what would be the best approach to handle this situation effectively?

levlam commented 2 months ago

If you asked for synchronous download then you can't set a timeout for the request. To be able to cancel request you must use asynchronous file download and canceled it with TdApi.CancelDownloadFile.

By the way, calls to TDLib never hanging. The response to Client.send is always returned immediately and synchronous waiting for callback invocation is almost always a bug.

cdxf commented 2 months ago

Thank for your answer.

For file download requests with specified offset and limit:

  1. How can I determine if the partial download is complete? Given that isDownloadingCompleted = false when the entire file isn't yet downloaded, should I check the downloadOffset, downloadedSize, and expectedSize values returned from updateFile to confirm completion?

  2. Is it possible to send multiple download requests for the same file with different offsets and limits concurrently, or must I wait for each request to complete before initiating another? I've attempted to send download requests simultaneously with varying offsets and limits, but they failed. What could be the reason for this issue?

    local = LocalFile { path = "" canBeDownloaded = true canBeDeleted = false isDownloadingActive = true isDownloadingCompleted = false downloadOffset = 83886083 downloadedPrefixSize = 0 downloadedSize = 0 }

levlam commented 2 months ago
  1. The downloading isn't active whenever isDownloadingActive changes from true to false. Before that the last request is still running. Whenever it changes to false, you can check whether requested chunk is present. If it isn't then download was canceled or has failed.
  2. No, there can be only one concurrent download request for a file. There is no point in multiple requests: the user can watch the video only in one place.
cdxf commented 2 months ago

I do not receive any UpdateFile with isDownloadingActive = false. Only when I close the client, then I receive the last UpdateFile with isDownloadingActive = false

I tried to download 1 file with 2 requests: (offset 2 limit 83886081) and (offset 83886083 limit 2000) one after another : Here is the responses:

UpdateFile: local = LocalFile { path = "D:\project\td\example\java\tdlib-PGSMcP0c\temp_1a215d" canBeDownloaded = true canBeDeleted = true isDownloadingActive = true isDownloadingCompleted = false downloadOffset = 2 downloadedPrefixSize = 83951614 downloadedSize = 83951616 } Other responses with isDownloadingCompleted = false is omitted for brevity UpdateFile: local = LocalFile { path = "D:\project\td\example\java\tdlib-PGSMcP0c\temp_1a215d" canBeDownloaded = true canBeDeleted = true isDownloadingActive = true isDownloadingCompleted = false downloadOffset = 83886083 downloadedPrefixSize = 65533 downloadedSize = 83951616 }

I then do not receive any response further. Then I Call TdApi.Close and received AuthorizationStateClosing update

Then receive the last UpdateFile local = LocalFile { path = "D:\project\td\example\java\tdlib-PGSMcP0c\temp_1a215d" canBeDownloaded = true canBeDeleted = true isDownloadingActive = false isDownloadingCompleted = false downloadOffset = 83886083 downloadedPrefixSize = 65533 downloadedSize = 43384832 }

levlam commented 2 months ago

Could you send full log to https://t.me/tdlib_bot?