Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropping Root outside Runtime.run panics #28

Open
Grinshpon opened this issue Jan 11, 2021 · 3 comments
Open

Dropping Root outside Runtime.run panics #28

Grinshpon opened this issue Jan 11, 2021 · 3 comments

Comments

@Grinshpon
Copy link

In a project, I have a state struct that contains a glsp runtime and a struct that holds callbacks:

struct Callbacks {
  //various callbacks of type: Option<Root<GFn>>
}

struct State {
  callbacks: Callbacks,
  runtime: Runtime,
  ...
}

At the very end of the program, when the state is dropped, it panics with the error: a Root or Gc has outlived its originating Runtime - aborting process

I then tried manually dropping the callbacks before the end of the program, and it seems the panic occurs there, so it looks like I can't drop Roots unless I'm in a runtime environment. Is this intentional?

@fleabitdev
Copy link
Owner

Thanks for the report!

This is intentional and documented. The best way to work around it is to construct your Runtime at the very start of your main function, run it immediately, and keep running it until the very end of your main function, as documented here.

Leaving this issue open, because the rule should be documented more prominently, particularly in the rustdoc pages for Root, Gc and Val. We should probably add a more explicit warning to the-glsp-crate.md, too. This rule used to be documented alongside the GSend marker trait, but that trait was removed in version 0.2, so the documentation is currently a little sparse.

@Grinshpon
Copy link
Author

Ah, I see, thank you!

Just curious, is there a significant performance difference in calling run once and keeping it active, versus calling it once or more each frame?

@fleabitdev
Copy link
Owner

run is basically free! It just changes the value of a thread-local Option<Rc<_>>, and then changes it back after running the closure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants