diff --git a/vendored/libfuse/lib/fuse_loop.cpp b/vendored/libfuse/lib/fuse_loop.cpp index 6f84980e..b28f535f 100644 --- a/vendored/libfuse/lib/fuse_loop.cpp +++ b/vendored/libfuse/lib/fuse_loop.cpp @@ -97,9 +97,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"); @@ -109,7 +112,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) @@ -117,7 +120,6 @@ struct AsyncWorker if(::_retriable_receive_error(rv)) continue; - msgbuf_free(msgbuf); if((rv == 0) || (rv == -ENODEV)) return; @@ -125,7 +127,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); @@ -164,9 +166,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"); @@ -176,23 +180,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()); } } };