td icon indicating copy to clipboard operation
td copied to clipboard

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

Open cdxf opened this issue 1 year ago • 5 comments

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?

cdxf avatar Aug 11 '24 04:08 cdxf

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.

levlam avatar Aug 11 '24 20:08 levlam

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 }

cdxf avatar Aug 12 '24 11:08 cdxf

  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.

levlam avatar Aug 12 '24 12:08 levlam

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 }

cdxf avatar Aug 13 '24 04:08 cdxf

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

levlam avatar Aug 13 '24 07:08 levlam