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

fixup(#7160 / tokio_epoll_uring_ext): double-panic caused by info! in thread-local's drop() #7164

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 8 additions & 21 deletions pageserver/src/virtual_file/io_engine/tokio_epoll_uring_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! on older kernels, such as some (but not all) older kernels in the Linux 5.10 series.
//! See <https://github.com/neondatabase/neon/issues/6373#issuecomment-1905814391> for more details.

use std::sync::atomic::AtomicU32;
use std::sync::{Arc, Weak};
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;

use tokio_util::sync::CancellationToken;
use tracing::{error, info, info_span, warn, Instrument};
Expand All @@ -24,38 +24,25 @@ struct ThreadLocalState(Arc<ThreadLocalStateInner>);
struct ThreadLocalStateInner {
cell: tokio::sync::OnceCell<SystemHandle>,
launch_attempts: AtomicU32,
weak_self: Weak<ThreadLocalStateInner>,
/// populated through fetch_add from [`THREAD_LOCAL_STATE_ID`]
thread_local_state_id: u64,
}

impl ThreadLocalState {
pub fn new() -> Self {
Self(Arc::new_cyclic(|weak| ThreadLocalStateInner {
Self(Arc::new(ThreadLocalStateInner {
cell: tokio::sync::OnceCell::default(),
launch_attempts: AtomicU32::new(0),
weak_self: Weak::clone(weak),
thread_local_state_id: THREAD_LOCAL_STATE_ID.fetch_add(1, Ordering::Relaxed),
}))
}
}

impl ThreadLocalStateInner {
pub fn make_id_string(&self) -> String {
format!("0x{:p}", self.weak_self.as_ptr())
}
}

impl Drop for ThreadLocalStateInner {
fn drop(&mut self) {
info!(parent: None, id=%self.make_id_string(), "tokio_epoll_uring_ext: thread-local state is being dropped and id might be re-used in the future");
format!("{}", self.0.thread_local_state_id)
}
}

impl std::ops::Deref for ThreadLocalState {
type Target = ThreadLocalStateInner;

fn deref(&self) -> &Self::Target {
&self.0
}
}
static THREAD_LOCAL_STATE_ID: AtomicU64 = AtomicU64::new(0);

thread_local! {
static THREAD_LOCAL: ThreadLocalState = ThreadLocalState::new();
Expand Down