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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goose isn't thread safe? 馃 #587

Closed
miparnisari opened this issue Aug 23, 2023 · 3 comments
Closed

Goose isn't thread safe? 馃 #587

miparnisari opened this issue Aug 23, 2023 · 3 comments

Comments

@miparnisari
Copy link

miparnisari commented Aug 23, 2023

This is about functions goose.SetLogger and goose.SetBaseFS:

image

@mfridman
Copy link
Collaborator

mfridman commented Aug 23, 2023

That's correct, there are parts of the public API that are not thread-safe, such as the global setters you've identified.

In practice, most projects initialize goose settings once, so it won't be an issue. However, when using the goose library in go tests that run concurrently there will likely be issues if 2 goroutines are trying to set the global settings (as of now, v3.15.0, there are no mutexes).

I would very much like to fix this by adding the concept of a *goose.Provider so all settings are scoped to the provider and within the same runtime you could have as many providers as you'd like.

#379 and #507

I had a /v4 branch that added a provider and refactored a lot of the project, but I'm considering porting the provider bits into the current /v3 module and iterating on that.

@mfridman
Copy link
Collaborator

mfridman commented Aug 25, 2023

There could be an opportunity to add a lock within the goose package itself to protect the global setters, which would eliminate the race conditions.

It's important to be aware that these operations involve modifying global settings. Using a setter function repeatedly might lead to unpredictable outcomes.

Another way to resolve this issue involves the func TestMain() approach, which allows you to centralize all initialization tasks within a single location.

@mfridman
Copy link
Collaborator

We recently (v3.16.0 and higher) added a Provider abstraction into the core library, which is safe for concurrent use.

There's a bit more information in the Provider blog post:

https://pressly.github.io/goose/blog/2023/goose-provider/

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