Perhaps just read the deadline in the lock, then do the rest outside the
lock:
func (s *SimplePeerConn) Read(b []byte) (n int, err error) {
s.deadlineLock.RLock()
deadline := s.readDeadline
s.deadlineLock.RUnlock()
ctx := context.Background()
if !deadline.IsZero() {
dctx, cancel := context.WithDeadline(ctx, deadline)
defer cancel()
ctx = dctx
}
return s.ReadWithContext(ctx, b)
}
On Mon, Feb 6, 2017 at 4:34 PM <[email protected]> wrote:
> I have the following function:
>
> func (s *SimplePeerConn) Read(b []byte) (n int, err error) {
> ctx := context.Background()
> var maybeCancelFn context.CancelFunc
> func() {
> s.deadlineLock.RLock()
> defer s.deadlineLock.RUnlock()
> if !s.readDeadline.IsZero() {
> ctx, maybeCancelFn = context.WithDeadline(ctx, s.readDeadline)
> }
> }()
> if maybeCancelFn != nil {
> defer maybeCancelFn()
> }
> return s.ReadWithContext(ctx, b)
> }
>
> And I'm getting a possible context leak when I assign maybeCancelFn. I
> originally had context.WithCancel and then overwrote the cancelFn in the
> inner func.
> How would I idiomatically rewrite this to not see the vet error?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.