-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix for duplicate types in TypeTable #3141
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
Conversation
| { | ||
| var typeEntry = element as SessionStateTypeEntry; | ||
| if (typeEntry.TypeData != null) | ||
| { |
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.
I'm not sure if the scenario is supported, but does Update-TypeData and Remove-TypeData work with shared type tables? And if so, will that scenario continue to work with these changes?
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.
Update-TypeData and Remove-TypeData work directly on TypeTable and nothing has changed there and the TypeTables remain the same. This bug is when passing the TypeTable to the ISS so I think it is Ok. But I am running regression tests to ensure nothing is broken.
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.
I would be surprised if we have tests covering the scenario - I think you'll need to write some new tests.
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.
As near as I can tell these cmdlets have nothing to do with the bug I fixed. They affect the TypeTable directly (add/remove) but are not involved with ISS binding to a runspace where the fix is. Maybe I am missing something but I don't see a scenario that needs testing.
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.
But the TypeTable is shared. If one changes the TypeTable in one runspace, then uses that shared TypeTable to construct another runspace, does your code handle that correctly?
I think this is a non-issue because changing a shared type table should not be allowed, but I was asking that you confirm that, preferably with tests because I wasn't confident your code was correct if it was allowed.
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.
Ok, I understand you now. I agree that this is a non-issue, not necessarily because changing a shared type table is not allowed but because the TypeTable is consistent in how it stores types. But I'll add a test for this.
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.
I verified that Update-TypeData and Remove-TypeData does not affect ISS TypeTable. I also verified that the code disallows a shared TypeTable in the ISS (unless it is the only entry), and added a test for this.
| lock (_syncObject) | ||
| { | ||
| foreach (var element in items) | ||
| { |
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.
Your check at the start of the function means you can cast instead of using as, which you should prefer because you're not testing if typeEntry is null. And I suppose we don't allow adding null entries?
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.
I thought so too but a direct cast isn't allowed:
var typeEntry = (SessionStateTypeEntry)element;
"Cannot covert type T to SessionStateTypeEntry"
It looks like a runtime cast is needed. Or is there another way?
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.
I don't know why a cast is not allowed - T is even constrained to be InitialSessionStateEntry, so C# should be smart enough to allow the cast. Oh well.
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.
I also tried using a type constrained on the method but that failed as well because SessionStateTypeEntry is a sealed class which is disallowed as a constraint...
daxian-dbw
left a comment
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.
The changes overall looks good and I believe you have answered/addressed the existing comments. I left three minor comments, can you take a look?
| $errs = $_ | ||
| } | ||
|
|
||
| $errs | Should Be $null |
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.
How about { $rs2.Open() } | Should Not Throw
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.
Thanks. Much better
| } | ||
| } | ||
|
|
||
| $errorId | Should Be "ErrorsUpdatingTypes" |
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.
As suggested by our test guideline, maybe the following is better:
try
{
$rs.Open()
throw "No Exception!"
}
catch
{
$_.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should Be "ErrorsUpdatingTypes"
}
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.
I have already been through this with Illya. The problem with this is that this can result in a null reference exception. It is better to test whether the desired error Id is found.
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.
There is no method call or indexing operation involved, only property access, so it shouldn't trigger a null reference exception. Can you please explain where the exception might be raised?
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.
try { throw "No Exception!" } catch { $_.Exception.InnerException -eq $null }
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.
property access on null value doesn't raise exception unless it's in strict mode and our test doesn't run in strict mode. But it's fine to be cautious.
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.
Good point, but I am never sure how Pester will run a block, for example ErrorActionStop. I guess I am good with either way.
| /// Special add for TypeTable type entries that removes redundant file entries. | ||
| /// </summary> | ||
| internal void AddTypeTableTypesInfo(IEnumerable<T> items) | ||
| { |
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.
Maybe the following check is more readable?
if (typeof(T) != typeof(SessionStateTypeEntry))
{
throw new PSInvalidOperationException();
}
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.
I don't think it is more readable, but it does seem simpler than checking for a valid cast/conversion.
When a TypeTable is created it includes the types from type files provided along with references to the type files. When the InitialSessionState (ISS) object processes these types it reads the type files again and ends up with duplicate type entries. PowerShell V5.1 ISS type processing was re-written to improve performance and no longer removes duplicate types, so that this scenario (runspace ISS reuse) results in errors causing a regression.
The fix is to copy only type data when a TypeTable is passed to the ISS.