-
Notifications
You must be signed in to change notification settings - Fork 19
fix: resolve Timer.Start dueTime bugs and hardening #363
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,38 +78,52 @@ public void StopRecordingDiagnostics() | |
|
|
||
| private async void OnTaskUnobservedException(object? sender, UnobservedTaskExceptionEventArgs args) | ||
| { | ||
| args.SetObserved(); | ||
|
|
||
| var correlationId = Guid.NewGuid(); | ||
| try | ||
| { | ||
| args.SetObserved(); | ||
|
|
||
| await this.EventLogger.WriteCritical( | ||
| args.Exception != null | ||
| ? $"An unobserved task exception was thrown. Correlation ID: {correlationId}. Error: {args.Exception}." | ||
| : $"An unobserved task exception was thrown. Correlation ID: {correlationId}. Error: No exception information was available.").ConfigureAwait(false); | ||
| var correlationId = Guid.NewGuid(); | ||
|
|
||
| if (args.Exception != null) | ||
| await this.EventLogger.WriteCritical( | ||
| args.Exception != null | ||
| ? $"An unobserved task exception was thrown. Correlation ID: {correlationId}. Error: {args.Exception}." | ||
| : $"An unobserved task exception was thrown. Correlation ID: {correlationId}. Error: No exception information was available.").ConfigureAwait(false); | ||
|
|
||
| if (args.Exception != null) | ||
| { | ||
| this.ExceptionObserved?.Invoke(this, new ExceptionObservedEventArgs(correlationId, args.Exception)); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| this.ExceptionObserved?.Invoke(this, new ExceptionObservedEventArgs(correlationId, args.Exception)); | ||
| // Swallow exceptions in last-resort exception handlers to prevent crashing the process. | ||
| } | ||
| } | ||
|
|
||
| private async void OnAppUnhandledException(object sender, UnhandledExceptionEventArgs args) | ||
| { | ||
| if (args.IsTerminating) | ||
| try | ||
| { | ||
| await this.EventLogger.WriteCritical( | ||
| "The application is terminating due to an unhandled exception being thrown.").ConfigureAwait(false); | ||
| } | ||
| if (args.IsTerminating) | ||
| { | ||
| await this.EventLogger.WriteCritical( | ||
| "The application is terminating due to an unhandled exception being thrown.").ConfigureAwait(false); | ||
| } | ||
|
|
||
| if (args.ExceptionObject is not Exception ex) | ||
| { | ||
| return; | ||
| } | ||
| if (args.ExceptionObject is not Exception ex) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var correlationId = Guid.NewGuid(); | ||
| var correlationId = Guid.NewGuid(); | ||
|
|
||
| await this.EventLogger.WriteCritical($"An unhandled exception was thrown. Correlation ID: {correlationId}. Error: {ex}").ConfigureAwait(false); | ||
| await this.EventLogger.WriteCritical($"An unhandled exception was thrown. Correlation ID: {correlationId}. Error: {ex}").ConfigureAwait(false); | ||
|
|
||
| this.ExceptionObserved?.Invoke(this, new ExceptionObservedEventArgs(correlationId, ex)); | ||
| this.ExceptionObserved?.Invoke(this, new ExceptionObservedEventArgs(correlationId, ex)); | ||
| } | ||
| catch | ||
| { | ||
| // Swallow exceptions in last-resort exception handlers to prevent crashing the process. | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,7 @@ | |
| { | ||
| this.CurrentQueue = new ConcurrentDictionary<string, NetworkRequestCallback>(); | ||
| this.processTimer = new Timer(); | ||
| this.processTimer.Tick += this.OnProcessTimerTick; | ||
|
Check warning on line 33 in src/MADE.Networking/Http/NetworkRequestManager.cs
|
||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -74,7 +74,7 @@ | |
| return; | ||
| } | ||
|
|
||
| this.processTimer.Tick -= this.OnProcessTimerTick; | ||
|
Check warning on line 77 in src/MADE.Networking/Http/NetworkRequestManager.cs
|
||
| this.processTimer.Stop(); | ||
| this.processTimer.Dispose(); | ||
| this.disposed = true; | ||
|
|
@@ -84,7 +84,8 @@ | |
| /// <summary> | ||
| /// Processes the current queue of network requests. | ||
| /// </summary> | ||
| public void ProcessCurrentQueue() | ||
| /// <returns>An asynchronous operation.</returns> | ||
| public async Task ProcessCurrentQueueAsync() | ||
| { | ||
| if (this.CurrentQueue.Count == 0 || this.isProcessingRequests) | ||
| { | ||
|
|
@@ -102,13 +103,13 @@ | |
| { | ||
| if (this.CurrentQueue.TryRemove( | ||
| this.CurrentQueue.FirstOrDefault().Key, | ||
| out NetworkRequestCallback request)) | ||
| out NetworkRequestCallback? request)) | ||
| { | ||
| requestTasks.Add(ExecuteRequestsAsync(this.CurrentQueue, request, cts.Token)); | ||
| } | ||
| } | ||
|
|
||
| Task.WhenAll(requestTasks).GetAwaiter().GetResult(); | ||
| await Task.WhenAll(requestTasks).ConfigureAwait(false); | ||
| } | ||
| finally | ||
| { | ||
|
|
@@ -220,23 +221,39 @@ | |
| } | ||
|
|
||
| NetworkRequest request = requestCallback.Request; | ||
| WeakReferenceCallback successCallback = requestCallback.SuccessCallback; | ||
| WeakReferenceCallback errorCallback = requestCallback.ErrorCallback; | ||
| WeakReferenceCallback? successCallback = requestCallback.SuccessCallback; | ||
| WeakReferenceCallback? errorCallback = requestCallback.ErrorCallback; | ||
|
|
||
| try | ||
| { | ||
| if (successCallback is null) | ||
| { | ||
| return; | ||
|
jamesmcroft marked this conversation as resolved.
|
||
| } | ||
|
|
||
| object response = await request.ExecuteAsync(successCallback.Type, cancellationToken).ConfigureAwait(false); | ||
| successCallback.Invoke(response); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| successCallback.Invoke(Activator.CreateInstance(successCallback.Type)); | ||
| if (successCallback is not null) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| { | ||
| successCallback.Invoke(Activator.CreateInstance(successCallback.Type)!); | ||
| } | ||
|
|
||
| errorCallback?.Invoke(ex); | ||
| } | ||
| } | ||
|
|
||
| private void OnProcessTimerTick(object sender, object e) | ||
| private async void OnProcessTimerTick(object sender, object e) | ||
| { | ||
| this.ProcessCurrentQueue(); | ||
| try | ||
| { | ||
| await this.ProcessCurrentQueueAsync().ConfigureAwait(false); | ||
| } | ||
| catch | ||
| { | ||
| // Swallow exceptions in timer callback to prevent unobserved task exceptions. | ||
|
jamesmcroft marked this conversation as resolved.
Outdated
|
||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.