From 8555c56418688a8110e0cf7308f92e5c4858bdeb Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Sat, 21 Nov 2026 14:53:44 -0500 Subject: [PATCH] fix: free a read thread's buffer when it is cancelled at shutdown - At shutdown the main loop cancels read threads that are still in receive_buf. The buffer each one had allocated for the next request was a raw pointer, so the cancel unwind left it allocated. LeakSanitizer reported it in about one daemon in six when unmounting under load. - Hold it in a unique_ptr so the unwind frees it. The async worker releases it when handing the request to the process pool, as before. --- vendored/libfuse/lib/fuse_loop.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/vendored/libfuse/lib/fuse_loop.cpp b/vendored/libfuse/lib/fuse_loop.cpp index ed57c2d8..f554dea8 100644 --- a/vendored/libfuse/lib/fuse_loop.cpp +++ b/vendored/libfuse/lib/fuse_loop.cpp @@ -96,9 +96,12 @@ struct AsyncWorker while(!fuse_session_exited(_se)) { int rv; - fuse_msgbuf_t *msgbuf; + // Freed when this iteration ends, including the unwind when the + // thread is cancelled in receive_buf at shutdown, unless handed + // to the process pool. + std::unique_ptr msgbuf(msgbuf_alloc(), + &msgbuf_free); - msgbuf = msgbuf_alloc(); if(not msgbuf) { fmt::println(stderr,"mergerfs: failure to allocate msgbuf, exiting"); @@ -108,7 +111,7 @@ struct AsyncWorker while(true) { pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); - rv = _se->receive_buf(_se,_fd,msgbuf); + rv = _se->receive_buf(_se,_fd,msgbuf.get()); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); if(rv > 0) @@ -116,7 +119,6 @@ struct AsyncWorker if(::_retriable_receive_error(rv)) continue; - msgbuf_free(msgbuf); if((rv == 0) || (rv == -ENODEV)) return; @@ -124,7 +126,7 @@ struct AsyncWorker } _process_tp->enqueue_work(ptok, - [se = _se,fd = _fd,msgbuf]() + [se = _se,fd = _fd,msgbuf = msgbuf.release()]() { se->process_buf(se,fd,msgbuf); msgbuf_free(msgbuf); @@ -163,9 +165,11 @@ struct SyncWorker while(!fuse_session_exited(_se)) { int rv; - fuse_msgbuf_t *msgbuf; + // Freed when this iteration ends, including the unwind when the + // thread is cancelled in receive_buf at shutdown. + std::unique_ptr msgbuf(msgbuf_alloc(), + &msgbuf_free); - msgbuf = msgbuf_alloc(); if(not msgbuf) { fmt::println(stderr,"mergerfs: failure to allocate msgbuf, exiting"); @@ -175,23 +179,20 @@ struct SyncWorker while(true) { pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); - rv = _se->receive_buf(_se,_fd,msgbuf); + rv = _se->receive_buf(_se,_fd,msgbuf.get()); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL); if(rv > 0) break; if(::_retriable_receive_error(rv)) continue; - msgbuf_free(msgbuf); if((rv == 0) || (rv == -ENODEV)) return; return ::_print_error(rv); } - _se->process_buf(_se,_fd,msgbuf); - - msgbuf_free(msgbuf); + _se->process_buf(_se,_fd,msgbuf.get()); } } };