code: public Stage cancelAndTriggerRelevantStages(Long stageId, Username username, LocalizedOperationResult result) throws Exception {
          Stage stageForId;
          LocalizedOperationResult opResult = result == null ? new DefaultLocalizedOperationResult() : result;
          try {
              stageForId = stageService.stageById(stageId);
          } catch (Exception e) {
              LOGGER.error("[Stage Cancellation] Failed to retrieve stage identifier", e);
              opResult.notFound("Stage '" + stageId + "' not found.", HealthStateType.general(HealthStateScope.GLOBAL));
              return null;
          }
  
          if (!stageForId.isActive()) {
              opResult.setMessage("Stage is not active. Cancellation Ignored.");
              return stageForId;
          }
  
          String stageMutex = mutexForStageInstance(stageForId.getIdentifier());
          synchronized (stageMutex) {
              // reload stage so we see committed state after acquiring mutex
              final Stage stage = stageService.stageById(stageId);
  
              String pipelineName = stage.getIdentifier().getPipelineName();
              String stageName = stage.getIdentifier().getStageName();
              String user = username == null ? null : username.getUsername().toString();
  
              if (!securityService.hasOperatePermissionForStage(pipelineName, stageName, user)) {
                  opResult.forbidden("Unauthorized to operate stage named " + stageName, HealthStateType.forbidden());
                  return null;
              }
  
              LOGGER.info("[Stage Cancellation] Cancelling stage {}", stage.getIdentifier());
              transactionTemplate.executeWithExceptionHandling(new com.thoughtworks.go.server.transaction.TransactionCallbackWithoutResult() {
                  @Override
                  public void doInTransactionWithoutResult(TransactionStatus status) {
                      stageService.cancelStage(stage, user);
                  }
              });
  
              transactionTemplate.executeWithExceptionHandling(new com.thoughtworks.go.server.transaction.TransactionCallbackWithoutResult() {
                  @Override
                  public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
                      automaticallyTriggerRelevantStagesFollowingCompletionOf(stage);
                  }
              });
  
              opResult.setMessage("Stage cancelled successfully.");
  
              return stage;
          }
      }
docs: synchronized for updating job