ExitCallback() closes the window

I notice this unexpected behavior, calling iren.ExitCallback() closes the rendering window which pops up again at the next iren.Start():

import time
import vtk

cone = vtk.vtkConeSource()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cone.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(actor)

renWin = vtk.vtkRenderWindow()
renWin.SetSize(500, 500)
renWin.AddRenderer(ren)
renWin.Render()

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

def callb(iren, ename):
    print("calling ExitCallback")
    iren.ExitCallback()

iren.RemoveAllObservers()
iren.AddObserver('KeyPressEvent', callb)

# iren.Initialize() # needed?
iren.Start()

print("window should stay open, but it closes")
time.sleep(1)
iren.Start() # pops up again

system info:

vtk version       : 9.2.6
numpy version     : 1.24.2
python version    : 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0]
python interpreter: /home/musy/soft/miniconda3/bin/python
system            : Linux 5.4.0-144-generic posix x86_64 (Ubuntu 20.04.5 LTS)

Why are you removing all the observers, namely iren.RemoveAllObservers()?

I don’t see the window closing at all in Windows. As a tesi, I commented out iren.RemoveAllObservers() so that I could interact with the image using the mouse, and, when a key is pressed, the timer is invoked and you cannot interact with the image. Note that in this case keypress events are passed through so pressing “w” will display a wireframe before the timer is invoked. After the timer expires you can interact with the image. If you press a key again, the script exits.
Note: I increased your timer interval to 10 to demonstrate this behaviour.

The closing of the window in TerminatApp() seems to be specific to X11.

The underlying X Window seems to be destroyed and replaced with a new one:

Before:                                 After:
vtkXOpenGLRenderWindow (0x565458130300) vtkXOpenGLRenderWindow (0x565458130300)
  ContextId: 0x565458152220             ContextId: 0x56545862f100
  Display Id: 0x55df23fc04c0            Display Id: 0x565458838b70
  Window Id: 85983234                   Window Id: 88080386

Someone would have to trace their way through the code to see why TerminateApp() doesn’t just exit the loop.

Edit: It looks like the Finalize() call to close the window was added to TerminateApp() in VTK 9.1, and it has moved around a bit since then.