Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't leak the function that is called on drop #111875

Merged
merged 1 commit into from
May 25, 2023

Conversation

WaffleLapkin
Copy link
Member

It probably wasn't causing problems anyway, but still, a // this leaks, please don't pass anything that owns memory is not sustainable.

I could implement a version which does not require Option, but it would require unsafe, at which point it's probably not worth it.

@rustbot
Copy link
Collaborator

rustbot commented May 23, 2023

r? @TaKO8Ki

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 23, 2023
@WaffleLapkin
Copy link
Member Author

r? @Nilstrieb

@rustbot rustbot assigned Noratrieb and unassigned TaKO8Ki May 23, 2023
Copy link
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

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

not because it's a good idea but out of curiosity, what unsafe approach did you imagine?

@@ -102,21 +102,27 @@ pub mod unord;
pub use ena::undo_log;
pub use ena::unify;

pub struct OnDrop<F: Fn()>(pub F);
/// Returns a structure that calls `f` when dropped.
pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
Copy link
Member

Choose a reason for hiding this comment

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

+1 on the rename

@Noratrieb
Copy link
Member

this touches ty::tls and while I'm fairly certain that it won't affect perf let's just make sure it doesn't
r=me afterwards
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 24, 2023
@bors
Copy link
Contributor

bors commented May 24, 2023

⌛ Trying commit e2b9530 with merge 0a7230578c08af7d4c955133f51b1eb698f1a6b2...

@WaffleLapkin
Copy link
Member Author

not because it's a good idea but out of curiosity, what unsafe approach did you imagine?

Basically using ManuallyDrop instead of option. drop then uses take() and disable uses another ManuallyDrop to prevent Drop from running, so that the function can be just dropped:

pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
    OnDrop(ManuallyDrop::new(f))
}

pub struct OnDrop<F: FnOnce()>(ManuallyDrop<F>);

impl<F: FnOnce()> OnDrop<F> {
    pub fn disable(self) {
        // Prevent `self`'s drop from running and taking/calling `self.0`
        let mut this = ManuallyDrop::new(self);
    
        // Safety:
        // `self.0` is only taken on drop and we prevented it from running.
        unsafe { ManuallyDrop::drop(&mut this.0) };
    }
}

impl<F: FnOnce()> Drop for OnDrop<F> {
    fn drop(&mut self) {
        // Safety:
        // This is the only place that takes `self.0` (except `disable` which is
        // mutually exclusive with this function);
        let f = unsafe { ManuallyDrop::take(&mut self.0) };
        f();
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=22588656290ac714396168de17c4622f

@WaffleLapkin WaffleLapkin removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 24, 2023
@Noratrieb
Copy link
Member

ah, since disable takes self, now I see. yeah, that would be simple and not even that hard to get right, but it seems a little unnecessary. if perf is sad r=me on the unsafe impl too

@bors
Copy link
Contributor

bors commented May 24, 2023

☀️ Try build successful - checks-actions
Build commit: 0a7230578c08af7d4c955133f51b1eb698f1a6b2 (0a7230578c08af7d4c955133f51b1eb698f1a6b2)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0a7230578c08af7d4c955133f51b1eb698f1a6b2): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.2%, 0.3%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.9% [0.9%, 0.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.5% [-1.5%, -1.5%] 1
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.4%, -2.0%] 2
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 645.989s -> 647.159s (0.18%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 24, 2023
@Noratrieb
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented May 25, 2023

📌 Commit e2b9530 has been approved by Nilstrieb

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label May 25, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request May 25, 2023
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#95198 (Add slice::{split_,}{first,last}_chunk{,_mut})
 - rust-lang#109899 (Use apple-m1 as target CPU for aarch64-apple-darwin.)
 - rust-lang#111624 (Emit diagnostic for privately uninhabited uncovered witnesses.)
 - rust-lang#111875 (Don't leak the function that is called on drop)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a9743e1 into rust-lang:master May 25, 2023
@rustbot rustbot added this to the 1.71.0 milestone May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants