If you see many of these operation has a safety guard, you can find the check description here:

  

      github.com
  

  
    tensorflow/tensorflow/blob/master/tensorflow/lite/python/interpreter.py#L516-L542



      
      
          def _safe_to_run(self):
            """Returns true if there exist no numpy array buffers.
          
          
  This means it is safe to run tflite calls that may destroy internally
            allocated memory. This works, because in the wrapper.cc we have made
            the numpy base be the self._interpreter.
            """
            # NOTE, our tensor() call in cpp will use _interpreter as a base pointer.
            # If this environment is the only _interpreter, then the ref count should be
            # 2 (1 in self and 1 in temporary of sys.getrefcount).
            return sys.getrefcount(self._interpreter) == 2
          
          
def _ensure_safe(self):
            """Makes sure no numpy arrays pointing to internal buffers are active.
          
          
  This should be called from any function that will call a function on
            _interpreter that may reallocate memory e.g. invoke(), ...
          
          
  Raises:
              RuntimeError: If there exist numpy objects pointing to internal memory
      
    
  

  This file has been truncated. show original

  

  
    
    
  

  


I don’t think that the problem is on set_tensor and get_tensor as they are the slow (copy) API instead of tensor().
Have you tried if holding input_details and output_details is going to be similar to the WRONG pattern explained at:

  
      

      TensorFlow
  

  
    

tf.lite.Interpreter  |  TensorFlow Core v2.6.0

  Interpreter interface for TensorFlow Lite Models.


  

  
    
    
  

  


This could also clarify why probably it was working when you tried with all the code gist in a single function as these references were confined to the function scope.