Skip to content

Commit 7bcaf5b

Browse files
ReubenBondCopilot
andcommitted
fix(codegen): preserve parameterless invokable activation
Keep a compatibility constructor for pooled invokables and only return instances to a pool when they were created by the pool-aware activator. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent a4eb02c commit 7bcaf5b

8 files changed

Lines changed: 73 additions & 15 deletions

src/Orleans.CodeGenerator/InvokableGenerator.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public GeneratedInvokableDescription Generate(InvokableMethodDescription invokab
2626
var invokableTypeSyntax = CreateInvokableTypeSyntax(generatedClassName, invokableMethodInfo);
2727
var fields = GetFieldDeclarations(invokableMethodInfo, fieldDescriptions, invokableTypeSyntax);
2828
var (ctor, ctorArgs) = GenerateConstructor(generatedClassName, invokableMethodInfo, baseClassType, fieldDescriptions, invokableTypeSyntax);
29+
var compatibilityCtor = fieldDescriptions.OfType<PoolFieldDescription>().Any()
30+
? ConstructorDeclaration(generatedClassName)
31+
.AddModifiers(Token(SyntaxKind.PublicKeyword))
32+
.WithBody(Block())
33+
: null;
2934
var accessibility = GetAccessibility(method);
3035
var compoundTypeAliases = GetCompoundTypeAliasAttributeArguments(invokableMethodInfo, invokableMethodInfo.Key);
3136

@@ -53,6 +58,7 @@ public GeneratedInvokableDescription Generate(InvokableMethodDescription invokab
5358
baseClassType,
5459
fieldDescriptions,
5560
fields,
61+
compatibilityCtor,
5662
ctor,
5763
compoundTypeAliases,
5864
targetField,
@@ -107,6 +113,7 @@ private ClassDeclarationSyntax GetClassDeclarationSyntax(
107113
INamedTypeSymbol baseClassType,
108114
List<InvokerFieldDescription> fieldDescriptions,
109115
MemberDeclarationSyntax[] fields,
116+
ConstructorDeclarationSyntax? compatibilityCtor,
110117
ConstructorDeclarationSyntax? ctor,
111118
List<CompoundTypeAliasComponent[]> compoundTypeAliases,
112119
TargetFieldDescription targetField,
@@ -124,7 +131,12 @@ private ClassDeclarationSyntax GetClassDeclarationSyntax(
124131
AttributeList(SingletonSeparatedList(GetCompoundTypeAliasAttribute(alias))));
125132
}
126133

127-
if (ctor != null)
134+
if (compatibilityCtor is not null)
135+
{
136+
classDeclaration = classDeclaration.AddMembers(compatibilityCtor);
137+
}
138+
139+
if (ctor is not null)
128140
{
129141
classDeclaration = classDeclaration.AddMembers(ctor);
130142
}
@@ -613,9 +625,11 @@ private MemberDeclarationSyntax GenerateDisposeMethod(
613625
{
614626
body.Add(
615627
ExpressionStatement(
616-
InvocationExpression(
617-
IdentifierName(poolField.FieldName).Member("Return"),
618-
ArgumentList(SingletonSeparatedList(Argument(ThisExpression()))))));
628+
ConditionalAccessExpression(
629+
IdentifierName(poolField.FieldName),
630+
InvocationExpression(
631+
MemberBindingExpression(IdentifierName("Return")),
632+
ArgumentList(SingletonSeparatedList(Argument(ThisExpression())))))));
619633
}
620634

621635
return MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), "Dispose")

test/Orleans.CodeGenerator.Tests/snapshots/OrleansSourceGeneratorTests.TestBasicGrain.verified.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public sealed class Invokable_IBasicGrain_GrainReference_6B0E24A1 : global::Orle
1818
global::TestProject.IBasicGrain _target;
1919
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IBasicGrain), "SayHello", null, new[] { typeof(string) });
2020
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IBasicGrain_GrainReference_6B0E24A1> _pool;
21+
public Invokable_IBasicGrain_GrainReference_6B0E24A1()
22+
{
23+
}
24+
2125
public Invokable_IBasicGrain_GrainReference_6B0E24A1(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IBasicGrain_GrainReference_6B0E24A1> pool) : base()
2226
{
2327
_pool = pool;
@@ -35,7 +39,7 @@ public override void Dispose()
3539
{
3640
arg0 = default;
3741
_target = default;
38-
_pool.Return(this);
42+
_pool?.Return(this);
3943
}
4044

4145
public override object GetArgument(int index)

test/Orleans.CodeGenerator.Tests/snapshots/OrleansSourceGeneratorTests.TestClassWithGenerateMethodSerializersAnnotation.verified.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public sealed class Invokable_IMyGrain_GrainReference_6D39E404 : global::Orleans
1818
global::IMyGrain _target;
1919
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::IMyGrain), "SayHello", null, new[] { typeof(string) });
2020
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrain_GrainReference_6D39E404> _pool;
21+
public Invokable_IMyGrain_GrainReference_6D39E404()
22+
{
23+
}
24+
2125
public Invokable_IMyGrain_GrainReference_6D39E404(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrain_GrainReference_6D39E404> pool) : base()
2226
{
2327
_pool = pool;
@@ -35,7 +39,7 @@ public override void Dispose()
3539
{
3640
arg0 = default;
3741
_target = default;
38-
_pool.Return(this);
42+
_pool?.Return(this);
3943
}
4044

4145
public override object GetArgument(int index)

test/Orleans.CodeGenerator.Tests/snapshots/OrleansSourceGeneratorTests.TestGrainComplexGrain.verified.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public sealed class Invokable_IComplexGrain_GrainReference_67FE5808 : global::Or
2222
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IComplexGrain), "ProcessData", null, new[] { typeof(int), typeof(string), typeof(global::TestProject.ComplexData), typeof(global::System.Threading.CancellationToken) });
2323
global::System.Threading.CancellationTokenSource _cts;
2424
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IComplexGrain_GrainReference_67FE5808> _pool;
25+
public Invokable_IComplexGrain_GrainReference_67FE5808()
26+
{
27+
}
28+
2529
public Invokable_IComplexGrain_GrainReference_67FE5808(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IComplexGrain_GrainReference_67FE5808> pool) : base()
2630
{
2731
_pool = pool;
@@ -50,7 +54,7 @@ public override void Dispose()
5054
_target = default;
5155
_cts?.Dispose();
5256
_cts = default;
53-
_pool.Return(this);
57+
_pool?.Return(this);
5458
}
5559

5660
public override object GetArgument(int index)

test/Orleans.CodeGenerator.Tests/snapshots/OrleansSourceGeneratorTests.TestGrainMethodAnnotatedWithInvokableBaseType.verified.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public sealed class Invokable_IHelloGrain_GrainReference_5336307F : global::Orle
1818
global::TestProject.IHelloGrain _target;
1919
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IHelloGrain), "SayHello", null, new[] { typeof(string) });
2020
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IHelloGrain_GrainReference_5336307F> _pool;
21+
public Invokable_IHelloGrain_GrainReference_5336307F()
22+
{
23+
}
24+
2125
public Invokable_IHelloGrain_GrainReference_5336307F(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IHelloGrain_GrainReference_5336307F> pool) : base()
2226
{
2327
_pool = pool;
@@ -36,7 +40,7 @@ public override void Dispose()
3640
{
3741
arg0 = default;
3842
_target = default;
39-
_pool.Return(this);
43+
_pool?.Return(this);
4044
}
4145

4246
public override object GetArgument(int index)

test/Orleans.CodeGenerator.Tests/snapshots/OrleansSourceGeneratorTests.TestGrainMethodAnnotatedWithResponseTimeout.verified.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public sealed class Invokable_IResponseTimeoutGrain_GrainReference_6BE752C8 : gl
1818
global::TestProject.IResponseTimeoutGrain _target;
1919
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IResponseTimeoutGrain), "LongRunningMethod", null, new[] { typeof(string) });
2020
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IResponseTimeoutGrain_GrainReference_6BE752C8> _pool;
21+
public Invokable_IResponseTimeoutGrain_GrainReference_6BE752C8()
22+
{
23+
}
24+
2125
public Invokable_IResponseTimeoutGrain_GrainReference_6BE752C8(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IResponseTimeoutGrain_GrainReference_6BE752C8> pool) : base()
2226
{
2327
_pool = pool;
@@ -37,7 +41,7 @@ public override void Dispose()
3741
{
3842
arg0 = default;
3943
_target = default;
40-
_pool.Return(this);
44+
_pool?.Return(this);
4145
}
4246

4347
public override object GetArgument(int index)

test/Orleans.CodeGenerator.Tests/snapshots/OrleansSourceGeneratorTests.TestGrainWithDifferentKeyTypes.verified.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public sealed class Invokable_IMyGrainWithGuidKey_GrainReference_8F0FEC0E : glob
1717
global::TestProject.IMyGrainWithGuidKey _target;
1818
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IMyGrainWithGuidKey), "GetGuidValue", null, null);
1919
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithGuidKey_GrainReference_8F0FEC0E> _pool;
20+
public Invokable_IMyGrainWithGuidKey_GrainReference_8F0FEC0E()
21+
{
22+
}
23+
2024
public Invokable_IMyGrainWithGuidKey_GrainReference_8F0FEC0E(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithGuidKey_GrainReference_8F0FEC0E> pool) : base()
2125
{
2226
_pool = pool;
@@ -32,7 +36,7 @@ public Invokable_IMyGrainWithGuidKey_GrainReference_8F0FEC0E(global::Orleans.Ser
3236
public override void Dispose()
3337
{
3438
_target = default;
35-
_pool.Return(this);
39+
_pool?.Return(this);
3640
}
3741

3842
protected override global::System.Threading.Tasks.Task<global::System.Guid> InvokeInner() => _target.GetGuidValue();
@@ -60,6 +64,10 @@ public sealed class Invokable_IMyGrainWithStringKey_GrainReference_43570316 : gl
6064
global::TestProject.IMyGrainWithStringKey _target;
6165
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IMyGrainWithStringKey), "GetStringKey", null, null);
6266
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithStringKey_GrainReference_43570316> _pool;
67+
public Invokable_IMyGrainWithStringKey_GrainReference_43570316()
68+
{
69+
}
70+
6371
public Invokable_IMyGrainWithStringKey_GrainReference_43570316(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithStringKey_GrainReference_43570316> pool) : base()
6472
{
6573
_pool = pool;
@@ -75,7 +83,7 @@ public Invokable_IMyGrainWithStringKey_GrainReference_43570316(global::Orleans.S
7583
public override void Dispose()
7684
{
7785
_target = default;
78-
_pool.Return(this);
86+
_pool?.Return(this);
7987
}
8088

8189
protected override global::System.Threading.Tasks.Task<string> InvokeInner() => _target.GetStringKey();
@@ -103,6 +111,10 @@ public sealed class Invokable_IMyGrainWithGuidCompoundKey_GrainReference_A9FEF7A
103111
global::TestProject.IMyGrainWithGuidCompoundKey _target;
104112
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IMyGrainWithGuidCompoundKey), "GetGuidAndStringKey", null, null);
105113
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithGuidCompoundKey_GrainReference_A9FEF7AF> _pool;
114+
public Invokable_IMyGrainWithGuidCompoundKey_GrainReference_A9FEF7AF()
115+
{
116+
}
117+
106118
public Invokable_IMyGrainWithGuidCompoundKey_GrainReference_A9FEF7AF(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithGuidCompoundKey_GrainReference_A9FEF7AF> pool) : base()
107119
{
108120
_pool = pool;
@@ -118,7 +130,7 @@ public Invokable_IMyGrainWithGuidCompoundKey_GrainReference_A9FEF7AF(global::Orl
118130
public override void Dispose()
119131
{
120132
_target = default;
121-
_pool.Return(this);
133+
_pool?.Return(this);
122134
}
123135

124136
protected override global::System.Threading.Tasks.Task<global::System.Tuple<global::System.Guid, string>> InvokeInner() => _target.GetGuidAndStringKey();
@@ -146,6 +158,10 @@ public sealed class Invokable_IMyGrainWithIntegerCompoundKey_GrainReference_9814
146158
global::TestProject.IMyGrainWithIntegerCompoundKey _target;
147159
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IMyGrainWithIntegerCompoundKey), "GetIntegerAndStringKey", null, null);
148160
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithIntegerCompoundKey_GrainReference_9814021A> _pool;
161+
public Invokable_IMyGrainWithIntegerCompoundKey_GrainReference_9814021A()
162+
{
163+
}
164+
149165
public Invokable_IMyGrainWithIntegerCompoundKey_GrainReference_9814021A(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IMyGrainWithIntegerCompoundKey_GrainReference_9814021A> pool) : base()
150166
{
151167
_pool = pool;
@@ -161,7 +177,7 @@ public Invokable_IMyGrainWithIntegerCompoundKey_GrainReference_9814021A(global::
161177
public override void Dispose()
162178
{
163179
_target = default;
164-
_pool.Return(this);
180+
_pool?.Return(this);
165181
}
166182

167183
protected override global::System.Threading.Tasks.Task<global::System.Tuple<long, string>> InvokeInner() => _target.GetIntegerAndStringKey();

test/Orleans.CodeGenerator.Tests/snapshots/OrleansSourceGeneratorTests.TestGrainWithMultipleInterfaces.verified.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public sealed class Invokable_IGrainA_GrainReference_11405B98 : global::Orleans.
1818
global::TestProject.IGrainA _target;
1919
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IGrainA), "MethodA", null, new[] { typeof(string) });
2020
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IGrainA_GrainReference_11405B98> _pool;
21+
public Invokable_IGrainA_GrainReference_11405B98()
22+
{
23+
}
24+
2125
public Invokable_IGrainA_GrainReference_11405B98(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IGrainA_GrainReference_11405B98> pool) : base()
2226
{
2327
_pool = pool;
@@ -35,7 +39,7 @@ public override void Dispose()
3539
{
3640
arg0 = default;
3741
_target = default;
38-
_pool.Return(this);
42+
_pool?.Return(this);
3943
}
4044

4145
public override object GetArgument(int index)
@@ -89,6 +93,10 @@ public sealed class Invokable_IGrainB_GrainReference_6B5D7809 : global::Orleans.
8993
global::TestProject.IGrainB _target;
9094
private static readonly global::System.Reflection.MethodInfo MethodBackingField = OrleansGeneratedCodeHelper.GetMethodInfoOrDefault(typeof(global::TestProject.IGrainB), "MethodB", null, new[] { typeof(string) });
9195
private readonly global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IGrainB_GrainReference_6B5D7809> _pool;
96+
public Invokable_IGrainB_GrainReference_6B5D7809()
97+
{
98+
}
99+
92100
public Invokable_IGrainB_GrainReference_6B5D7809(global::Orleans.Serialization.Invocation.InvokablePool<Invokable_IGrainB_GrainReference_6B5D7809> pool) : base()
93101
{
94102
_pool = pool;
@@ -106,7 +114,7 @@ public override void Dispose()
106114
{
107115
arg0 = default;
108116
_target = default;
109-
_pool.Return(this);
117+
_pool?.Return(this);
110118
}
111119

112120
public override object GetArgument(int index)

0 commit comments

Comments
 (0)