Skip to content

libct: close the mount source fd ASAP!#5177

Merged
cyphar merged 3 commits into
opencontainers:mainfrom
lifubang:refactor-mounts
Mar 28, 2026
Merged

libct: close the mount source fd ASAP!#5177
cyphar merged 3 commits into
opencontainers:mainfrom
lifubang:refactor-mounts

Conversation

@lifubang
Copy link
Copy Markdown
Member

@lifubang lifubang commented Mar 16, 2026

This commit factors out setupAndMountToRootfs without changing any
logic. Use "Hide whitespace changes" during review to focus on the
actual changes.

The refactor ensures the mount source file descriptor is closed via
defer in each loop iteration, reducing the total number of open FDs
in runc. This helps avoid hitting the file descriptor limit under
high concurrency or when handling many mounts.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Close mount source file descriptors as soon as they are no longer needed during rootfs setup, reducing peak FD usage in runc (especially under high mount counts / concurrency).

Changes:

  • Refactors mount source-fd acquisition logic into a new initMountEntry helper.
  • Moves lifetime management of the mount source FD into mountToRootfs to close it immediately after each mount is processed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread libcontainer/rootfs_linux.go Outdated
@kolyshkin kolyshkin requested review from rata March 16, 2026 18:19
Copy link
Copy Markdown
Member

@rata rata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, are you seeing a lot fds and is causing issues for you?

Comment thread libcontainer/rootfs_linux.go Outdated
Comment thread libcontainer/rootfs_linux.go Outdated
Comment thread libcontainer/rootfs_linux.go Outdated
@lifubang
Copy link
Copy Markdown
Member Author

Just curious, are you seeing a lot fds and is causing issues for you?

This can fail under a restrictive nofile (max open files) limit when using many idmapped mounts. The second commit demonstrates the issue; without the fix in the first commit, the test fails.

Copy link
Copy Markdown
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@kolyshkin
Copy link
Copy Markdown
Contributor

Guess you'd want 1.5 backport @lifubang ?

@lifubang
Copy link
Copy Markdown
Member Author

Guess you'd want 1.5 backport @lifubang ?

Yes, it looks like a small bug.

@lifubang lifubang added this to the 1.5.0-rc.2 milestone Mar 19, 2026
@lifubang lifubang added the backport/1.5-todo A PR in main branch which needs to be backported to release-1.5 label Mar 19, 2026
@lifubang lifubang requested review from cyphar and rata March 19, 2026 08:16
Copy link
Copy Markdown
Member

@rata rata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Let's tune the error handling and simplify a little bit the code

Comment thread libcontainer/rootfs_linux.go
Comment thread libcontainer/rootfs_linux.go Outdated
Comment thread libcontainer/rootfs_linux.go Outdated
Comment thread libcontainer/rootfs_linux.go Outdated
Comment thread libcontainer/rootfs_linux.go
Signed-off-by: lifubang <lifubang@acmcoder.com>
Copy link
Copy Markdown
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For review purposes, it would be nice to split the second commit into two

  1. Factor out setupAndMountToRootfs.
  2. Implement the fix.

Otherwise it is harder to review what exactly are you changing since you also move the code around.

Still LGTM though

@kolyshkin kolyshkin requested a review from rata March 19, 2026 23:28
@cyphar
Copy link
Copy Markdown
Member

cyphar commented Mar 20, 2026

Yeah I agree @kolyshkin, it took me a couple of read-throughs to see what was being changed.

This commit factors out setupAndMountToRootfs without changing any
logic. Use "Hide whitespace changes" during review to focus on the
actual changes.

The refactor ensures the mount source file descriptor is closed via
defer in each loop iteration, reducing the total number of open FDs
in runc. This helps avoid hitting the file descriptor limit under
high concurrency or when handling many mounts.

Signed-off-by: lifubang <lifubang@acmcoder.com>
Signed-off-by: lifubang <lifubang@acmcoder.com>
@lifubang
Copy link
Copy Markdown
Member Author

2. Implement the fix.

In fact, no second fix is needed. As @rata suggested, factoring out setupAndMountToRootfs is sufficient to ensure the mount source file descriptor is closed in each loop iteration.

You can use “Hide whitespace changes” to make the review easier.

@kolyshkin
Copy link
Copy Markdown
Contributor

  1. Implement the fix.

In fact, no second fix is needed. As @rata suggested, factoring out setupAndMountToRootfs is sufficient to ensure the mount source file descriptor is closed in each loop iteration.

You can use “Hide whitespace changes” to make the review easier.

Thank you for clarification! Still LGTM )

@kolyshkin
Copy link
Copy Markdown
Contributor

@lifubang Do we want 1.4 backport for this one? Seems like a bug to me and 1.4 is to be supported for 6+ months.

Copy link
Copy Markdown
Member

@rata rata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I'm not sure about the test, but I'm fine with it. I'm going to be afk next week.

EDIT: grr, I cleartly didn't post this last week. I'm coming back now :-D

Comment on lines +119 to +122
update_config '.process.rlimits = [{
"type": "RLIMIT_NOFILE",
"soft": 20,
"hard": 20
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is using exactly 20, right?

I think there are many things that can use fds and this might be flaky or failing in the future. But if this is important for your environment, I'm fine having it. If it causes CI issues in the future, we can decide then what to do

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is using exactly 20, right?

Yes, the test explicitly adds 20 mounts. Before this patch, this would have caused a failure due to the limit.
Since runc theoretically supports an RLIMIT_NOFILE value of 1, this test case is deterministic and should not be flaky in the future.

Comment on lines +107 to +109
destname="${2:-}"
setup_idmap_single_mount 0:100000:65536 0:100000:65536 "$mountname" "$destname"
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing this? It's not really need it in the test, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary because I need to mount to more than three target directories within the container.

Copy link
Copy Markdown
Member

@cyphar cyphar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

}

// setupAndMountToRootfs sets up the mount for a single mount point and mounts it to the rootfs.
func setupAndMountToRootfs(pipe *syncSocket, config *configs.Config, mountConfig *mountConfig, m *configs.Mount) error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think something like doMount or applyMount would be a little less specific (I think setupAndMountToRootfs is too descriptive without telling you what stage in the process it is) but I don't really like any of those names to be honest... 🤔

@cyphar cyphar merged commit 7b40afb into opencontainers:main Mar 28, 2026
63 checks passed
@lifubang lifubang added the backport/1.4-todo A PR in main branch which needs to backported to release-1.4 label Mar 28, 2026
@lifubang lifubang added backport/1.4-done A PR in main branch which has been backported to release-1.4 backport/1.5-done A PR in main branch which has been backported to release-1.5 and removed backport/1.4-todo A PR in main branch which needs to backported to release-1.4 backport/1.5-todo A PR in main branch which needs to be backported to release-1.5 labels Mar 29, 2026
@kolyshkin kolyshkin removed this from the 1.5.0-rc.2 milestone Apr 2, 2026
@kolyshkin kolyshkin mentioned this pull request Apr 2, 2026
Maks1mS pushed a commit to stplr-dev/stplr that referenced this pull request Apr 5, 2026
This PR contains the following updates:

| Package | Type | Update | Change | OpenSSF |
|---|---|---|---|---|
| [github.com/opencontainers/runc](https://github.com/opencontainers/runc) | require | patch | `v1.4.1` → `v1.4.2` | [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/opencontainers/runc/badge)](https://securityscorecards.dev/viewer/?uri=github.com/opencontainers/runc) |

---

> ⚠️ **Warning**
>
> Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/23) for more information.

---

### Release Notes

<details>
<summary>opencontainers/runc (github.com/opencontainers/runc)</summary>

### [`v1.4.2`](https://github.com/opencontainers/runc/releases/tag/v1.4.2): runc v1.4.2 -- &quot;Я — Земля! Я своих провожаю питомцев&quot;

[Compare Source](opencontainers/runc@v1.4.1...v1.4.2)

This is the second patch release of the 1.4.z release series of runc.

##### Fixed

- A regression in runc v1.3.0 which can result in a stuck `runc exec` or
  `runc run` when the container process runs for a short time. ([#&#8203;5208](opencontainers/runc#5208),
  [#&#8203;5210](opencontainers/runc#5210), [#&#8203;5216](opencontainers/runc#5216))

- Mount sources that need to be open on the host are now closed earlier during
  container start, reducing the total amount of used file descriptors and
  helping to avoid hitting the open files limit when handling many such mounts.
  ([#&#8203;5177](opencontainers/runc#5177), [#&#8203;5201](opencontainers/runc#5201))

##### Static Linking Notices

The `runc` binary distributed with this release are *statically linked* with
the following [GNU LGPL-2.1][lgpl-2.1] licensed libraries, with `runc` acting
as a "work that uses the Library":

[lgpl-2.1]: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html

- [libseccomp](https://github.com/seccomp/libseccomp)

The versions of these libraries were not modified from their upstream versions,
but in order to comply with the LGPL-2.1 (§6(a)), we have attached the
complete source code for those libraries which (when combined with the attached
runc source code) may be used to exercise your rights under the LGPL-2.1.

However we strongly suggest that you make use of your distribution's packages
or download them from the authoritative upstream sources, especially since
these libraries are related to the security of your containers.

***

Thanks to the following contributors for making this release possible:

- Ayato Tokubi <atokubi@redhat.com>
- Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
- Aleksa Sarai <cyphar@cyphar.com>
- Kir Kolyshkin <kolyshkin@gmail.com>
- Li Fubang <lifubang@acmcoder.com>
- Rodrigo Campos Catelin <rodrigo@amutable.com>

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At 12:00 AM through 04:59 AM and 10:00 PM through 11:59 PM, Monday through Friday ( * 0-4,22-23 * * 1-5 ), Only on Sunday and Saturday ( * * * * 0,6 ) (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44Ni4xIiwidXBkYXRlZEluVmVyIjoiNDMuODYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiS2luZC9EZXBlbmRlbmNpZXMiXX0=-->

Reviewed-on: https://altlinux.space/stapler/stplr/pulls/387
Co-authored-by: Renovate Bot <stapler-helper-bot@noreply.altlinux.space>
Co-committed-by: Renovate Bot <stapler-helper-bot@noreply.altlinux.space>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/1.4-done A PR in main branch which has been backported to release-1.4 backport/1.5-done A PR in main branch which has been backported to release-1.5 kind/refactor refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants