Commit ea38f9c0ca
Unsigned
Layout: unified · split
src/server.rs +74 −4
| @@ -103,6 +103,32 @@ use crate::{ | ||
| 103 | 103 | ConfigHandle, Servers, PLUGIN_NAME, |
| 104 | 104 | }; |
| 105 | 105 | |
| 106 | fn with_entered_runtime_until_final_drop<F>( | |
| 107 | runtime: Rc<tokio::runtime::Runtime>, | |
| 108 | f: F, | |
| 109 | ) where | |
| 110 | F: FnOnce(), | |
| 111 | { | |
| 112 | with_entered_runtime_until_drop(runtime, f, drop); | |
| 113 | } | |
| 114 | ||
| 115 | fn with_entered_runtime_until_drop<F, D>( | |
| 116 | runtime: Rc<tokio::runtime::Runtime>, | |
| 117 | f: F, | |
| 118 | drop_runtime: D, | |
| 119 | ) where | |
| 120 | F: FnOnce(), | |
| 121 | D: FnOnce(Rc<tokio::runtime::Runtime>), | |
| 122 | { | |
| 123 | let handle = runtime.handle().clone(); | |
| 124 | let guard = handle.enter(); | |
| 125 | ||
| 126 | f(); | |
| 127 | ||
| 128 | drop_runtime(runtime); | |
| 129 | drop(guard); | |
| 130 | } | |
| 131 | ||
| 106 | 132 | #[derive(Debug)] |
| 107 | 133 | pub enum ServerError { |
| 108 | 134 | StartError(String), |
| @@ -1649,9 +1675,10 @@ impl InnerServer { | ||
| 1649 | 1675 | }); |
| 1650 | 1676 | |
| 1651 | 1677 | if let Some(runtime) = runtime { |
| 1652 | let _guard = runtime.enter(); | |
| 1653 | self.shutdown_sdk_state(); | |
| 1654 | drop(connection); | |
| 1678 | with_entered_runtime_until_final_drop(runtime, || { | |
| 1679 | self.shutdown_sdk_state(); | |
| 1680 | drop(connection); | |
| 1681 | }); | |
| 1655 | 1682 | } else { |
| 1656 | 1683 | let runtime = self.servers.runtime().to_owned(); |
| 1657 | 1684 | let _guard = runtime.enter(); |
| @@ -1711,8 +1738,32 @@ impl InnerServer { | ||
| 1711 | 1738 | |
| 1712 | 1739 | #[cfg(test)] |
| 1713 | 1740 | mod tests { |
| 1714 | use super::InnerServer; | |
| 1741 | use super::{with_entered_runtime_until_drop, InnerServer}; | |
| 1715 | 1742 | use std::path::{Path, PathBuf}; |
| 1743 | use std::rc::Rc; | |
| 1744 | use std::sync::{ | |
| 1745 | atomic::{AtomicU8, Ordering}, | |
| 1746 | Arc, | |
| 1747 | }; | |
| 1748 | use tokio::runtime::{Handle, Runtime}; | |
| 1749 | ||
| 1750 | const NOT_DROPPED: u8 = 0; | |
| 1751 | const DROPPED_OUTSIDE_RUNTIME: u8 = 1; | |
| 1752 | const DROPPED_INSIDE_RUNTIME: u8 = 2; | |
| 1753 | ||
| 1754 | struct DropProbe(Arc<AtomicU8>); | |
| 1755 | ||
| 1756 | impl Drop for DropProbe { | |
| 1757 | fn drop(&mut self) { | |
| 1758 | let context = if Handle::try_current().is_ok() { | |
| 1759 | DROPPED_INSIDE_RUNTIME | |
| 1760 | } else { | |
| 1761 | DROPPED_OUTSIDE_RUNTIME | |
| 1762 | }; | |
| 1763 | ||
| 1764 | self.0.store(context, Ordering::SeqCst); | |
| 1765 | } | |
| 1766 | } | |
| 1716 | 1767 | |
| 1717 | 1768 | #[test] |
| 1718 | 1769 | fn keeps_absolute_download_path_for_display() { |
| @@ -1737,4 +1788,23 @@ mod tests { | ||
| 1737 | 1788 | |
| 1738 | 1789 | assert_eq!(InnerServer::display_download_path(&path), path); |
| 1739 | 1790 | } |
| 1791 | ||
| 1792 | #[test] | |
| 1793 | fn final_runtime_drop_happens_while_handle_is_entered() { | |
| 1794 | let runtime = Rc::new(Runtime::new().expect("runtime")); | |
| 1795 | let drop_context = Arc::new(AtomicU8::new(NOT_DROPPED)); | |
| 1796 | let final_drop_context = Arc::clone(&drop_context); | |
| 1797 | ||
| 1798 | with_entered_runtime_until_drop( | |
| 1799 | runtime, | |
| 1800 | || {}, | |
| 1801 | |runtime| { | |
| 1802 | let probe = DropProbe(final_drop_context); | |
| 1803 | drop(runtime); | |
| 1804 | drop(probe); | |
| 1805 | }, | |
| 1806 | ); | |
| 1807 | ||
| 1808 | assert_eq!(drop_context.load(Ordering::SeqCst), DROPPED_INSIDE_RUNTIME); | |
| 1809 | } | |
| 1740 | 1810 | } |