Skip to content

Commit

Permalink
Create and call task outside of the GIL of the main thread to avoid d…
Browse files Browse the repository at this point in the history
…eadlock
  • Loading branch information
tonybaloney committed Nov 15, 2024
1 parent 1ab2bb7 commit e3f3abd
Showing 1 changed file with 70 additions and 19 deletions.
89 changes: 70 additions & 19 deletions src/CSnakes.SourceGeneration/Reflection/MethodReflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ PredefinedTypeSyntax s when s.Keyword.IsKind(SyntaxKind.VoidKeyword) => true,
IdentifierName($"__func_{function.Name}"))))))
);

var callStatement = LocalDeclarationStatement(
LocalDeclarationStatementSyntax callStatement;

if (!function.IsAsync)
{
callStatement = LocalDeclarationStatement(
VariableDeclaration(
IdentifierName("PyObject"))
.WithVariables(
Expand All @@ -167,10 +171,23 @@ PredefinedTypeSyntax s when s.Keyword.IsKind(SyntaxKind.VoidKeyword) => true,
.WithInitializer(
EqualsValueClause(
callExpression)))));
if (resultShouldBeDisposed)

} else
{
callStatement = LocalDeclarationStatement(
VariableDeclaration(
IdentifierName("PyObject"))
.WithVariables(
SingletonSeparatedList(
VariableDeclarator(
Identifier("__result_pyObject"))
)));

}
if (resultShouldBeDisposed && !function.IsAsync)
callStatement = callStatement.WithUsingKeyword(Token(SyntaxKind.UsingKeyword));
StatementSyntax[] statements = [
ExpressionStatement(

var logStatement = ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
Expand All @@ -183,22 +200,56 @@ PredefinedTypeSyntax s when s.Keyword.IsKind(SyntaxKind.VoidKeyword) => true,
Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal("Invoking Python function: {FunctionName}"))),
Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(function.Name)))
])))
);

BlockSyntax body;
if (function.IsAsync)
{
ExpressionStatementSyntax localCallStatement = ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("__result_pyObject"),
callExpression));

StatementSyntax[] statements = [
logStatement,
functionObject,
.. pythonConversionStatements,
localCallStatement
];

body = Block(
callStatement,
UsingStatement(
null,
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("GIL"),
IdentifierName("Acquire"))),
Block(statements)
),
functionObject,
.. pythonConversionStatements,
callStatement,
returnExpression];
var body = Block(
UsingStatement(
null,
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("GIL"),
IdentifierName("Acquire"))),
Block(statements)
));

returnExpression);
} else
{
StatementSyntax[] statements = [
logStatement,
functionObject,
.. pythonConversionStatements,
callStatement,
returnExpression];
body = Block(
UsingStatement(
null,
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("GIL"),
IdentifierName("Acquire"))),
Block(statements)
));
}

// Sort the method parameters into this order
// 1. All positional arguments
// 2. All keyword-only arguments
Expand Down

0 comments on commit e3f3abd

Please sign in to comment.