ChainSafe / ChainSafe/gossamer

refactor: close channel by the goroutine that write to it or don't close it

Open
#3,978 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
454
Forks
144
PR merge metrics
No merged PRs in 30d

Description

## Issue summary
Inside methods Start of metrics and pprof the channel done os closed:
```go
go s.server.Run(ctx, ready, s.done)

select {
case <-ready:
return nil
case err := <-s.done:
close(s.done)
if err != nil {
return err
}
return ErrServerDoneBeforeReady
}
```

This is not the proper place to close the channel. In Go, it's generally recommended to close the channel by the goroutine that writes to it, especially if you want to signal to other goroutines that no more data will be sent through that channel. This helps in avoiding deadlocks and ensures that receiving goroutines can safely iterate over the channel without getting stuck indefinitely.

However, there are situations where you might not need to explicitly close the channel. If the channel's lifetime is tied to the lifetime of the program, and you know that it won't be used after a certain point, you can let it be garbage collected without closing it explicitly.

Here is probably better to remove close(s.done).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.