Skip to content

Commit c4dd08b

Browse files
authored
Change connection task error handling. (#873)
1 parent f7a537a commit c4dd08b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Diff for: src/NATS.Client/Connection.cs

+14-7
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,24 @@ public virtual void open(Srv s, Options options)
388388
client.Client.DualMode = true;
389389

390390
var task = client.ConnectAsync(s.Url.Host, s.Url.Port);
391-
// avoid raising TaskScheduler.UnobservedTaskException if the timeout occurs first
392-
task.ContinueWith(t =>
391+
NATSConnectionException nce = null;
392+
try
393393
{
394-
GC.KeepAlive(t.Exception);
395-
close(client);
396-
}, TaskContinuationOptions.OnlyOnFaulted);
397-
if (!task.Wait(TimeSpan.FromMilliseconds(options.Timeout)))
394+
if (!task.Wait(TimeSpan.FromMilliseconds(options.Timeout)))
395+
{
396+
nce = new NATSConnectionException("timeout");
397+
}
398+
}
399+
catch (Exception e)
400+
{
401+
nce = new NATSConnectionException(e.Message);
402+
}
403+
404+
if (nce != null)
398405
{
399406
close(client);
400407
client = null;
401-
throw new NATSConnectionException("timeout");
408+
throw nce;
402409
}
403410

404411
client.NoDelay = false;

Diff for: src/Samples/JetStreamStarter/JetStreamStarter.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
// limitations under the License.
1313

1414
using System;
15+
using System.Threading;
1516
using NATS.Client;
1617
using NATS.Client.Internals;
18+
using NATS.Client.JetStream;
19+
using NATS.Client.KeyValue;
1720

1821
namespace NATSExamples
1922
{
@@ -42,7 +45,7 @@ static void Main(string[] args)
4245
{
4346
Console.WriteLine("Connection closed.");
4447
};
45-
48+
4649
Console.WriteLine($"Connecting to '{opts.Url}'");
4750

4851
using (IConnection c = new ConnectionFactory().CreateConnection(opts))

0 commit comments

Comments
 (0)