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

Go Substrate Client - Thread safety #1023

Open
sameh-farouk opened this issue Nov 26, 2024 · 0 comments
Open

Go Substrate Client - Thread safety #1023

sameh-farouk opened this issue Nov 26, 2024 · 0 comments
Labels
type_feature New feature or request

Comments

@sameh-farouk
Copy link
Member

sameh-farouk commented Nov 26, 2024

Currently, the Go TFChain client isn't Thread safe (as Thabet originally mentioned).

  • A major issue with the current API is that The current implementation of GetClient() has a race condition because it returns references to the internal connection state (cl and meta) that other goroutines can modify after the lock is released.

  • Also, we need to verify if the inner client used is thread-safe and revise if we need/can go with this.

One option we have to deal with GetClient is to present new safer API that use operation-based approach

func (s *Substrate) withClient(op func(Conn, Meta) error) error {
    s.mu.Lock()
    defer s.mu.Unlock()
    
    if s.closed {
        return ErrClosed
    }
    
    // Execute operation while holding the lock
    return op(s.cl, s.meta)
}
func (s *Substrate) withClientReturn(op func(Conn, Meta) (any, error)) (any, error) {
    s.mu.Lock()
    defer s.mu.Unlock()
    
    if s.closed {
        return nil, ErrClosed
    }
    
    return op(s.cl, s.meta)
}
@sameh-farouk sameh-farouk added the type_feature New feature or request label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type_feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant