Skip to content

Go Substrate Client - Thread safety #1023

@sameh-farouk

Description

@sameh-farouk

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions