code: def fail_stale_trials(study: "optuna.Study") -> None:
      """Fail stale trials and run their failure callbacks.
  
      The running trials whose heartbeat has not been updated for a long time will be failed,
      that is, those states will be changed to :obj:`~optuna.trial.TrialState.FAIL`.
  
      .. seealso::
  
          See :class:`~optuna.storages.RDBStorage`.
  
      Args:
          study:
              Study holding the trials to check.
      """
      storage = study._storage
  
      if not isinstance(storage, BaseHeartbeat):
          return
  
      if not is_heartbeat_enabled(storage):
          return
  
      failed_trial_ids = []
      for trial_id in storage._get_stale_trial_ids(study._study_id):
          try:
              if storage.set_trial_state_values(
                  trial_id, state=TrialState.FAIL
              ):
                  failed_trial_ids.append(trial_id)
          except RuntimeError:
              # If another process fails the trial, the storage raises RuntimeError.
              pass
  
      failed_trial_callback = storage.get_failed_trial_callback()
      if failed_trial_callback is not None:
          for trial_id in failed_trial_ids:
              failed_trial = copy.deepcopy(storage.get_trial(trial_id))
              failed_trial_callback(study, failed_trial)
  
imports: import abc
  import copy
  from threading import Event
  from threading import Thread
  from types import TracebackType
  from typing import Callable
  from typing import List
  from typing import Optional
  from typing import Type
  import optuna
  from optuna._experimental import experimental_func
  from optuna.storages import BaseStorage
  from optuna.trial import FrozenTrial
  from optuna.trial import TrialState
label_desc: Use image node_id 2 for calling a global function with example usage: fail_stale_trials(study) without return types