Skip to content

Commit 93986ef

Browse files
committed
Fix kqueue FDPoller GC crash on Darwin
1 parent c84d634 commit 93986ef

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

common/bufio/fd_poller_darwin.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,22 @@ func (p *FDPoller) Add(handler FDHandler, fd int) error {
9494
p.registrationCounter++
9595
registrationID := p.registrationCounter
9696

97+
entry := &fdDemuxEntry{
98+
fd: fd,
99+
registrationID: registrationID,
100+
handler: handler,
101+
}
102+
97103
_, err := unix.Kevent(p.kqueueFD, []unix.Kevent_t{{
98104
Ident: uint64(fd),
99105
Filter: unix.EVFILT_READ,
100106
Flags: unix.EV_ADD | unix.EV_ONESHOT,
101-
Udata: (*byte)(unsafe.Pointer(uintptr(registrationID))),
107+
Udata: (*byte)(unsafe.Pointer(entry)),
102108
}}, nil, nil)
103109
if err != nil {
104110
return err
105111
}
106112

107-
entry := &fdDemuxEntry{
108-
fd: fd,
109-
registrationID: registrationID,
110-
handler: handler,
111-
}
112113
p.entries[fd] = entry
113114
p.registrationToFD[registrationID] = fd
114115

@@ -208,26 +209,20 @@ func (p *FDPoller) run() {
208209
continue
209210
}
210211

211-
registrationID := uint64(uintptr(unsafe.Pointer(event.Udata)))
212+
eventEntry := (*fdDemuxEntry)(unsafe.Pointer(event.Udata))
212213

213214
p.mutex.Lock()
214-
mappedFD, ok := p.registrationToFD[registrationID]
215-
if !ok || mappedFD != fd {
216-
p.mutex.Unlock()
217-
continue
218-
}
219-
220-
entry := p.entries[fd]
221-
if entry == nil || entry.registrationID != registrationID {
215+
currentEntry := p.entries[fd]
216+
if currentEntry != eventEntry {
222217
p.mutex.Unlock()
223218
continue
224219
}
225220

226-
delete(p.registrationToFD, registrationID)
221+
delete(p.registrationToFD, currentEntry.registrationID)
227222
delete(p.entries, fd)
228223
p.mutex.Unlock()
229224

230-
go entry.handler.HandleFDEvent()
225+
go currentEntry.handler.HandleFDEvent()
231226
}
232227

233228
p.mutex.Lock()

0 commit comments

Comments
 (0)