Skip to content

Commit

Permalink
scheduler: Do not re-add FD finalisers on FDs that already have one.
Browse files Browse the repository at this point in the history
Fixes #65.

Commit c25dcb9 introduced a memory
leak, whereby 'schedule-tasks-for-active-fd' would keep adding fdes
finalizers on actively-used file descriptors, for example when calling
'accept' several times on the same file descriptor.

This commit fixes it by allowing 'schedule-tasks-for-active-fd' to
distinguish between "finalized file descriptor" and "file descriptor
without waiters but with a finalizer already".

* fibers/scheduler.scm (schedule-tasks-for-active-fd): Set the car of
EVENTS+WAITERS to 0, not #f.
(schedule-task-when-fd-active): Treat the cases where FD-WAITERS = #f
and FD-WAITERS = '(#f) in the same way.  In the other case, assume
ACTIVE-EVENTS is an integer.
  • Loading branch information
civodul committed Nov 9, 2022
1 parent 6e87122 commit a9c2dbb
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions fibers/scheduler.scm
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ remote kernel thread."
(#f (warn "scheduler for unknown fd" fd))
((and events+waiters (active-events . waiters))
;; First, clear the active status, as the EPOLLONESHOT has
;; deactivated our entry in the epoll set.
(set-car! events+waiters #f)
;; deactivated our entry in the epoll set. Set the car to 0, not #f, so
;; that 'schedule-tasks-for-active-fd' doesn't end up re-adding a
;; finalizer on FD.
(set-car! events+waiters 0)
(set-cdr! events+waiters '())
(unless (zero? (logand revents (logior EPOLLHUP EPOLLERR)))
(hashv-remove! (scheduler-fd-waiters sched) fd))
Expand Down Expand Up @@ -336,21 +338,19 @@ expressed as an epoll bitfield."

(let ((fd-waiters (hashv-ref (scheduler-fd-waiters sched) fd)))
(match fd-waiters
((active-events . waiters)
(set-cdr! fd-waiters (acons events task waiters))
(unless (and active-events
(= (logand events active-events) events))
(let ((active-events (logior events (or active-events 0))))
(set-car! fd-waiters active-events)
(add-fdes-finalizer! fd (fd-finalizer fd-waiters))
(epoll-add*! (scheduler-epfd sched) fd
(logior active-events EPOLLONESHOT)))))
(#f
((or #f (#f)) ;FD is new or was finalized
(let ((fd-waiters (list events (cons events task))))
(hashv-set! (scheduler-fd-waiters sched) fd fd-waiters)
(add-fdes-finalizer! fd (fd-finalizer fd-waiters))
(epoll-add*! (scheduler-epfd sched) fd
(logior events EPOLLONESHOT)))))))
(logior events EPOLLONESHOT))))
((active-events . waiters)
(set-cdr! fd-waiters (acons events task waiters))
(unless (= (logand events active-events) events)
(let ((active-events (logior events active-events)))
(set-car! fd-waiters active-events)
(epoll-add*! (scheduler-epfd sched) fd
(logior active-events EPOLLONESHOT))))))))

(define (schedule-task-when-fd-readable sched fd task)
"Arrange to schedule @var{task} on @var{sched} when the file
Expand Down

0 comments on commit a9c2dbb

Please sign in to comment.