• arcnmx's avatar
    event/SocketEvent: remove FD before closing socket · 0a81e462
    arcnmx authored
    SocketEvent knows the FD is still open and is about to close it, so
    it's unnecessary to rely on the kernel (via AbandonFD) to clean up the
    epoll_wait list.
    
    ### Why this is relevant
    
    - `AbandonFD` assumes that upon closing the socket, the FD will be automatically removed from the epoll list. That fd is associated with a reference to the `SocketEvent`, so this is an important and dangerous assumption to get wrong. In the case that the FD isn't immediately removed from the list by the kernel, the event loop can crash due to the `SocketEvent` being destroyed and it being a use-after-free bug at that point.
    - If a socket FD happens to be duplicated, then closing the SocketEvent FD will not automatically remove it from epoll, and will trigger said bug/crash. It is only automatically removed when all FD references to the underlying socket/resource are closed?
    - A `fork()` is one example where a socket FD can be duplicated and result in this situation.
        - `CLOEXEC` might be considered mitigation for this but also introduces a race condition where the crash can occur between a `fork()` and `exec()` without additional synchronization to freeze the event loop.
    
    One could argue the mpd event loop isn't fork-safe, and thus should be allowed to use `AbandonFD` however it likes. A decision on whether this is intended should probably be declared; but either way this fix seems appropriate in cases where `Abandon` isn't actually necessary. It also might be possible to fix `AbandonFD` to mark the `SocketEvent` as removed without using `EPOLL_CTL_DEL`?
    
    [edit: made this dependent on HAVE_THREADED_EVENT_LOOP which is always
    true for MPD, but not for ncmpc, for example - mk]
    0a81e462
Name
Last commit
Last update
.github Loading commit data...
android Loading commit data...
build Loading commit data...
doc Loading commit data...
python/build Loading commit data...
src Loading commit data...
subprojects Loading commit data...
systemd Loading commit data...
test Loading commit data...
win32 Loading commit data...
.clang-format Loading commit data...
.gitignore Loading commit data...
.travis.yml Loading commit data...
AUTHORS Loading commit data...
COPYING Loading commit data...
NEWS Loading commit data...
README.md Loading commit data...
meson.build Loading commit data...
meson_options.txt Loading commit data...
mpd.svg Loading commit data...
valgrind.suppressions Loading commit data...