Skip to content

Commit

Permalink
Merge pull request #19325 from unoplatform/mergify/bp/release/stable/…
Browse files Browse the repository at this point in the history
…5.6/pr-19319

chore(devSrv): Improve reliability of connection by catching generic errors (backport #19319)
  • Loading branch information
jeromelaban authored Jan 25, 2025
2 parents 1b3e705 + c915681 commit 9150366
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 70 deletions.
60 changes: 35 additions & 25 deletions src/Uno.UI.RemoteControl.Host/RemoteControlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,46 +146,56 @@ public async Task RunAsync(WebSocket socket, CancellationToken ct)

while (await WebSocketHelper.ReadFrame(socket, ct) is Frame frame)
{
if (frame.Scope == "RemoteControlServer")
try
{
if (frame.Name == ProcessorsDiscovery.Name)
if (frame.Scope == "RemoteControlServer")
{
await ProcessDiscoveryFrame(frame);
continue;
}
if (frame.Name == ProcessorsDiscovery.Name)
{
await ProcessDiscoveryFrame(frame);
continue;
}

if (frame.Name == KeepAliveMessage.Name)
{
await ProcessPingFrame(frame);
continue;
if (frame.Name == KeepAliveMessage.Name)
{
await ProcessPingFrame(frame);
continue;
}
}
}

if (_processors.TryGetValue(frame.Scope, out var processor))
{
if (this.Log().IsEnabled(LogLevel.Debug))
if (_processors.TryGetValue(frame.Scope, out var processor))
{
this.Log().LogDebug("Received Frame [{Scope} / {Name}] to be processed by {processor}", frame.Scope, frame.Name, processor);
}
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug("Received Frame [{Scope} / {Name}] to be processed by {processor}", frame.Scope, frame.Name, processor);
}

try
{
DevServerDiagnostics.Current = DiagnosticsSink.Instance;
await processor.ProcessFrame(frame);
try
{
DevServerDiagnostics.Current = DiagnosticsSink.Instance;
await processor.ProcessFrame(frame);
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError(e, "Failed to process frame [{Scope} / {Name}]", frame.Scope, frame.Name);
}
}
}
catch (Exception e)
else
{
if (this.Log().IsEnabled(LogLevel.Error))
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogError(e, "Failed to process frame [{Scope} / {Name}]", frame.Scope, frame.Name);
this.Log().LogDebug("Unknown Frame [{Scope} / {Name}]", frame.Scope, frame.Name);
}
}
}
else
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Debug))
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogDebug("Unknown Frame [{Scope} / {Name}]", frame.Scope, frame.Name);
this.Log().LogError(error, "Failed to process frame [{Scope} / {Name}]", frame.Scope, frame.Name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<RollForward>minor</RollForward>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);VSTHRD200</NoWarn>
<DefineConstants>$(DefineConstants);IS_DEVSERVER</DefineConstants>
</PropertyGroup>

<Import Project="../targetframework-override-noplatform.props" />
Expand Down
24 changes: 23 additions & 1 deletion src/Uno.UI.RemoteControl/Helpers/WebSocketHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,29 @@ public static class WebSocketHelper
{
mem.Position = 0;

return Frame.Read(mem);
try
{
return Frame.Read(mem);
}
catch (Exception error)
{
#if IS_DEVSERVER
var log = Uno.Extensions.LogExtensionPoint.Log(typeof(Frame));
if (log.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error))
{
Microsoft.Extensions.Logging.LoggerExtensions.LogError(log, error, "Failed to read frame");
}
#else // Client
var log = Uno.Foundation.Logging.LogExtensionPoint.Log(typeof(Frame));
if (log.IsEnabled(Uno.Foundation.Logging.LogLevel.Error))
{
log.LogError("Failed to read frame", error);
}
#endif

mem.Position = 0;
mem.SetLength(0);
}
}
}
}
Expand Down
98 changes: 54 additions & 44 deletions src/Uno.UI.RemoteControl/RemoteControlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,79 +516,89 @@ private async Task ProcessMessages(WebSocket socket, CancellationToken ct)

while (await WebSocketHelper.ReadFrame(socket, ct) is HotReload.Messages.Frame frame)
{
if (frame.Scope == WellKnownScopes.DevServerChannel)
try
{
if (frame.Name == KeepAliveMessage.Name)
{
ProcessPong(frame);
}
else if (frame.Name == ProcessorsDiscoveryResponse.Name)
if (frame.Scope == WellKnownScopes.DevServerChannel)
{
ProcessServerProcessorsDiscovered(frame);
if (frame.Name == KeepAliveMessage.Name)
{
ProcessPong(frame);
}
else if (frame.Name == ProcessorsDiscoveryResponse.Name)
{
ProcessServerProcessorsDiscovered(frame);
}
}
}
else
{
if (_processors.TryGetValue(frame.Scope, out var processor))
else
{
if (this.Log().IsEnabled(LogLevel.Trace))
if (_processors.TryGetValue(frame.Scope, out var processor))
{
this.Log().Trace($"Received frame [{frame.Scope}/{frame.Name}]");
}
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Received frame [{frame.Scope}/{frame.Name}]");
}

var skipProcessing = false;
foreach (var preProcessor in _preprocessors)
{
try
var skipProcessing = false;
foreach (var preProcessor in _preprocessors)
{
if (await preProcessor.SkipProcessingFrame(frame))
try
{
if (await preProcessor.SkipProcessingFrame(frame))
{
skipProcessing = true;
break;
}
}
catch (Exception error)
{
skipProcessing = true;
break;
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while **PRE**processing frame [{frame.Scope}/{frame.Name}] be pre-processor {preProcessor}", error);
}
}
}
catch (Exception error)

if (!skipProcessing)
{
if (this.Log().IsEnabled(LogLevel.Error))
try
{
this.Log().LogError($"Error while **PRE**processing frame [{frame.Scope}/{frame.Name}]", error);
await processor.ProcessFrame(frame);
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while processing frame [{frame.Scope}/{frame.Name}] by processor {processor}", e);
}
}
}
}

if (!skipProcessing)
else
{
try
if (this.Log().IsEnabled(LogLevel.Trace))
{
await processor.ProcessFrame(frame);
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while processing frame [{frame.Scope}/{frame.Name}]", e);
}
this.Log().Trace($"Unknown Frame scope {frame.Scope}");
}
}
}
else

try
{
FrameReceived?.Invoke(this, new ReceivedFrameEventArgs(frame));
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Trace))
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().Trace($"Unknown Frame scope {frame.Scope}");
this.Log().LogError($"Error while notifying frame received {frame.Scope}/{frame.Name}", error);
}
}
}

try
{
FrameReceived?.Invoke(this, new ReceivedFrameEventArgs(frame));
}
catch (Exception error)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().LogError($"Error while notifying frame received {frame.Scope}/{frame.Name}", error);
this.Log().LogError($"Error while processing frame {frame.Scope}/{frame.Name}", error);
}
}
}
Expand Down

0 comments on commit 9150366

Please sign in to comment.