Skip to content

include/scope_guard: make scope_guard non-movable - #73241

Open
tchaikov wants to merge 3 commits into
ceph:mainfrom
tchaikov:wip-scope-guard-move
Open

tchaikov wants to merge 3 commits into
ceph:mainfrom
tchaikov:wip-scope-guard-move

Conversation

@tchaikov

@tchaikov tchaikov commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

The defaulted move constructor left the moved-from guard armed, so a guard that was moved ran its action twice, once from each object. The defaulted move assignment threw away the action the target was holding, and the moved-from guard still ran later. It only compiled for callables that can be assigned, which rules out any lambda with
captures.

No caller moves or assigns a guard, and make_scope_guard() doesn't need to move one either: since C++17, both its return statement and "auto g = make_scope_guard(...)" construct the guard in place. So instead of fixing the move constructor, drop it. Deleting the copy constructor and copy assignment also suppresses the implicit move operations, so a guard can be neither copied nor moved.

make_scope_guard() didn't compile when given an lvalue: with F = T&, the const F& and F&& constructors end up with the same signature. Decay F, so the guard keeps its own copy of the callable.

While at it, mark the constructors [[nodiscard]], and drop the in_place_type overload of make_scope_guard(), which nobody calls.

@tchaikov

Copy link
Copy Markdown
Contributor Author

inspired by #71130 (comment)

@tchaikov

This comment was marked as outdated.

@gregsfortytwo

Copy link
Copy Markdown
Member

Should we just disable the move and copy constructors on this? We shouldn't be doing that with scope guards on stack I don't think (but I'm on mobile right now, so maybe we use scope_guard more flexibly than that).

The defaulted move constructor left the moved-from guard armed, so a
guard that was moved ran its action twice, once from each object. The
defaulted move assignment threw away the action the target was
holding, and the moved-from guard still ran later. It only compiled
for callables that can be assigned, which rules out any lambda with
captures.

No caller moves or assigns a guard, and make_scope_guard() doesn't
need to move one either: since C++17, both its return statement and
"auto g = make_scope_guard(...)" construct the guard in place. So
instead of fixing the move constructor, drop it. Deleting the copy
constructor and copy assignment also suppresses the implicit move
operations, so a guard can be neither copied nor moved.

make_scope_guard() didn't compile when given an lvalue: with F = T&,
the const F& and F&& constructors end up with the same signature.
Decay F, so the guard keeps its own copy of the callable.

While at it, mark the constructors [[nodiscard]], and drop the
in_place_type overload of make_scope_guard(), which nobody calls.

Signed-off-by: Kefu Chai <[email protected]>
scope_guard had no tests at all. Cover the fixes from the previous
commit: the guard can be neither copied nor moved, and
make_scope_guard() accepts an lvalue and copies it.

The other tests make sure the simpler implementation still handles
move-only lambdas, "scope_guard g{...}" without template arguments,
and std::in_place construction.

Signed-off-by: Kefu Chai <[email protected]>
A guard is often used for rollback: it should undo a change if the
function bails out early, and do nothing once the function gets to
the end. Without a way to turn the guard off, the caller has to keep
a bool next to the guard and test it in the lambda. ceph#71130 does
exactly that to roll back a half-created pool in
OSDMonitor::prepare_new_pool().

Add an "active" flag, and dismiss() to clear it. Put the callable
under [[no_unique_address]], so a guard holding a captureless lambda
is no bigger than the flag.

Signed-off-by: Kefu Chai <[email protected]>
@tchaikov
tchaikov force-pushed the wip-scope-guard-move branch from 9652a89 to 5523698 Compare October 29, 2026 13:38
@tchaikov tchaikov changed the title include/scope_guard: disarm moved-from guards include/scope_guard: make scope_guard non-movable Oct 29, 2026
@tchaikov

Copy link
Copy Markdown
Contributor Author

Should we just disable the move and copy constructors on this? We shouldn't be doing that with scope guards on stack I don't think (but I'm on mobile right now, so maybe we use scope_guard more flexibly than that).

then we can no longer return a named guard from a function.. but yeah, i think you are right. we are not likely to do this as in general, the scope_guard tackles the cleanup of (local) variables on stack.

dropped.

@tchaikov

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

1 similar comment
@tchaikov

tchaikov commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check arm64

@gregsfortytwo gregsfortytwo left a comment

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.

(Sorry I dropped getting back to you on this!)

I think this is okay, but honestly it's got more new C++ and wizardry than I'm familiar with. @adamemerson I think you keep up on such things; do you have a couple minutes to check it out?

@adamemerson adamemerson 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

@adamemerson

Copy link
Copy Markdown
Contributor

I honestly would have made move call dismiss on the moved-from, but either works.

@tchaikov

tchaikov commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

@adamemerson hi Adam, thanks for your review and approval. yeah, the current implementation follows Greg's suggestion at #73241 (comment) . as scope_guard is a local variable, and make_scope_guard() can still return a guard directly thru guaranteed copy elision.

we could bring the move ctor back when we need to return a named guard.

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath24-testing-20261114.234450-main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants