Skip to content

Commit

Permalink
fix: fd operator segment fault because not being cached
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantxie committed Sep 10, 2024
1 parent e6ec47b commit af6ff62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
5 changes: 5 additions & 0 deletions net_netfd.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func (c *netFD) connect(ctx context.Context, la, ra syscall.Sockaddr) (rsa sysca
}

c.pd = newPollDesc(c.fd)
defer func() {
// free operator to avoid leak
c.pd.operator.Free()
c.pd = nil
}()
for {
// Performing multiple connect system calls on a
// non-blocking socket under Unix variants does not
Expand Down
14 changes: 5 additions & 9 deletions net_polldesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import (
func newPollDesc(fd int) *pollDesc {
pd := &pollDesc{}
poll := pollmanager.Pick()
pd.operator = &FDOperator{
poll: poll,
FD: fd,
OnWrite: pd.onwrite,
OnHup: pd.onhup,
}
pd.operator = poll.Alloc()
pd.operator.poll = poll
pd.operator.FD = fd
pd.operator.OnWrite = pd.onwrite
pd.operator.OnHup = pd.onhup
pd.writeTrigger = make(chan struct{})
pd.closeTrigger = make(chan struct{})
return pd
Expand Down Expand Up @@ -60,10 +59,7 @@ func (pd *pollDesc) WaitWrite(ctx context.Context) (err error) {
err = nil
case <-ctx.Done(): // triggered by ctx
// deregister from poller, upper caller function will close fd
// detach first but there's a very small possibility that operator is doing in poller,
// so need call unused() to wait operator done
pd.detach()
pd.operator.unused()
err = mapErr(ctx.Err())
}
// double check close trigger
Expand Down

0 comments on commit af6ff62

Please sign in to comment.