# How to ensure module is actually removed on shutdown()

**URL:** <https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085>\
**Category:** Module Development\
**Created:** [March 29, 2024, 7:06pm UTC](https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085 "2024-03-29T19:06:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Miller](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/allen_miller/32/43816_2.png) [@Allen\_Miller](https://forum.inductiveautomation.com/u/Allen_Miller)\
**Post date:** [March 29, 2024, 7:06pm UTC](https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085/1 "2024-03-29T19:06:16Z")

</div>

## Problem

We are working on a module that has unexpected behavior when restarted or uninstalled/reinstalled. I believe these issues are caused by resources that are not removed by the module `shutdown()` method, and I'd like to better understand how we can locate these resources to ensure they are _all_ removed.

## Background

The module we are working on throws errors in the logs and unexpected behavior occurs when the module is restarted or uninstalled/reinstalled. In every instance, this behavior is fixed by restarting the gateway.

From the [SDK Programmer's Guide](https://www.sdk-docs.inductiveautomation.com/docs/programming-for-the-gateway/#shutdown):

> **Important:** You should take special care to remove all resources added by the module to the platform. If any resource is left uncollected, the entire module will remain in memory, leading to a memory leak, which will be particularly troublesome if the module is reloaded several times in one gateway session.

## Progress made

I think the above statement typifies the issues we're seeing - code continues to execute on the gateway from the uninstalled module, which causes errors or unexpected behavior when the module is subsequently restarted. We have begun solving these issues by terminating the objects created - specifically, calling `.shutdown()` or `.close()` methods on some objects.

These attempts have been successful - the errors or unexpected behavior has ceased once that object is removed.

## Question

How do we ensure that _all_ resources have been removed during module `shutdown()`? I'm looking for a general approach to locate and solve these issues when they occur, as well as any coding best practices to prevent them from happening in the first place.

Thanks for taking a look!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 29, 2024, 8:00pm UTC](https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085/2 "2024-03-29T20:00:03Z")

</div>

There's no general panacea. Static analysis tools and IDE inspections can help identify possible leaks, but there's no single answer (even Rust programs can have memory leaks given sufficient programmer effort).

All that said... Module reloading is going away entirely in 8.3, so in _this particular area_ you can just get to "good enough" for 8.1.

---

<div class="post-metadata">

**Author:** ![Allen\_Miller](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/allen_miller/32/43816_2.png) [@Allen\_Miller](https://forum.inductiveautomation.com/u/Allen_Miller)\
**Post date:** [March 29, 2024, 8:10pm UTC](https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085/3 "2024-03-29T20:10:46Z")

</div>

> There's no general panacea. Static analysis tools and IDE inspections can help identify possible leaks, but there's no single answer

That's what I feared, but thanks for clarifying! I'll look into static analysis tools; is there anything you've used in the past you can recommend?

> Module reloading is going away entirely in 8.3, so in _this particular area_ you can just get to "good enough" for 8.1

This is the first I've heard of that - do you mean the module `restart` button is going away, or is something more fundamental changing?

> even Rust programs can have memory leaks given sufficient programmer effort

Challenge accepted 🕶 Haha thanks for the laugh and the help!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 29, 2024, 8:21pm UTC](https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085/4 "2024-03-29T20:21:42Z")

</div>

We use SonarQube internally. It's... okay.

Module reloading as a concept is going away. The gateway will start with whatever modules were installed, and there's a new state of "pending install" or something like it that basically means "the next time you start the gateway, this module will be added to the list of modules."

But it won't be possible, through any mechanism, to restart the entire module. It's up to you if you want to try hot-reloading within your own stuff inside the module.

---

<div class="post-metadata">

**Author:** ![Allen\_Miller](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/allen_miller/32/43816_2.png) [@Allen\_Miller](https://forum.inductiveautomation.com/u/Allen_Miller)\
**Post date:** [March 29, 2024, 8:36pm UTC](https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085/5 "2024-03-29T20:36:46Z")

</div>

> The gateway will start with whatever modules were installed, and there's a new state of "pending install" or something like it that basically means "the next time you start the gateway, this module will be added to the list of modules.

Fascinating. I take it that means a gateway restart will be required for all module installs in 8.3?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 29, 2024, 8:41pm UTC](https://forum.inductiveautomation.com/t/how-to-ensure-module-is-actually-removed-on-shutdown/88085/6 "2024-03-29T20:41:58Z")

</div>

Yep. We're also definitely trying to reduce startup time as a consequence 😄
