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

🐛 Workers sharing the same modules registry can result in unexpected behaviors #2364

Open
dario-piotrowicz opened this issue Jul 8, 2024 · 9 comments
Assignees

Comments

@dario-piotrowicz
Copy link
Member

As discussed with @jasnell each isolate gets its own global module registry that is shared and reused for the lifetime of the isolate.

Meaning that different worker runs can share the same module registry, in case modules include their own state this means that such state can be shared across different worker runs and this can result in incorrect/unexpected behaviors and even data leakage.

I've seen this creating issues in the wild (cloudflare/next-on-pages#805) and I can imagine this potentially being problematic with any library that relies on top level modules state (but I am not sure how common that is).

Although unrealistic here's an example minimal reproduction of the issue: https://github.com/dario-piotrowicz/workerd-modules-sharing-issue-reproduction/

@dario-piotrowicz
Copy link
Member Author

cc. @IgorMinar @irvinebroque

@kentonv
Copy link
Member

kentonv commented Jul 8, 2024

Why is this unexpected? Isn't this how it works on all runtimes? A module is only instantiated the first time it is imported, and then reused through the lifetime of the isolate. In Node.js, the one copy of the module would be used for the entire process lifetime.

If you store per-session state in a global you are going to have trouble in any runtime, no?

@irvinebroque
Copy link
Collaborator

I think this is an issue of people used to building for web browsers, then trying to build things on the backend.

Ex: consider the example code that Dario shared — if you were to store per-session state in a global, client-side, in a browser, that'd be fine. You're doing so only in the context of a particular user.

Then you develop that same way on the server, without thinking about it, almost just by muscle memory. And you've heard that serverless functions are "stateless", and so you maybe take a mental model that any global or local state you define gets blown away on each invocation, akin to reloading a webpage.

That's where people end up getting confused. (not saying anything about what we should or shouldn't do here, just laying out how I think someone can end up getting mixed up)

@kentonv
Copy link
Member

kentonv commented Jul 10, 2024

Closing this because it's working as designed. Creating a new isolate for every request would, at present, be way too expensive to be feasible.

(That said, we do actually have some designs on changing that, but it requires deep V8 changes. "Lightweight isolate cloning" is what we call it. Still just an idea at this point.)

@kentonv kentonv closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2024
@jasnell
Copy link
Member

jasnell commented Jul 10, 2024

Reopening this as it's still something I'm actively investigating to see if there's anything reasonable we can do here.

@jasnell jasnell reopened this Jul 10, 2024
@dario-piotrowicz
Copy link
Member Author

PS: sorry for the late reply, I somehow missed the responses 🙇

@kentonv I totally agree with you that this behavior is normal and actually expected. My argument here is that in our platform it can result in unexpected behaviors and, depending on the modules users use this is something that can very likely be out of their control.

I just opened the issue to see if we could improve this as it can be really problematic for developers (who again, might not have viable workarounds for it). I am not sure if something can/should be done here, but I feel like it was (hopefully) worth raising the concern 🙂

PS: I also totally agree with @irvinebroque's comment, but again, my worry is less regarding devs actually doing this but they inheriting unexpected behaviors because of this from libraries.

@jasnell jasnell self-assigned this Jul 10, 2024
@kentonv
Copy link
Member

kentonv commented Jul 11, 2024

Does someone have some kind of proposal for what "we can do here"? We've spent a lot of time discussing this over the years and the only proposal that has ever seemed feasible is lightweight isolate cloning.

@jasnell
Copy link
Member

jasnell commented Jul 11, 2024

... have some kind of proposal for what "we can do here"? ...

Working on formulating one. Whether I'll be successful in doing so remains to be seen. My current energy is focused on nodejs-compat work but I'll be returning the module registry related stuff shortly and will be actively working on this. In the meantime there's no harm in keeping the issue open and when I've got something more concrete I'll update.

@IgorMinar
Copy link
Collaborator

IgorMinar commented Jul 13, 2024 via email

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

5 participants