|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | + |
| 18 | +package org.sonar.python.checks; |
| 19 | + |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import javax.annotation.Nullable; |
| 23 | + |
| 24 | +import org.sonar.check.Rule; |
| 25 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 26 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 27 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 28 | +import org.sonar.plugins.python.api.symbols.Usage; |
| 29 | +import org.sonar.plugins.python.api.tree.AwaitExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.BaseTreeVisitor; |
| 31 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 32 | +import org.sonar.plugins.python.api.tree.Expression; |
| 33 | +import org.sonar.plugins.python.api.tree.FileInput; |
| 34 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 35 | +import org.sonar.plugins.python.api.tree.Name; |
| 36 | +import org.sonar.plugins.python.api.tree.Tree; |
| 37 | +import org.sonar.plugins.python.api.tree.Tree.Kind; |
| 38 | +import org.sonar.plugins.python.api.tree.WhileStatement; |
| 39 | +import org.sonar.python.tree.TreeUtils; |
| 40 | +import org.sonar.python.types.v2.TypeCheckMap; |
| 41 | + |
| 42 | +@Rule(key = "S7484") |
| 43 | +public class BusyWaitingInAsyncCheck extends PythonSubscriptionCheck { |
| 44 | + private static final String MESSAGE = "Refactor this loop to use an `Event` instead of polling with `sleep`."; |
| 45 | + private static final String SECONDARY_MESSAGE = "This function is async."; |
| 46 | + private static final String POLLING_MESSAGE = "Polling happens here."; |
| 47 | + |
| 48 | + private TypeCheckMap<Object> asyncSleepTypeChecks; |
| 49 | + |
| 50 | + @Override |
| 51 | + public void initialize(Context context) { |
| 52 | + context.registerSyntaxNodeConsumer(Kind.FILE_INPUT, this::initializeTypeCheckMap); |
| 53 | + context.registerSyntaxNodeConsumer(Kind.WHILE_STMT, this::checkWhileStatement); |
| 54 | + } |
| 55 | + |
| 56 | + private void initializeTypeCheckMap(SubscriptionContext ctx) { |
| 57 | + var object = new Object(); |
| 58 | + asyncSleepTypeChecks = new TypeCheckMap<>(); |
| 59 | + asyncSleepTypeChecks.put(ctx.typeChecker().typeCheckBuilder().isTypeOrInstanceWithName("asyncio.sleep"), object); |
| 60 | + asyncSleepTypeChecks.put(ctx.typeChecker().typeCheckBuilder().isTypeWithFqn("trio.sleep"), object); |
| 61 | + asyncSleepTypeChecks.put(ctx.typeChecker().typeCheckBuilder().isTypeOrInstanceWithName("anyio.sleep"), object); |
| 62 | + } |
| 63 | + |
| 64 | + private void checkWhileStatement(SubscriptionContext ctx) { |
| 65 | + var whileStmt = (WhileStatement) ctx.syntaxNode(); |
| 66 | + var enclosingFuncDef = (FunctionDef) TreeUtils.firstAncestorOfKind(whileStmt, Kind.FUNCDEF); |
| 67 | + var asyncToken = Optional.ofNullable(enclosingFuncDef).map(FunctionDef::asyncKeyword); |
| 68 | + if (asyncToken.isEmpty()) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + var sleepFinder = new SleepCallFinder(asyncSleepTypeChecks); |
| 73 | + whileStmt.body().accept(sleepFinder); |
| 74 | + |
| 75 | + if (sleepFinder.sleepAwait == null) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + var conditionChecker = new GlobalOrNonLocalNameFinder(enclosingFuncDef); |
| 80 | + whileStmt.condition().accept(conditionChecker); |
| 81 | + |
| 82 | + if (conditionChecker.foundGlobalOrNonLocal) { |
| 83 | + ctx.addIssue(whileStmt.condition(), MESSAGE) |
| 84 | + .secondary(sleepFinder.sleepAwait, POLLING_MESSAGE) |
| 85 | + .secondary(asyncToken.get(), SECONDARY_MESSAGE); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static class GlobalOrNonLocalNameFinder extends BaseTreeVisitor { |
| 90 | + boolean foundGlobalOrNonLocal = false; |
| 91 | + private final FunctionDef whileLoopFunction; |
| 92 | + |
| 93 | + GlobalOrNonLocalNameFinder(FunctionDef whileLoopFunction) { |
| 94 | + this.whileLoopFunction = whileLoopFunction; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void visitName(Name name) { |
| 99 | + if (foundGlobalOrNonLocal) { |
| 100 | + return; |
| 101 | + } |
| 102 | + var symbol = name.symbol(); |
| 103 | + if (symbol != null && isSymbolDeclaredInOuterScope(symbol, whileLoopFunction)) { |
| 104 | + foundGlobalOrNonLocal = true; |
| 105 | + } |
| 106 | + super.visitName(name); |
| 107 | + } |
| 108 | + |
| 109 | + // Use of Symbol V1 instead of SymbolV2 because of SONARPY-2974 |
| 110 | + private static boolean isSymbolDeclaredInOuterScope(Symbol symbol, FunctionDef currentFunctionContext) { |
| 111 | + var fileInput = (FileInput) TreeUtils.firstAncestorOfKind(currentFunctionContext, Kind.FILE_INPUT); |
| 112 | + for (var usage : symbol.usages()) { |
| 113 | + if (usage.kind() == Usage.Kind.ASSIGNMENT_LHS) { |
| 114 | + if (fileInput.globalVariables().contains(symbol)) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + var currentFunction = (FunctionDef) TreeUtils.firstAncestorOfKind(currentFunctionContext, Kind.FUNCDEF); |
| 118 | + while (currentFunction != null) { |
| 119 | + if (currentFunction.localVariables().contains(symbol)) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + currentFunction = (FunctionDef) TreeUtils.firstAncestorOfKind(currentFunction, Kind.FUNCDEF); |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + } |
| 127 | + return false; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static class SleepCallFinder extends BaseTreeVisitor { |
| 132 | + Tree sleepAwait = null; |
| 133 | + private final TypeCheckMap<Object> asyncSleepTypeChecks; |
| 134 | + |
| 135 | + SleepCallFinder(TypeCheckMap<Object> asyncSleepTypeChecks) { |
| 136 | + this.asyncSleepTypeChecks = asyncSleepTypeChecks; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void visitAwaitExpression(AwaitExpression awaitExpr) { |
| 141 | + var expr = awaitExpr.expression(); |
| 142 | + if (expr instanceof CallExpression callExpr && isAsyncSleepCall(callExpr.callee())) { |
| 143 | + sleepAwait = awaitExpr.awaitToken(); |
| 144 | + } |
| 145 | + super.visitAwaitExpression(awaitExpr); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + protected void scan(@Nullable Tree tree) { |
| 150 | + if (sleepAwait != null) { |
| 151 | + return; |
| 152 | + } |
| 153 | + super.scan(tree); |
| 154 | + } |
| 155 | + |
| 156 | + private boolean isAsyncSleepCall(Expression call) { |
| 157 | + return asyncSleepTypeChecks.getOptionalForType(call.typeV2()).isPresent(); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments