-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: do not leak the nested runtime (#8968)
Signed-off-by: Bugen Zhao <[email protected]> Signed-off-by: Runji Wang <[email protected]> Co-authored-by: Runji Wang <[email protected]>
- Loading branch information
1 parent
e685bdb
commit 36dd239
Showing
10 changed files
with
94 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 RisingWave Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::mem::ManuallyDrop; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use tokio::runtime::Runtime; | ||
|
||
/// A wrapper around [`Runtime`] that shuts down the runtime in the background when dropped. | ||
/// | ||
/// This is necessary because directly dropping a nested runtime is not allowed in a parent runtime. | ||
pub struct BackgroundShutdownRuntime(ManuallyDrop<Runtime>); | ||
|
||
impl Drop for BackgroundShutdownRuntime { | ||
fn drop(&mut self) { | ||
// Safety: The runtime is only dropped once here. | ||
let runtime = unsafe { ManuallyDrop::take(&mut self.0) }; | ||
|
||
#[cfg(madsim)] | ||
drop(runtime); | ||
#[cfg(not(madsim))] | ||
runtime.shutdown_background(); | ||
} | ||
} | ||
|
||
impl Deref for BackgroundShutdownRuntime { | ||
type Target = Runtime; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
impl DerefMut for BackgroundShutdownRuntime { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl From<Runtime> for BackgroundShutdownRuntime { | ||
fn from(runtime: Runtime) -> Self { | ||
Self(ManuallyDrop::new(runtime)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters