docstr: Called when our script finishes executing, even if it finished
  early with an exception. We perform post-run cleanup here.
docstr_tok: ["Called","when","our","script","finishes","executing",",","even","if","it","finished","early","with","an","exception",".","We","perform","post-run","cleanup","here","."]
code: def _on_script_finished(
      self,
      ctx: ScriptRunContext,
      event: ScriptRunnerEvent,
      premature_stop: bool,
  ) -> None:
      """Called when our script finishes executing, even if it finished
      early with an exception. We perform post-run cleanup here.
      """
      # Tell session_state to update itself in response
      if not premature_stop:
          self._session_state.on_script_finished(
              ctx.widget_ids_this_run
          )
  
      # Signal that the script has finished. (We use SCRIPT_STOPPED_WITH_SUCCESS
      # even if we were stopped with an exception.)
      self.on_event.send(self, event=event)
  
      # Remove orphaned files now that the script has run and files in use
      # are marked as active.
      runtime.get_instance().media_file_mgr.remove_orphaned_files()
  
      # Force garbage collection to run, to help avoid memory use building up
      # This is usually not an issue, but sometimes GC takes time to kick in and
      # causes apps to go over resource limits, and forcing it to run between
      # script runs is low cost, since we aren't doing much work anyway.
      if config.get_option("runner.postScriptGC"):
          gc.collect(2)
  
imports: import gc
  import sys
  import threading
  import types
  from contextlib import contextmanager
  from enum import Enum
  from timeit import default_timer
  from typing import Callable, Dict, Optional
  from blinker import Signal
  from streamlit import config, runtime, source_util, util
  from streamlit.error_util import handle_uncaught_app_exception
  from streamlit.logger import get_logger
  from streamlit.proto.ClientState_pb2 import ClientState
  from streamlit.proto.ForwardMsg_pb2 import ForwardMsg
  from streamlit.runtime.scriptrunner.script_cache import ScriptCache
  from streamlit.runtime.scriptrunner.script_requests import RerunData, ScriptRequests, ScriptRequestType
  from streamlit.runtime.scriptrunner.script_run_context import ScriptRunContext, add_script_run_ctx, get_script_run_ctx
  from streamlit.runtime.state import SCRIPT_RUN_WITHOUT_ERRORS_KEY, SafeSessionState, SessionState
  from streamlit.runtime.uploaded_file_manager import UploadedFileManager
  from streamlit.vendor.ipython.modified_sys_path import modified_sys_path
label_desc: Use image node_id 14 for calling the ScriptRunner obj's underlying member method code with example usage: obj._on_script_finished(ctx, event, premature_stop) without return types