onDispose

abstract fun onDispose(callback: (event: DisposeEvent) -> Unit)(source)

Registers a handler for resource cleanup when server is shutting down.

This callback is intended for the explicit release of unmanaged resources that the server might have initialized. Invoked during the server's lifecycle termination and when the dev server is live reloaded. For example, this could be used to close a database connection.

Limitations

Not guaranteed to be invoked (e.g., abrupt termination, power loss). In other words, even though this event is provided, you have to assume there's a chance it won't get called, and you must write your server logic in a way that can recover from these events.

Use Cases

  • Useful in development for handling resource leaks during frequent server restarts due to live reloading.

  • Releasing locks on resources that would prevent the server from reacquiring them after the live reloading/restart.

Parameters

callback

Function to execute for resource disposal, receiving a DisposeEvent as parameter.