kv: Set finished before the stdin handler removes itself #216

Merged
vitalif merged 1 commits from koolkhel/vitastor:epoll-handler-copy into master 2026-12-04 00:19:52 +03:00
Contributor

Description

vitastor-kv's interactive stdin handler (src/kv/kv_cli.cpp:197-201) removes its own FD handler on EPOLLRDHUP and then sets finished = true. set_fd_handler(0, false, NULL) erases the handler's std::function from epoll_handlers while the lambda is still running, so finished is reached through the captured this in the freed closure. Set the flag first; the removal is then the last thing the handler does.

Found: by reading the code, then reproduced: vitastor-kv built with ASAN, against a local etcd, with stdin a socket whose peer closes. Practical impact is small: it needs stdin to be a socket, and the process is about to exit anyway.

Tested: that run reports heap-use-after-free at kv_cli.cpp:200 on master (4 of 4 runs) and exits cleanly with the fix (3 of 3).

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description vitastor-kv's interactive stdin handler (src/kv/kv_cli.cpp:197-201) removes its own FD handler on EPOLLRDHUP and then sets `finished = true`. `set_fd_handler(0, false, NULL)` erases the handler's std::function from `epoll_handlers` while the lambda is still running, so `finished` is reached through the captured `this` in the freed closure. Set the flag first; the removal is then the last thing the handler does. Found: by reading the code, then reproduced: vitastor-kv built with ASAN, against a local etcd, with stdin a socket whose peer closes. Practical impact is small: it needs stdin to be a socket, and the process is about to exit anyway. Tested: that run reports heap-use-after-free at kv_cli.cpp:200 on master (4 of 4 runs) and exits cleanly with the fix (3 of 3). ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-12-01 21:25:19 +03:00
handle_events() and the initial poll timer call the handler by reference
to the std::function inside its epoll_handlers node. A handler that
removes its own FD erases that node, and one that replaces its handler
assigns over it, so the closure is destroyed while it runs; vitastor-kv's
stdin handler then reads `this` from the freed closure to set `finished`.
Call a copy instead.
koolkhel added 1 commit 2026-12-02 00:03:02 +03:00
The byte written before set_fd_handler() was also reported by epoll,
which ran the handler before the 1 us timer, so the timer path was never
tested. Drain it after set_fd_handler(): only the timer reports it now.
koolkhel changed title from epoll_manager: Call FD handlers through a copy to kv: Set finished before the stdin handler removes itself 2026-12-02 16:28:57 +03:00
koolkhel force-pushed epoll-handler-copy from c7db8d4b06 to 4f9a123dce 2026-12-02 16:28:58 +03:00 Compare
Author
Contributor

Reworked as asked: the fix is now on the kv side (set finished before the stdin handler removes itself), epoll_manager is unchanged and test_epoll_manager is dropped.

Reworked as asked: the fix is now on the kv side (set `finished` before the stdin handler removes itself), epoll_manager is unchanged and test_epoll_manager is dropped.
vitalif merged commit 265f00cd3f into master 2026-12-04 00:19:52 +03:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#216
No description provided.