Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SynchronizationContext should not be restored when the stream is completed in WebGL #439

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async void ConnectAndForget()
var task = client.__ConnectAndSubscribeAsync(receiver, CancellationToken.None);
try
{
await task;
await task.ConfigureAwait(false);
}
catch (Exception e)
{
Expand All @@ -64,7 +64,7 @@ async void ConnectAndForget()
await client.__ConnectAndSubscribeAsync(receiver, cancellationToken).ConfigureAwait(false);
return (TStreamingHub)(object)client;
}

private static StreamingHubClientBase<TStreamingHub, TReceiver> CreateClient<TStreamingHub, TReceiver>(CallInvoker callInvoker, TReceiver receiver, string host, CallOptions option, MessagePackSerializerOptions serializerOptions, IMagicOnionClientLogger logger)
where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ async Task StartSubscribe(SynchronizationContext syncContext, Task<bool> firstMo
{
try
{
#if !UNITY_WEBGL
// set syncContext before await
// NOTE: If restore SynchronizationContext in WebGL environment, a continuation will not be executed inline and will be stuck.
if (syncContext != null && SynchronizationContext.Current == null)
{
SynchronizationContext.SetSynchronizationContext(syncContext);
}
await DisposeAsyncCore(false);
#endif
await DisposeAsyncCore(false).ConfigureAwait(false);
}
finally
{
Expand Down Expand Up @@ -268,7 +271,7 @@ byte[] BuildMessage()
}

var v = BuildMessage();
using (await asyncLock.LockAsync())
using (await asyncLock.LockAsync().ConfigureAwait(false))
{
await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false);
}
Expand Down Expand Up @@ -310,7 +313,7 @@ byte[] BuildMessage()
await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false);
}

return await tcs.Task; // wait until server return response(or error). if connection was closed, throws cancellation from DisposeAsyncCore.
return await tcs.Task.ConfigureAwait(false); // wait until server return response(or error). if connection was closed, throws cancellation from DisposeAsyncCore.
}

void ThrowIfDisposed()
Expand Down Expand Up @@ -340,7 +343,7 @@ async Task DisposeAsyncCore(bool waitSubscription)

try
{
await connection.RequestStream.CompleteAsync();
await connection.RequestStream.CompleteAsync().ConfigureAwait(false);
}
catch { } // ignore error?
finally
Expand All @@ -353,7 +356,7 @@ async Task DisposeAsyncCore(bool waitSubscription)
{
if (subscription != null)
{
await subscription;
await subscription.ConfigureAwait(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static T Create<T>(CallInvoker invoker, MessagePackSerializerOptions seri
if (ctor == null)
{
#if ((ENABLE_IL2CPP && !UNITY_EDITOR) || NET_STANDARD_2_0)
throw new InvalidOperationException("Does not registered client factory, dynamic code generation is not supported on IL2CPP. Please use code generator(moc).");
throw new InvalidOperationException($"Unable to find a client factory of type '{typeof(T)}'. If the application is running on IL2CPP or AOT, dynamic code generation is not supported. Please use the code generator (moc).");
#else
var t = DynamicClientBuilder<T>.ClientType;
return (T)Activator.CreateInstance(t, invoker, serializerOptions, clientFilters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async void ConnectAndForget()
var task = client.__ConnectAndSubscribeAsync(receiver, CancellationToken.None);
try
{
await task;
await task.ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ async Task StartSubscribe(SynchronizationContext syncContext, Task<bool> firstMo
{
try
{
#if !UNITY_WEBGL
// set syncContext before await
// NOTE: If restore SynchronizationContext in WebGL environment, a continuation will not be executed inline and will be stuck.
if (syncContext != null && SynchronizationContext.Current == null)
{
SynchronizationContext.SetSynchronizationContext(syncContext);
}
await DisposeAsyncCore(false);
#endif
await DisposeAsyncCore(false).ConfigureAwait(false);
}
finally
{
Expand Down Expand Up @@ -268,7 +271,7 @@ byte[] BuildMessage()
}

var v = BuildMessage();
using (await asyncLock.LockAsync())
using (await asyncLock.LockAsync().ConfigureAwait(false))
{
await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false);
}
Expand Down Expand Up @@ -310,7 +313,7 @@ byte[] BuildMessage()
await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false);
}

return await tcs.Task; // wait until server return response(or error). if connection was closed, throws cancellation from DisposeAsyncCore.
return await tcs.Task.ConfigureAwait(false); // wait until server return response(or error). if connection was closed, throws cancellation from DisposeAsyncCore.
}

void ThrowIfDisposed()
Expand Down Expand Up @@ -340,7 +343,7 @@ async Task DisposeAsyncCore(bool waitSubscription)

try
{
await connection.RequestStream.CompleteAsync();
await connection.RequestStream.CompleteAsync().ConfigureAwait(false);
}
catch { } // ignore error?
finally
Expand All @@ -353,7 +356,7 @@ async Task DisposeAsyncCore(bool waitSubscription)
{
if (subscription != null)
{
await subscription;
await subscription.ConfigureAwait(false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/MagicOnion.Client/StreamingHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async void ConnectAndForget()
var task = client.__ConnectAndSubscribeAsync(receiver, CancellationToken.None);
try
{
await task;
await task.ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
13 changes: 8 additions & 5 deletions src/MagicOnion.Client/StreamingHubClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ async Task StartSubscribe(SynchronizationContext syncContext, Task<bool> firstMo
{
try
{
#if !UNITY_WEBGL
// set syncContext before await
// NOTE: If restore SynchronizationContext in WebGL environment, a continuation will not be executed inline and will be stuck.
if (syncContext != null && SynchronizationContext.Current == null)
{
SynchronizationContext.SetSynchronizationContext(syncContext);
}
await DisposeAsyncCore(false);
#endif
await DisposeAsyncCore(false).ConfigureAwait(false);
}
finally
{
Expand Down Expand Up @@ -268,7 +271,7 @@ byte[] BuildMessage()
}

var v = BuildMessage();
using (await asyncLock.LockAsync())
using (await asyncLock.LockAsync().ConfigureAwait(false))
{
await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false);
}
Expand Down Expand Up @@ -310,7 +313,7 @@ byte[] BuildMessage()
await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false);
}

return await tcs.Task; // wait until server return response(or error). if connection was closed, throws cancellation from DisposeAsyncCore.
return await tcs.Task.ConfigureAwait(false); // wait until server return response(or error). if connection was closed, throws cancellation from DisposeAsyncCore.
}

void ThrowIfDisposed()
Expand Down Expand Up @@ -340,7 +343,7 @@ async Task DisposeAsyncCore(bool waitSubscription)

try
{
await connection.RequestStream.CompleteAsync();
await connection.RequestStream.CompleteAsync().ConfigureAwait(false);
}
catch { } // ignore error?
finally
Expand All @@ -353,7 +356,7 @@ async Task DisposeAsyncCore(bool waitSubscription)
{
if (subscription != null)
{
await subscription;
await subscription.ConfigureAwait(false);
}
}

Expand Down