Skip to content

Commit

Permalink
[#106] Unobserved exception was rethrown by the finalizer thread
Browse files Browse the repository at this point in the history
  • Loading branch information
xinchen10 committed Jun 8, 2018
1 parent cf00057 commit 8366a43
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
5 changes: 5 additions & 0 deletions Microsoft.Azure.Amqp/Amqp/FaultTolerantAmqpObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public bool TryGetOpenedObject(out T openedAmqpObject)
return openedAmqpObject != null;
}

protected override bool IsValid(T value)
{
return value.State == AmqpObjectState.Opened;
}

protected override async Task<T> OnCreateAsync(TimeSpan timeout)
{
T amqpObject = await this.createObjectAsync(timeout).ConfigureAwait(false);
Expand Down
51 changes: 26 additions & 25 deletions Microsoft.Azure.Amqp/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,39 @@ public async Task<TValue> GetOrCreateAsync(TimeSpan timeout)

if (this.TryGet(out tcs))
{
return await tcs.Task.ConfigureAwait(false);
TValue current = await tcs.Task.ConfigureAwait(false);
if (this.IsValid(current))
{
return current;
}

this.Invalidate(current);
}

tcs = new TaskCompletionSource<TValue>();
if (this.TrySet(tcs))
{
this.CreateValue(tcs, timeoutHelper.RemainingTime()).Fork();
try
{
TValue value = await this.OnCreateAsync(timeout).ConfigureAwait(false);
tcs.SetResult(value);
}
catch (Exception ex) when (!Fx.IsFatal(ex))
{
this.TryRemove();
tcs.SetException(ex);
}

return await tcs.Task;
}
}

if (this.disposed)
{
throw new ObjectDisposedException(this.GetType().Name);
}
else
{
throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "Timed out trying to create {0}", this.GetType().Name));
}

throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "Creation of {0} did not complete in {1} milliseconds.", typeof(TValue).Name, timeout.TotalMilliseconds));
}

protected void Invalidate(TValue instance)
Expand All @@ -128,6 +143,11 @@ protected void Invalidate(TValue instance)
}
}

protected virtual bool IsValid(TValue value)
{
return true;
}

protected abstract Task<TValue> OnCreateAsync(TimeSpan timeout);

protected abstract void OnSafeClose(TValue value);
Expand Down Expand Up @@ -169,24 +189,5 @@ bool TryRemove()
}
}
}

async Task CreateValue(TaskCompletionSource<TValue> tcs, TimeSpan timeout)
{
try
{
TValue value = await OnCreateAsync(timeout).ConfigureAwait(false);
tcs.SetResult(value);

if (this.disposed)
{
OnSafeClose(value);
}
}
catch (Exception ex) when (!Fx.IsFatal(ex))
{
this.TryRemove();
tcs.SetException(ex);
}
}
}
}

0 comments on commit 8366a43

Please sign in to comment.