# Stop module exit

**URL:** <https://discourse.slicer.org/t/stop-module-exit/17986>\
**Category:** Development\
**Created:** [June 7, 2021, 2:39am UTC](https://discourse.slicer.org/t/stop-module-exit/17986 "2021-06-07T02:39:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giovform](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/giovform/32/4687_2.png) [@giovform](https://discourse.slicer.org/u/giovform)\
**Post date:** [June 7, 2021, 2:39am UTC](https://discourse.slicer.org/t/stop-module-exit/17986/1 "2021-06-07T02:39:54Z")

</div>

Hello! Is there a way to stop a module from exiting, from inside its own exit() function? I would like to offer the user the option to cancel his exit from the module to allow him finish some operations that he may want to do before exiting.

For example, he clicks another module and gets a prompt: “Are you sure you want to exit the module?”

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 7, 2021, 3:58am UTC](https://discourse.slicer.org/t/stop-module-exit/17986/2 "2021-06-07T03:58:56Z")

</div>

A Python module should never call `exit()`. I would recommend to ask the developer to remove this call. If the developer does not respond to this then you can use the module in a separate process (for example, run it in a Python CLI module).

If you mean that you would like to write a module that closes the application gracefully (lets the user decide to actually quit or not) then you can use `slicer.util.mainWindow().close()`.

---

<div class="post-metadata">

**Author:** ![giovform](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/giovform/32/4687_2.png) [@giovform](https://discourse.slicer.org/u/giovform)\
**Post date:** [June 7, 2021, 1:06pm UTC](https://discourse.slicer.org/t/stop-module-exit/17986/3 "2021-06-07T13:06:04Z")

</div>

Hi Andras, I mean the **exit(self)** function that is called when you exit a module.

```auto
  def enter(self):
    self.interfaceFrame.enabled = False
    self.setupDialog()

  def exit(self):
    self.removeInteractorObservers()

```

The code above is from the Landmark Registration module, as an example.

A separate but important issue is that when I close Slicer, the **dialog asking to save the scene** appears **before the call to** **exit(self)** from the current module. Shouldn’t it be **after**? (the developer may want to do some changes on the scene within the current module **exit(self)**, before allowing saving the scene, and thus keep those changes)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 7, 2021, 6:38pm UTC](https://discourse.slicer.org/t/stop-module-exit/17986/4 "2021-06-07T18:38:24Z")

</div>

Once the method’s `exit` method is called, exiting the module is already in progress. You can always apply hacks, such as starting a timer in the `exit` method and switch back to the current module when the timer elapses, but the proper solution is to only display actions to the user that he is not allowed to perform. For example, you can hide the module selector toolbar.

> [@giovform](#):
>
> A separate but important issue is that when I close Slicer, the **dialog asking to save the scene** appears **before the call to** **exit(self)** from the current module. Shouldn’t it be **after**? (the developer may want to do some changes on the scene within the current module **exit(self)** , before allowing saving the scene, and thus keep those changes)

All persistent information must be stored in the scene. If there is really absolutely no other way than doing some last-minute node updates then you can find some hacks (e.g., [override default scene save dialog](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#override-default-scene-save-dialog) where you first do what you need and then call the default dialog), but I would not recommend to do this. Storing information in the scene is not required only because of scene saving but the application and all modules assume that information in the scene is up-to-date and you don’t withhold (or privately cache) anything essential.
