Core: avoid possible infinite loop if switching to same DC

People are reporting that under some circumstances, an infinite
loop could happen when TLSharp tries to handle a reconnection to
a different DC, if the DC instructed to be used is the same as
the one that was used in the last connection.

Not sure how could this happen (although the analysis present in
this github issue [1] might help understand it), but this commit
helps to make TLSharp fail fast (with an exception) instead of an
infinite loop from now on, which will help avoiding people file
issues such as [2] and [3] and instead maybe file a proper bug
report easier to understand, to try to fix the underlying root
cause.

[1] https://github.com/sochix/TLSharp/issues/719
[2] https://github.com/sochix/TLSharp/issues/803
[3] https://github.com/sochix/TLSharp/issues/839
This commit is contained in:
Andres G. Aragoneses 2019-02-10 13:53:46 +01:00
parent d04ddb1e0e
commit 60a3c62357
4 changed files with 39 additions and 11 deletions

View file

@ -45,7 +45,7 @@ namespace TLSharp.Core
_handler = handler;
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
_transport = new TcpTransport(_session.DataCenter.Address, _session.DataCenter.Port, _handler);
}
public async Task ConnectAsync(bool reconnect = false)
@ -90,10 +90,10 @@ namespace TLSharp.Core
}
var dc = dcOptions.First(d => d.Id == dcId);
var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port);
_transport = new TcpTransport(dc.IpAddress, dc.Port, _handler);
_session.ServerAddress = dc.IpAddress;
_session.Port = dc.Port;
_session.DataCenter = dataCenter;
await ConnectAsync(true);
@ -121,6 +121,12 @@ namespace TLSharp.Core
}
catch(DataCenterMigrationException e)
{
if (_session.DataCenter.DataCenterId.HasValue &&
_session.DataCenter.DataCenterId.Value == e.DC)
{
throw new Exception($"Telegram server replied requesting a migration to DataCenter {e.DC} when this connection was already using this DataCenter", e);
}
await ReconnectToDcAsync(e.DC);
// prepare the request for another try
request.ConfirmReceived = false;