Skip to content
Closed
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ examples/perl/**/local/*
**cpanfile.snapshot
**/.modules/
**/*.AppHost.TypeScript/nuget.config
*.lscache
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<ItemGroup Label="System">
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Linq.AsyncEnumerable" Version="$(DotNetExtensionsVersion)" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="10.0.6" />
</ItemGroup>
<ItemGroup Label="Overrides">
<PackageVersion Include="Microsoft.Bcl.Memory" Version="9.0.14" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Aspire.Hosting" />
<PackageReference Include="Microsoft.PowerShell.SDK" />
<PackageReference Include="System.Security.Cryptography.Xml" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ await builder.Eventing.PublishAsync(
var poolName = res.Name;
var poolLogger = loggerService.GetLogger(poolName);

_ = res.StartAsync(sessionState, notificationService, poolLogger, hostLifetime, ct);
// Await pool open so the resource isn't marked Running before the RunspacePool exists.
// Otherwise dependent scripts that WaitFor(pool) can race and hit a null Parent.Pool.
await res.StartAsync(sessionState, notificationService, poolLogger, hostLifetime, ct);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch. I had done some refactoring and clearly messed up. This absolutely needs to be awaited.

});

return poolBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,22 @@ public async Task StartAsync(ILogger scriptLogger,
CancellationToken cancellationToken = default)
{
scriptLogger.LogInformation("Starting PowerShell script '{ScriptName}'", Name);

Debug.Assert(scriptLogger is not null);
_scriptLogger = scriptLogger;

// Aspire's WaitFor on the parent runspace pool doesn't reliably block here, so poll until
// the pool has been created AND opened by the parent resource before binding the PowerShell
// instance to it. Without this, _ps.RunspacePool can be null (hangs InvokeAsync) or in
// 'Opening' state (throws InvalidRunspacePoolStateException).
while ((Parent.Pool is null || Parent.Pool.RunspacePoolStateInfo.State != System.Management.Automation.Runspaces.RunspacePoolState.Opened)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't need this after having added the await earlier, right? Unless there's some other issue implied?

&& !cancellationToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromMilliseconds(50), cancellationToken).ConfigureAwait(false);
}

cancellationToken.ThrowIfCancellationRequested();

Debug.Assert(Parent.Pool is not null);
_ps.RunspacePool = Parent.Pool;

Expand Down
Loading