Skip to content

Commit 043fccd

Browse files
committed
chore: ensure download client session
1 parent bc56e0a commit 043fccd

1 file changed

Lines changed: 41 additions & 11 deletions

File tree

TelegramDownloader/Data/TelegramService.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,53 @@ private static bool IsSessionInvalidException(Exception ex)
210210
}
211211

212212
/// <summary>
213-
/// Handles invalid session during download - resets download client and returns main client
213+
/// Handles invalid session during download - tries to re-authenticate, if not possible resets and uses main client
214214
/// </summary>
215-
private WTelegram.Client HandleInvalidDownloadSession(Exception ex)
215+
private async Task<WTelegram.Client> HandleInvalidDownloadSessionAsync(Exception ex)
216216
{
217-
_logger.LogWarning(ex, "Download client session is invalid, resetting and using main client");
217+
_logger.LogWarning(ex, "Download client error detected, checking if session is still valid");
218218
downloadClientMut.WaitOne();
219219
try
220220
{
221-
downloadClient?.Dispose();
222-
downloadClient = null;
223-
DeleteDownloadSession();
221+
if (downloadClient == null)
222+
{
223+
return client;
224+
}
225+
226+
// Try to verify if the session is still valid by attempting login
227+
try
228+
{
229+
var loginResult = await downloadClient.Login(null);
230+
if (loginResult == null)
231+
{
232+
// Session is still valid (already authenticated), might have been a transient error
233+
_logger.LogInformation("Download client session is still valid, continuing to use it");
234+
return downloadClient;
235+
}
236+
else
237+
{
238+
// Login returned something (phone_number, verification_code, etc.) - session needs re-auth
239+
_logger.LogWarning("Download client session requires re-authentication (returned: {Result}), switching to main client", loginResult);
240+
downloadClient?.Dispose();
241+
downloadClient = null;
242+
DeleteDownloadSession();
243+
return client;
244+
}
245+
}
246+
catch (Exception loginEx)
247+
{
248+
// Login attempt failed - session is definitely invalid
249+
_logger.LogWarning(loginEx, "Download client login check failed, session is invalid, switching to main client");
250+
downloadClient?.Dispose();
251+
downloadClient = null;
252+
DeleteDownloadSession();
253+
return client;
254+
}
224255
}
225256
finally
226257
{
227258
downloadClientMut.ReleaseMutex();
228259
}
229-
return client;
230260
}
231261

232262
/// <summary>
@@ -949,8 +979,8 @@ public async Task<Byte[]> DownloadFileStream(Message message, long offset, int l
949979
}
950980
catch (Exception ex) when (IsSessionInvalidException(ex))
951981
{
952-
_logger.LogWarning(ex, "Download client session invalid, switching to main client");
953-
dlClient = HandleInvalidDownloadSession(ex);
982+
_logger.LogWarning(ex, "Download client session error, verifying session validity");
983+
dlClient = await HandleInvalidDownloadSessionAsync(ex);
954984
file = await dlClient.Upload_GetFile(location, currentOffset, limit: totalDownloadBytes);
955985
}
956986
catch (Exception ex)
@@ -1118,8 +1148,8 @@ public async Task<Stream> DownloadFileAndReturnWithOffset(ChatMessages message,
11181148
}
11191149
catch (Exception ex) when (IsSessionInvalidException(ex))
11201150
{
1121-
_logger.LogWarning(ex, "Download client session invalid, switching to main client");
1122-
dlClient = HandleInvalidDownloadSession(ex);
1151+
_logger.LogWarning(ex, "Download client session error, verifying session validity");
1152+
dlClient = await HandleInvalidDownloadSessionAsync(ex);
11231153
file = await dlClient.Upload_GetFile(location, currentOffset, limit: chunkSize);
11241154
}
11251155
catch (Exception ex)

0 commit comments

Comments
 (0)