Skip to content

Store context as singleton with global reference #89

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jeffcharles
Copy link
Contributor

The purpose of this change is to enable future work to access data in the context in the host environment which won't have a reference to the context. But as a nice side effect of making the context a singleton, consumers of the API can't accidentally hang a function invocation by trying to create multiple contexts.

As far as I can tell, we don't have a need for multiple contexts. If we ever find we do, the logic in here can be updated to hold a collection of created contexts instead of one context.

@jeffcharles jeffcharles force-pushed the jc.store-reference-to-context-singleton branch from af46540 to 3a9ac87 Compare June 13, 2025 18:39
@adampetro
Copy link
Contributor

As far as I can tell, we don't have a need for multiple contexts. If we ever find we do, the logic in here can be updated to hold a collection of created contexts instead of one context.

We do have a need for multiple contexts in tests. For example, we create one each time run_function_with_input is called in the shopify_function Rust crate here

@jeffcharles
Copy link
Contributor Author

We do have a need for multiple contexts in tests. For example, we create one each time run_function_with_input is called in the shopify_function Rust crate here

I should've been more specific, we don't need multiple contexts per thread. The new_with_input invocation can reset the state in current thread's context.

@adampetro
Copy link
Contributor

It wouldn't shock me if someone ended up calling new_with_input multiple times in the same thread and possibly holding onto the values at the same time, but I think it's probably unlikely so we could punt and add support for that later

@jeffcharles
Copy link
Contributor Author

Yeah I'm not sure when it would make sense to do that. FWIW, I've figured out a different way to handle logging that doesn't involve a context. But while investigating that approach, I've also realized there is very likely a way I could apply the same approach for function output (since I'll have to update that approach to replace the write to stdout in any case). I'll put together a document if prototyping that goes well.

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.

2 participants