Skip to content

LightningEnvironment finalizer exception #200

@p-lindberg

Description

@p-lindberg

I'm curious about the following exception, thrown inside the Dispose method of LightningEnvironment:

if(!disposing)
throw new InvalidOperationException("The LightningEnvironment was not disposed and cannot be reliably dealt with from the finalizer");

What is the rationale behind it? Is it only because there is no guarantee that it's safe to dispose the environment from a finalizer?

In my use case, which is inside a Unity game, it causes some issues. We don't always have control over when finalizers are called due to the fact that the runtime can hot-reload assemblies. When this happens, all objects are finalized immediately. This triggers the exception, unless we take steps to detect the assembly reload and manually dispose before it happens (which is my current workaround).

What's more; the exception itself just gets swallowed by the runtime, as, at least in the Mono runtime, exceptions in finalizers are not propagated anywhere. The only way to notice that it actually occurs is to have the debugger connected and place a breakpoint on the exception. Luckily, I noticed this while debugging something else. The unfortunate effect of this is that the remainder of the disposal method does not run, which leaves the environment open. This could potentially be an even less desirable outcome than what might happen if the environment is closed at an inopportune moment.

Would you be open to reconsidering this approach? I believe it may be better to just proceed with the disposal regardless, as that would at least result in a best-effort cleanup in all cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions