-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Avoid null refs in BlazorIgntior when Disposed #14341
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 all commits
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Ignitor | ||
{ | ||
internal class CancellableOperation<TResult> | ||
{ | ||
public CancellableOperation(TimeSpan? timeout) | ||
{ | ||
Timeout = timeout; | ||
|
||
Completion = new TaskCompletionSource<TResult>(TaskContinuationOptions.RunContinuationsAsynchronously); | ||
Completion.Task.ContinueWith( | ||
(task, state) => | ||
{ | ||
var operation = (CancellableOperation<TResult>)state; | ||
operation.Dispose(); | ||
}, | ||
this, | ||
TaskContinuationOptions.ExecuteSynchronously); // We need to execute synchronously to clean-up before anything else continues | ||
|
||
if (Timeout != null && Timeout != System.Threading.Timeout.InfiniteTimeSpan && Timeout != TimeSpan.MaxValue) | ||
{ | ||
Cancellation = new CancellationTokenSource(Timeout.Value); | ||
CancellationRegistration = Cancellation.Token.Register( | ||
(self) => | ||
{ | ||
var operation = (CancellableOperation<TResult>)self; | ||
operation.Completion.TrySetCanceled(operation.Cancellation.Token); | ||
operation.Cancellation.Dispose(); | ||
operation.CancellationRegistration.Dispose(); | ||
}, | ||
this); | ||
} | ||
} | ||
|
||
public TimeSpan? Timeout { get; } | ||
|
||
public TaskCompletionSource<TResult> Completion { get; } | ||
|
||
public CancellationTokenSource Cancellation { get; } | ||
|
||
public CancellationTokenRegistration CancellationRegistration { get; } | ||
|
||
public bool Disposed { get; private set; } | ||
|
||
private void Dispose() | ||
{ | ||
if (Disposed) | ||
{ | ||
return; | ||
} | ||
|
||
Disposed = true; | ||
Completion.TrySetCanceled(Cancellation.Token); | ||
Cancellation.Dispose(); | ||
CancellationRegistration.Dispose(); | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -108,7 +108,7 @@ public async Task DisposeAsync() | |||||
var instance = await SeleniumStandaloneServer.GetInstanceAsync(output); | ||||||
|
||||||
var attempt = 0; | ||||||
var maxAttempts = 3; | ||||||
const int maxAttempts = 3; | ||||||
do | ||||||
{ | ||||||
try | ||||||
|
@@ -132,18 +132,16 @@ public async Task DisposeAsync() | |||||
|
||||||
return (driver, logs); | ||||||
} | ||||||
catch | ||||||
catch (Exception ex) | ||||||
{ | ||||||
if (attempt >= maxAttempts) | ||||||
{ | ||||||
throw new InvalidOperationException("Couldn't create a Selenium remote driver client. The server is irresponsive"); | ||||||
} | ||||||
output.WriteLine($"Error initializing RemoteWebDriver: {ex.Message}"); | ||||||
} | ||||||
|
||||||
attempt++; | ||||||
|
||||||
} while (attempt < maxAttempts); | ||||||
|
||||||
// We will never get here. Keeping the compiler happy. | ||||||
throw new InvalidOperationException("Couldn't create a Selenium remote driver client. The server is unresponsive"); | ||||||
throw new InvalidOperationException("Couldn't create a Selenium remote driver client. The server is irresponsive"); | ||||||
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.
Suggested change
(Joking, but wouldn't a better message be something like "This line of code should not be reachable", given the comment above?) 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. Completely orthogonal comment, I've seen selenium timeout in some tests, I'm wondering if we have instances where it becomes irresponsible. And if we should change the code that initializes selenium (in the future) to do this check on a per-test basis and switch the server (start a new one) if it detects the current one is not being responsive. |
||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we doing this now?