ilyalatt / Telega

C# Telegram MTProto Client
https://ilyalatt.github.io/Telega/
MIT License
58 stars 15 forks source link

Disconnecting after one or two hours #83

Open derodevil opened 2 years ago

derodevil commented 2 years ago
IDisposable KeepAlive(TelegramClient tg) => Observable
            .Timer(dueTime: TimeSpan.Zero, period: TimeSpan.FromSeconds(10))
            .Select(_ => Observable.FromAsync(() => tg.Call(new Telega.Rpc.Dto.Functions.Ping(pingId: 0))).Materialize())
            .Concat()
            .Subscribe();

The above snippet doesn't work. My winforms application gets disconnected after 1 to 2 hours inactivity.

[EDIT] I try my own custom ping every ten seconds. After approximately an hour it generates error as follows:

Telega.TgInternalException: Telega internal exception. Unhandled exception. See an inner exception. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 91.108.56.156:443
   at System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.TcpClient.EndConnect(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
   at Telega.Connect.TgConnectionEstablisher.<CreateTcpClient>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Telega.Connect.TgConnectionEstablisher.<EstablishConnection>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Telega.Connect.TgConnectionPool.<ReConnect>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Telega.Connect.TgBellhop.<ChangeConn>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Telega.Connect.TgBellhop.<CallWithReConnect>d__19`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Telega.Connect.TgBellhop.<CallWithMigration>d__20`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Telega.TaskWrapper.<Wrap>d__2`1.MoveNext()
   --- End of inner exception stack trace ---
   at Telega.TaskWrapper.<Wrap>d__2`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at TestTelegram.MainUi.<Ping>d__11.MoveNext()
ilyalatt commented 2 years ago

Hello. Looks like you use preview version of 0.5.0. Try to update the library.

derodevil commented 2 years ago

Is this code still needed or you have updated the library where it keeps alive without the code? IDisposable KeepAlive(TelegramClient tg) =>

ilyalatt commented 2 years ago

This code is still needed. It should be removed in ‘0.6.0’. On 15 Jan 2022, 12:03 +0100, derodevil @.***>, wrote:

Is this code still needed or you have update the library where it keeps alive without the code? IDisposable KeepAlive(TelegramClient tg) => — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

derodevil commented 2 years ago

I use the latest version 0.5.1 started at 17:45 and I let it idle for more than two hours. At 21:00 I send a text message and it couldn't receive any massage. I don't think the latest version keep it alive. My code looks like this:

var contacts = await tg.Contacts.GetContacts();
... some logic with `contacts`

KeepAlive(tg); //where the `KeepAlive` is the above snippet

tg.Updates.Stream.Subscribe(
    onNext: async n =>
    {
        try
        {
            ... my other code when receiving a message
        }
        catch (Exception ex)
        {
            WriteLog(ex.Message)
        }
    },
    onError: e =>
    {
        WriteLog(e.Message);
    }
);
ilyalatt commented 2 years ago

Hm. Can you subscribe to internal exceptions? Should look like tg.Updates.Exceptions.Subscribe(Console.WriteLine). KeepAlive snippet just ignores ping exceptions. Replace .Subscribe() with .Select(x => x.Exception).Where(x => x != null).Subscribe(Console.WriteLine). It should give more information for debugging.

derodevil commented 2 years ago

I had been running from 11pm to 7am and no exception was caught. I've checked the log file since I put the error message to a file and nothing there.

[EDIT] I think I find the problem here where the tg.Updates and/or KeepAlive method should not be placed inside a multi threaded method. I tried this approach and it keeps alive for more than two hours

ilyalatt commented 2 years ago

Yet another reason to move KeepAlive into the library internals

derodevil commented 2 years ago

Well, I'm waiting for the next release

derodevil commented 2 years ago

Do you have any updates for this issue?

ilyalatt commented 2 years ago

Do you have any updates for this issue?

Not yet. I had started the refactoring and abandoned it. I do not have time for Telega now and the next few months. I can review and merge a PR however.

derodevil commented 2 years ago

Hi.. I'm still waiting 💯