[PR #2790] [MERGED] Use scope_guard for directory handles to prevent leaks #2791

Closed
opened 2026-03-04 02:07:19 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/s3fs-fuse/s3fs-fuse/pull/2790
Author: @CarstenGrohmann
Created: 1/23/2026
Status: Merged
Merged: 1/24/2026
Merged by: @ggtakec

Base: masterHead: scope_guard_for_directory_handles


📝 Commits (1)

  • 538e86a Use scope_guard for directory handles to prevent leaks

📊 Changes

3 files changed (+5 additions, -9 deletions)

View changed files

📝 src/fdcache.cpp (+2 -3)
📝 src/s3fs.cpp (+2 -2)
📝 src/s3fs_util.cpp (+1 -4)

📄 Description

This PR replaces manual closedir() calls with the existing scope_guard RAII pattern in directory-traversing functions. This ensures directory handles are properly closed even when exceptions occur.

Problem:

The affected functions iterate over directories using opendir()/readdir()/closedir(). They contain:

  1. std::string operations that can throw std::bad_alloc
  2. Recursive calls that propagate exceptions from deeper levels

When an exception is thrown, execution bypasses the manual closedir() calls, causing directory handle leaks. In long-running processes with deep directory trees, this can eventually exhaust the file descriptor limit.

Solution:

Use the existing scope_guard class (defined in s3fs_util.h) to ensure closedir() is called when the scope exits, regardless of how it exits:

scope_guard guard([dp]() { closedir(dp); });

Hint:

The code in s3fs.cpp was correct and was only modified for consistency with the rest of the codebase.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/s3fs-fuse/s3fs-fuse/pull/2790 **Author:** [@CarstenGrohmann](https://github.com/CarstenGrohmann) **Created:** 1/23/2026 **Status:** ✅ Merged **Merged:** 1/24/2026 **Merged by:** [@ggtakec](https://github.com/ggtakec) **Base:** `master` ← **Head:** `scope_guard_for_directory_handles` --- ### 📝 Commits (1) - [`538e86a`](https://github.com/s3fs-fuse/s3fs-fuse/commit/538e86ab293d16b1bd743c7a5e3c22895fb9a123) Use scope_guard for directory handles to prevent leaks ### 📊 Changes **3 files changed** (+5 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `src/fdcache.cpp` (+2 -3) 📝 `src/s3fs.cpp` (+2 -2) 📝 `src/s3fs_util.cpp` (+1 -4) </details> ### 📄 Description This PR replaces manual `closedir()` calls with the existing `scope_guard` RAII pattern in directory-traversing functions. This ensures directory handles are properly closed even when exceptions occur. **Problem:** The affected functions iterate over directories using `opendir()`/`readdir()`/`closedir()`. They contain: 1. `std::string` operations that can throw `std::bad_alloc` 2. Recursive calls that propagate exceptions from deeper levels When an exception is thrown, execution bypasses the manual closedir() calls, causing directory handle leaks. In long-running processes with deep directory trees, this can eventually exhaust the file descriptor limit. **Solution:** Use the existing scope_guard class (defined in `s3fs_util.h`) to ensure `closedir()` is called when the scope exits, regardless of how it exits: ``` scope_guard guard([dp]() { closedir(dp); }); ``` **Hint:** The code in `s3fs.cpp` was correct and was only modified for consistency with the rest of the codebase. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-04 02:07:19 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/s3fs-fuse#2791
No description provided.