Skip to content

Commit 27f1fa6

Browse files
committed
Improve exception catching in the background tasks
1 parent eec0e5d commit 27f1fa6

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

WalletConnectSharp.Core/Controllers/HeartBeat.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using WalletConnectSharp.Common.Logging;
12
using WalletConnectSharp.Core.Interfaces;
23

34
namespace WalletConnectSharp.Core.Controllers
@@ -76,7 +77,14 @@ public Task InitAsync(CancellationToken cancellationToken = default)
7677
{
7778
while (!token.IsCancellationRequested)
7879
{
79-
Pulse();
80+
try
81+
{
82+
Pulse();
83+
}
84+
catch (Exception ex)
85+
{
86+
WCLogger.LogError(ex);
87+
}
8088

8189
await Task.Delay(Interval, token);
8290
}

WalletConnectSharp.Core/Controllers/Relayer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ protected virtual async void OnProviderPayload(string payloadJson)
277277

278278
protected virtual async Task<bool> ShouldIgnoreMessageEvent(MessageEvent messageEvent)
279279
{
280-
if (!(await Subscriber.IsSubscribed(messageEvent.Topic))) return true;
280+
var isSubscribed = await Subscriber.IsSubscribed(messageEvent.Topic);
281+
if (!isSubscribed)
282+
{
283+
return true;
284+
}
281285

282286
var exists = Messages.Has(messageEvent.Topic, messageEvent.Message);
283287
return exists;

WalletConnectSharp.Core/Controllers/Subscriber.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -522,31 +522,43 @@ public async Task Unsubscribe(string topic, UnsubscribeOptions opts = null)
522522
/// Determines whether the given topic is subscribed or not
523523
/// </summary>
524524
/// <param name="topic">The topic to check</param>
525+
/// <param name="cancellationToken">A token to cancel the operation.</param>
525526
/// <returns>Return true if the topic is subscribed, false otherwise</returns>
526-
public Task<bool> IsSubscribed(string topic)
527+
public async Task<bool> IsSubscribed(string topic, CancellationToken cancellationToken = default)
527528
{
528-
if (Topics.Contains(topic)) return Task.FromResult(true);
529-
530-
return Task.Run(async delegate()
529+
if (Topics.Contains(topic))
531530
{
532-
var startTime = DateTimeOffset.Now.ToUnixTimeSeconds();
531+
return true;
532+
}
533+
534+
var startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
535+
const int timeoutMilliseconds = 5_000;
536+
const int delayMilliseconds = 20;
533537

534-
while (true)
538+
try
539+
{
540+
while (!cancellationToken.IsCancellationRequested)
535541
{
536542
if (!pending.ContainsKey(topic) && Topics.Contains(topic))
537543
{
538544
return true;
539545
}
540546

541-
var currentTime = DateTimeOffset.Now.ToUnixTimeSeconds();
542-
if (currentTime - startTime >= 5)
547+
var elapsedMilliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime;
548+
if (elapsedMilliseconds >= timeoutMilliseconds)
543549
{
544550
return false;
545551
}
546552

547-
await Task.Delay(20);
553+
await Task.Delay(delayMilliseconds, cancellationToken);
548554
}
549-
});
555+
}
556+
catch (OperationCanceledException)
557+
{
558+
return false;
559+
}
560+
561+
return false;
550562
}
551563

552564
protected virtual async Task<string[]> RpcBatchSubscribe(string[] topics, ProtocolOptions relay)

WalletConnectSharp.Core/Interfaces/ISubscriber.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public interface ISubscriber : IModule
7171
/// Determines whether the given topic is subscribed or not
7272
/// </summary>
7373
/// <param name="topic">The topic to check</param>
74+
/// /// <param name="cancellationToken">A token to cancel the operation.</param>
7475
/// <returns>Return true if the topic is subscribed, false otherwise</returns>
75-
public Task<bool> IsSubscribed(string topic);
76+
public Task<bool> IsSubscribed(string topic, CancellationToken cancellationToken = default);
7677
}
7778
}

0 commit comments

Comments
 (0)