Skip to content

Commit

Permalink
Fixed PromiseMethodBuilders for async Promise functions with the IL2C…
Browse files Browse the repository at this point in the history
…PP compiler.

Ignore Internal functions and PromiseYielder.Routines in causality traces.
Include causality trace from invalid returned promise.
  • Loading branch information
Tim committed Sep 24, 2020
1 parent 473730a commit 43b7aee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 68 deletions.
5 changes: 3 additions & 2 deletions InternalShared/HelperFunctionsInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Proto.Utils;

#if PROMISE_DEBUG
using System.Diagnostics;
using System.Linq;
#endif

Expand All @@ -23,6 +23,7 @@ namespace Proto.Promises
/// <summary>
/// Members of this type are meant for INTERNAL USE ONLY! Do not use in user code! Use the documented public APIs.
/// </summary>
[DebuggerNonUserCode]
internal static partial class Internal
{
public static bool invokingRejected;
Expand Down Expand Up @@ -153,7 +154,7 @@ public static string FormatStackTrace(IEnumerable<StackTrace> stackTraces)
var trace = stackFrames
.Where(frame =>
{
// Ignore DebuggerStepThrough and DebuggerHidden and DebuggerNonUserCode.
// Ignore DebuggerNonUserCode and DebuggerHidden.
var methodType = frame.GetMethod();
return !methodType.IsDefined(typeof(DebuggerNonUserCodeAttribute), false)
&& !methodType.DeclaringType.IsDefined(typeof(DebuggerNonUserCodeAttribute), false)
Expand Down
80 changes: 16 additions & 64 deletions Promises/Internal/AsyncAwaitInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,23 @@ public struct PromiseMethodBuilder
private Action _continuation;

[DebuggerHidden]
public Promise Task { get; private set; }
public Promise Task { get { return _deferred.Promise; } }

[DebuggerHidden]
public static PromiseMethodBuilder Create()
{
return new PromiseMethodBuilder();
return new PromiseMethodBuilder()
{
_deferred = Promise.Deferred.New()
};
}

[DebuggerHidden]
public void SetException(Exception exception)
{
if (exception is OperationCanceledException)
{
if (!_deferred.IsValid)
{
Task = Promise.Canceled(exception);
}
else
{
((Internal.ICancelDelegate) _deferred.Promise).Invoke(Internal.CreateCancelContainer(ref exception));
}
((Internal.ICancelDelegate) _deferred.Promise).Invoke(Internal.CreateCancelContainer(ref exception));
}
else
{
Expand All @@ -206,32 +202,14 @@ public void SetException(Exception exception)
#endif
exception = new InvalidOperationException("RethrowException is only valid in promise onRejected callbacks.", stacktrace);
}
if (!_deferred.IsValid)
{
Task = Promise.Rejected(exception);
}
else
{
_deferred.Reject(exception);
}
_deferred.Reject(exception);
}
_deferred = default;
_continuation = null;
}

[DebuggerHidden]
public void SetResult()
{
if (!_deferred.IsValid)
{
Task = Promise.Resolved();
}
else
{
_deferred.Resolve();
_deferred = default;
_continuation = null;
}
_deferred.Resolve();
}

[DebuggerHidden]
Expand Down Expand Up @@ -269,8 +247,6 @@ private void SetContinuation<TStateMachine>(ref TStateMachine stateMachine)
{
if (_continuation is null)
{
_deferred = Promise.Deferred.New();
Task = _deferred.Promise;
_continuation = stateMachine.MoveNext;
}
}
Expand All @@ -286,27 +262,23 @@ public struct PromiseMethodBuilder<T>
private Action _continuation;

[DebuggerHidden]
public Promise<T> Task { get; private set; }
public Promise<T> Task { get { return _deferred.Promise; } }

[DebuggerHidden]
public static PromiseMethodBuilder<T> Create()
{
return new PromiseMethodBuilder<T>();
return new PromiseMethodBuilder<T>()
{
_deferred = Promise<T>.Deferred.New()
};
}

[DebuggerHidden]
public void SetException(Exception exception)
{
if (exception is OperationCanceledException)
{
if (!_deferred.IsValid)
{
Task = Promise.Canceled<T, Exception>(exception);
}
else
{
((Internal.ICancelDelegate) _deferred.Promise).Invoke(Internal.CreateCancelContainer(ref exception));
}
((Internal.ICancelDelegate) _deferred.Promise).Invoke(Internal.CreateCancelContainer(ref exception));
}
else
{
Expand All @@ -319,32 +291,14 @@ public void SetException(Exception exception)
#endif
exception = new InvalidOperationException("RethrowException is only valid in promise onRejected callbacks.", stacktrace);
}
if (!_deferred.IsValid)
{
Task = Promise.Rejected<T, Exception>(exception);
}
else
{
_deferred.Reject(exception);
}
_deferred.Reject(exception);
}
_deferred = default;
_continuation = null;
}

[DebuggerHidden]
public void SetResult(T result)
{
if (!_deferred.IsValid)
{
Task = Promise.Resolved(result);
}
else
{
_deferred.Resolve(result);
_deferred = default;
_continuation = null;
}
_deferred.Resolve(result);
}

[DebuggerHidden]
Expand Down Expand Up @@ -382,8 +336,6 @@ private void SetContinuation<TStateMachine>(ref TStateMachine stateMachine)
{
if (_continuation is null)
{
_deferred = Promise<T>.Deferred.New();
Task = _deferred.Promise;
_continuation = stateMachine.MoveNext;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Promises/Internal/DebugInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ partial void ValidateReturn(Promise other)
// Validate returned promise as not disposed.
if (IsDisposed(other._valueOrPrevious))
{
throw new InvalidReturnException("A disposed promise was returned.");
throw new InvalidReturnException("A disposed promise was returned.", Internal.GetFormattedStacktrace(other));
}

// A promise cannot wait on itself.
Expand All @@ -92,7 +92,7 @@ partial void ValidateReturn(Promise other)
{
if (prev == this)
{
throw new InvalidReturnException("Circular Promise chain detected.", ((Internal.ITraceable) other).Trace.ToString());
throw new InvalidReturnException("Circular Promise chain detected.", Internal.GetFormattedStacktrace(other));
}
prev.BorrowPassthroughs(ref passThroughs);
}
Expand Down
2 changes: 2 additions & 0 deletions Promises/Unity/PromiseYielder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public static void ClearPooledObjects()
}
}

[System.Diagnostics.DebuggerNonUserCode]
private class Routine : IEnumerator, ILinked<Routine>
{
Routine ILinked<Routine>.Next { get; set; }
Expand Down Expand Up @@ -344,6 +345,7 @@ void Complete()
void IEnumerator.Reset() { }
}

[System.Diagnostics.DebuggerNonUserCode]
private class Routine<T> : IEnumerator, ILinked<Routine<T>>
{
Routine<T> ILinked<Routine<T>>.Next { get; set; }
Expand Down

0 comments on commit 43b7aee

Please sign in to comment.