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

simplify break condition for epoch ticker thread #2717

Merged
merged 1 commit into from
Aug 16, 2024

Conversation

benbrandt
Copy link
Contributor

@benbrandt benbrandt commented Aug 15, 2024

It seems that the crossbeam channel was just being used as a way to exit the ticker loop. Once the sender was dropped, then try_recv would fail and the loop would break.

However, it also seems, based on the wasmtime docs that just upgrading a weak reference to the Arc should serve roughly the same purpose. Once all references to the Engine are dropped, then the loop should break.

If there was a reason to use a channel here because there are potential references to the Engine Arc I didn't catch in the codebase, then feel free to ignore.

Thanks!

@benbrandt benbrandt force-pushed the simplify-epoch-increment-thread branch from 437ae59 to 6ab6524 Compare August 15, 2024 14:25
@rylev rylev self-requested a review August 15, 2024 15:26
Signed-off-by: Ben Brandt <benjamin.j.brandt@gmail.com>
@benbrandt benbrandt force-pushed the simplify-epoch-increment-thread branch from 6ab6524 to 56253c5 Compare August 15, 2024 16:28
@lann
Copy link
Collaborator

lann commented Aug 15, 2024

However, it also seems, based on the wasmtime docs that just upgrading a weak reference to the Arc should serve roughly the same purpose. Once all references to the Engine are dropped, then the loop should break.

Seems a little fragile to me since any consumer could clone the Engine, but given the low impact of leaking this thread its probably fine. 🤷

In any case I certainly can't remember why I pulled in crossbeam-channel for this. 🤔

@benbrandt
Copy link
Contributor Author

@lann this was also my thought... It does seem fragile, but should the ticker keep going as long as there is potentially something using the engine?

Open to reasons why this might be a bad idea, not sure how often the engine gets cloned. I mostly opened this as a learning exercise. If it simplified things, great. If it introduces an issue, I'm curious to know why.

@lann
Copy link
Collaborator

lann commented Aug 15, 2024

Oh sorry, I should have added: I'm fine with merging this. 🙂 I saw that @rylev added himself as a reviewer and want to give him a chance to weigh in.

@benbrandt
Copy link
Contributor Author

@lann all good, I think I caught that 😄

Another option I guess would be to store the handle to the thread and make sure it shuts down in a drop handler or something?

@lann
Copy link
Collaborator

lann commented Aug 15, 2024

After reviewing how all of this is supposed to work I think this PR actually makes this crate safer to use: If a consumer of this crate wanted to shut down the engine (but not the entire process) while a guest instance was running, killing the epoch ticker thread could leave a misbehaving guest running indefinitely where it would normally time out. By tying the thread lifetime to the lifetime of the wasmtime::Engine that should become impossible.

I don't think this is relevant to any of the actual Spin trigger handlers (which always terminate immediately after dropping the engine anyway), but on balance I think this is an improvement.

Copy link
Collaborator

@lann lann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'll still wait for @rylev to take a look but I'm more confident this should merge)

Copy link
Collaborator

@rylev rylev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also initially worried about this being fragile, but on further thinking, this seems to be exactly the behavior we want. Thanks so much 🎉

@rylev rylev merged commit e043361 into spinframework:main Aug 16, 2024
17 checks passed
@benbrandt
Copy link
Contributor Author

benbrandt commented Aug 16, 2024

@lann and @rylev thanks for the review!

Thinking on it more, it might feel less fragile if this thread was owned by the same struct as the actual engine, inside the Arc, so it could be cleaned up on Drop. But this would have to be in wasmtime itself I guess and since it isn't always used in this mode might also not be the right place for it.

But I appreciate you all taking time to validate the idea, glad it helps!

@benbrandt benbrandt deleted the simplify-epoch-increment-thread branch August 16, 2024 08:09
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

Successfully merging this pull request may close these issues.

3 participants