Skip to content

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

Merged
merged 3 commits into from
Sep 25, 2019
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
1 change: 1 addition & 0 deletions docs/BuildFromSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Building ASP.NET Core on Windows requires:
```ps1
PS> ./eng/scripts/InstallJdk.ps1
```
* Chrome - Selenium-based tests require a version of Chrome to be installed. Download and install it from [https://www.google.com/chrome]

### macOS/Linux

Expand Down
60 changes: 5 additions & 55 deletions src/Components/Ignitor/src/BlazorClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public BlazorClient()

public Task<CapturedRenderBatch> PrepareForNextBatch(TimeSpan? timeout)
{
if (NextBatchReceived?.Completion != null)
if (NextBatchReceived != null && !NextBatchReceived.Disposed)
{
throw new InvalidOperationException("Invalid state previous task not completed");
}
Expand All @@ -100,7 +100,7 @@ public Task<CapturedRenderBatch> PrepareForNextBatch(TimeSpan? timeout)

public Task<CapturedJSInteropCall> PrepareForNextJSInterop(TimeSpan? timeout)
{
if (NextJSInteropReceived?.Completion != null)
if (NextJSInteropReceived != null && !NextJSInteropReceived.Disposed)
{
throw new InvalidOperationException("Invalid state previous task not completed");
}
Expand All @@ -112,7 +112,7 @@ public Task<CapturedJSInteropCall> PrepareForNextJSInterop(TimeSpan? timeout)

public Task<string> PrepareForNextDotNetInterop(TimeSpan? timeout)
{
if (NextDotNetInteropCompletionReceived?.Completion != null)
if (NextDotNetInteropCompletionReceived != null && !NextDotNetInteropCompletionReceived.Disposed)
{
throw new InvalidOperationException("Invalid state previous task not completed");
}
Expand All @@ -124,7 +124,7 @@ public Task<string> PrepareForNextDotNetInterop(TimeSpan? timeout)

public Task<string> PrepareForNextCircuitError(TimeSpan? timeout)
{
if (NextErrorReceived?.Completion != null)
if (NextErrorReceived != null && !NextErrorReceived.Disposed)
{
throw new InvalidOperationException("Invalid state previous task not completed");
}
Expand All @@ -136,7 +136,7 @@ public Task<string> PrepareForNextCircuitError(TimeSpan? timeout)

public Task<Exception> PrepareForNextDisconnect(TimeSpan? timeout)
{
if (NextDisconnect?.Completion != null)
if (NextDisconnect != null && !NextDisconnect.Disposed)
{
throw new InvalidOperationException("Invalid state previous task not completed");
}
Expand Down Expand Up @@ -499,56 +499,6 @@ public ElementNode FindElementById(string id)
return element;
}

private class CancellableOperation<TResult>
{
public CancellableOperation(TimeSpan? timeout)
{
Timeout = timeout;
Initialize();
}

public TimeSpan? Timeout { get; }

public TaskCompletionSource<TResult> Completion { get; set; }

public CancellationTokenSource Cancellation { get; set; }

public CancellationTokenRegistration CancellationRegistration { get; set; }

private void Initialize()
{
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);
}
}

private void Dispose()
{
Completion = null;
Cancellation.Dispose();
CancellationRegistration.Dispose();
}
}

private string[] ReadMarkers(string content)
{
content = content.Replace("\r\n", "").Replace("\n", "");
Expand Down
64 changes: 64 additions & 0 deletions src/Components/Ignitor/src/CancellableOperation.cs
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();
}
}
}
4 changes: 2 additions & 2 deletions src/Components/Web.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions src/Shared/E2ETesting/BrowserFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task DisposeAsync()
var instance = await SeleniumStandaloneServer.GetInstanceAsync(output);

var attempt = 0;
var maxAttempts = 3;
const int maxAttempts = 3;
Copy link
Member

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?

do
{
try
Expand All @@ -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");
Copy link
Member

@SteveSandersonMS SteveSandersonMS Sep 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new InvalidOperationException("Couldn't create a Selenium remote driver client. The server is irresponsive");
throw new InvalidOperationException("Couldn't create a Selenium remote driver client. The server is anti-responsive");

(Joking, but wouldn't a better message be something like "This line of code should not be reachable", given the comment above?)

Copy link
Member

Choose a reason for hiding this comment

The 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.

}
}
}