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

async: ensure connection close error is set #180

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions async.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type Async struct {
stale []*packet.Packet
logger types.Logger
wg sync.WaitGroup
error atomic.Value
errorMu sync.RWMutex
error error
streamsMu sync.Mutex
streams map[uint16]*Stream
newStreamHandlerMu sync.Mutex
Expand Down Expand Up @@ -241,11 +242,9 @@ func (c *Async) Logger() types.Logger {

// Error returns the error that caused the frisbee.Async connection to close
func (c *Async) Error() error {
err := c.error.Load()
if err == nil {
return nil
}
return err.(error)
c.errorMu.RLock()
defer c.errorMu.RUnlock()
return c.error
}

// Closed returns whether the frisbee.Async connection is closed
Expand Down Expand Up @@ -424,12 +423,16 @@ func (c *Async) close() error {
}

func (c *Async) closeWithError(err error) error {
c.errorMu.Lock()
defer c.errorMu.Unlock()

c.error = err
closeError := c.close()
if closeError != nil {
c.Logger().Debug().Err(closeError).Msgf("attempted to close connection with error `%s`, but got error while closing", err)
return closeError
c.error = errors.Join(closeError, err)
return c.error
}
c.error.Store(err)
_ = c.conn.Close()
return err
}
Expand Down