Skip to content

client: fix leak of file handles - #55619

Merged
vshankar merged 1 commit into
ceph:mainfrom
xhernandez:fix-leak
Mar 6, 2024
Merged

vshankar merged 1 commit into
ceph:mainfrom
xhernandez:fix-leak

Conversation

@xhernandez

@xhernandez xhernandez commented Feb 16, 2024 •

Copy link
Copy Markdown
Contributor

Based on posix specification, the fd passed to fdopendir() will be closed by closedir(). However CephFS client wasn't doing that. If the user opened a directory using ceph_openat(), for example, and then passed the returned fd to ceph_fdopendir(), the created Fh associated with the new open was never destroyed.

This patch records the fd used in ceph_fdopendir() so that it can be closed when ceph_closedir() is called.

Fixes: https://tracker.ceph.com/issues/64479

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands
  • jenkins retest this please
  • jenkins test classic perf
  • jenkins test crimson perf
  • jenkins test signed
  • jenkins test make check
  • jenkins test make check arm64
  • jenkins test submodules
  • jenkins test dashboard
  • jenkins test dashboard cephadm
  • jenkins test api
  • jenkins test docs
  • jenkins render docs
  • jenkins test ceph-volume all
  • jenkins test ceph-volume tox
  • jenkins test windows
  • jenkins test rook e2e

@github-actions github-actions Bot added the cephfs Ceph File System label Feb 16, 2024

@anoopcs9 anoopcs9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As mentioned below, commit message also needs the following correction:
s/ceph_fdopenat()/ceph_fdopendir

Comment thread src/client/Client.h Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
int fd; // fd attached using fdopenat (-1 if none)
int fd; // fd attached using fdopendir (-1 if none)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Thanks :)

@xhernandez
xhernandez force-pushed the fix-leak branch 3 times, most recently from 12fff05 to b88fc2d Compare February 16, 2024 18:14
@vshankar
vshankar requested a review from a team February 17, 2024 05:59
@vshankar

Copy link
Copy Markdown
Contributor

@xhernandez Thanks for the fix. We intent to backport this fix, so, this requires a redmine tracker (and link it in the commit message with Fixes: http://.... tag.

@xhernandez

Copy link
Copy Markdown
Contributor Author

@xhernandez Thanks for the fix. We intent to backport this fix, so, this requires a redmine tracker (and link it in the commit message with Fixes: http://.... tag.

Done

Comment thread src/client/Client.h Resolved
Based on posix specification, the fd passed to fdopendir() will be
closed by closedir(). However CephFS client wasn't doing that. If the
user opened a directory using ceph_openat(), for example, and then
passed the returned fd to ceph_fdopendir(), the created Fh associated
with the new open was never destroyed.

This patch records the fd used in ceph_fdopendir() so that it can be
closed when ceph_closedir() is called.

Fixes: https://tracker.ceph.com/issues/64479
Signed-off-by: Xavi Hernandez <[email protected]>
@dparmar18

Copy link
Copy Markdown
Contributor

it would be good to have a test case for this, you can try use Client::get_filehandle to check if the fd is still valid post closedir

@xhernandez

Copy link
Copy Markdown
Contributor Author

it would be good to have a test case for this, you can try use Client::get_filehandle to check if the fd is still valid post closedir

@dparmar18 How should I write this test ? I've seen that there are some CephFS tests that use libcephfs in src/test/libcephfs, but Client::get_filehandle() is not exported by libcephfs. Should I write a test in src/test/client ?

@dparmar18

Copy link
Copy Markdown
Contributor

it would be good to have a test case for this, you can try use Client::get_filehandle to check if the fd is still valid post closedir

@dparmar18 How should I write this test ? I've seen that there are some CephFS tests that use libcephfs in src/test/libcephfs, but Client::get_filehandle() is not exported by libcephfs. Should I write a test in src/test/client ?

yeah that would be the place

@xhernandez

Copy link
Copy Markdown
Contributor Author

it would be good to have a test case for this, you can try use Client::get_filehandle to check if the fd is still valid post closedir

@dparmar18 How should I write this test ? I've seen that there are some CephFS tests that use libcephfs in src/test/libcephfs, but Client::get_filehandle() is not exported by libcephfs. Should I write a test in src/test/client ?

yeah that would be the place

I've just realized that Client::get_filehandle() is a protected method. Is there any other way to write tests that can access internal methods ?

@dparmar18

Copy link
Copy Markdown
Contributor

it would be good to have a test case for this, you can try use Client::get_filehandle to check if the fd is still valid post closedir

@dparmar18 How should I write this test ? I've seen that there are some CephFS tests that use libcephfs in src/test/libcephfs, but Client::get_filehandle() is not exported by libcephfs. Should I write a test in src/test/client ?

yeah that would be the place

I've just realized that Client::get_filehandle() is a protected method. Is there any other way to write tests that can access internal methods ?

Right, that is a blocker then. I guess the what looks possible is to write a test case via a script and run it using a YAML

@xhernandez

Copy link
Copy Markdown
Contributor Author

I've just realized that Client::get_filehandle() is a protected method. Is there any other way to write tests that can access internal methods ?

Right, that is a blocker then. I guess the what looks possible is to write a test case via a script and run it using a YAML

I'm sorry, but I'm very new to Ceph development and I don't know much about it, specially for testing. Can you point me to some place where it's described how to write these scripts and yaml files, and how to integrate them into the regular testing procedures ?

@dparmar18

Copy link
Copy Markdown
Contributor

I've just realized that Client::get_filehandle() is a protected method. Is there any other way to write tests that can access internal methods ?

Right, that is a blocker then. I guess the what looks possible is to write a test case via a script and run it using a YAML

I'm sorry, but I'm very new to Ceph development and I don't know much about it, specially for testing. Can you point me to some place where it's described how to write these scripts and yaml files, and how to integrate them into the regular testing procedures ?

it should be simple, checkout qa/workunits/, you should find tons of scripts, and I hope this isn't getting sort of an overkill, any thoughts @vshankar

@vshankar

Copy link
Copy Markdown
Contributor

it would be good to have a test case for this, you can try use Client::get_filehandle to check if the fd is still valid post closedir

Its a bit tricky to have test cases for thing like this fix since the way the client is written. I'd defer that for now...

@dparmar18 dparmar18 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@rosinL

rosinL commented Feb 22, 2024

Copy link
Copy Markdown
Member

jenkins test make check arm64

@vshankar

Copy link
Copy Markdown
Contributor

vshankar added a commit to vshankar/ceph that referenced this pull request Mar 4, 2024
* refs/pull/55619/head:
	client: fix leak of file handles

Reviewed-by: Dhairya Parmar <[email protected]>
Reviewed-by: Venky Shankar <[email protected]>
@vshankar

vshankar commented Mar 4, 2024

Copy link
Copy Markdown
Contributor

@vshankar
vshankar merged commit 55ac8d6 into ceph:main Mar 6, 2024
mchangir pushed a commit to ceph/ceph-ci that referenced this pull request Feb 26, 2025
ceph/ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph/ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
mkogan1 pushed a commit to mkogan1/ceph that referenced this pull request Mar 17, 2025
Likely ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
(cherry picked from commit 442e645)

Resolves: rhbz#2331338
mkogan1 pushed a commit to mkogan1/ceph that referenced this pull request Mar 17, 2025
ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
(cherry picked from commit c4ce3c0)

Resolves: rhbz#2331338
soumyakoduri pushed a commit to soumyakoduri/ceph that referenced this pull request Apr 21, 2025
Likely ceph#55619 missed that.

Resolves: rhbz#2317735

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
(cherry picked from commit 442e645)
soumyakoduri pushed a commit to soumyakoduri/ceph that referenced this pull request Apr 21, 2025
ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Resolves: rhbz#2317735

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
(cherry picked from commit c4ce3c0)
joscollin pushed a commit to joscollin/ceph that referenced this pull request May 30, 2025
Likely ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
joscollin pushed a commit to joscollin/ceph that referenced this pull request May 30, 2025
ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
joscollin pushed a commit to joscollin/ceph that referenced this pull request Jun 4, 2025
Likely ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
joscollin pushed a commit to joscollin/ceph that referenced this pull request Jun 4, 2025
ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
joscollin pushed a commit to ceph/ceph-ci that referenced this pull request Jul 23, 2025
Likely ceph/ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
joscollin pushed a commit to ceph/ceph-ci that referenced this pull request Jul 23, 2025
ceph/ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph/ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
joscollin pushed a commit to joscollin/ceph that referenced this pull request Jul 30, 2025
Likely ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
joscollin pushed a commit to joscollin/ceph that referenced this pull request Jul 30, 2025
ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
joscollin pushed a commit to joscollin/ceph that referenced this pull request Aug 11, 2025
Likely ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
joscollin pushed a commit to joscollin/ceph that referenced this pull request Aug 11, 2025
ceph#55619 eliminates the need for ceph_close() call after successful ceph_fdopendir() one.
And introduces automatic file descriptor's close when corresponding ceph_closedir() is called.
That hasty ceph_close() call makes file descriptor
available for a new allocation which might conflict with the automatic fd close in the above ceph_closedir().

Full PeerReplayer::do_synchronize() has been reworked to close fds
properly, depending on whether ceph_fdopendir() has been already applied
to them.

Additionally for the sake of uniformity this reworks incremental do_synchronize()
in a way to do final fd closings similar to full implementation.

Plus this effectively reverts
ceph@bd78bdc
as it looks like a wrong approach to fight broken file descriptor
references. No much sense in reopening of the current snapshot's root folder
on each new entry processing. Instead this patch just doesn't close it from the
beginning.

Fixes: https://tracker.ceph.com/issues/68853

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7a747bc)
ifed01 added a commit to ifed01/ceph that referenced this pull request Aug 12, 2025
Likely ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
sajibreadd pushed a commit to sajibreadd/ceph that referenced this pull request Dec 5, 2025
Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit a27ed27)

Conflicts:
	src/test/libcephfs/CMakeLists.txt

libcephfs/client: pin inode/dentry for an opened directory

Fixes: https://tracker.ceph.com/issues/69092

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 7b8a86f)

test/libcephfs: remove warning in Windows build

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit c9d7345)

common/ceph_fs: Enable O_DIRECTORY|O_NOFOLLOWUP flags translation under Win
in ceph_flags_sys2wire()

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 4badc83)

cephfs-mirror-hostpapa-reef

(cherry picked from commit e904f62436db6a2eb295b7a128a5b5d42f7df68b)

cephfs/client: dir_reset_t::reset() - add missing fd reset.

Likely ceph#55619 missed that.

Signed-off-by: Igor Fedotov <[email protected]>
(cherry picked from commit 6f1d21c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cephfs Ceph File System

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants