Skip to content

Commit 5d4d167

Browse files
committed
Move task locals inside slot supplier
1 parent 2379745 commit 5d4d167

File tree

3 files changed

+166
-171
lines changed

3 files changed

+166
-171
lines changed

temporalio/bridge/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ anyhow = "1.0"
1212
async-trait = "0.1"
1313
futures = "0.3"
1414
log = "0.4"
15-
once_cell = "1.16"
1615
prost = "0.13"
1716
pyo3 = { version = "0.20", features = ["extension-module", "abi3-py38", "anyhow"] }
1817
pyo3-asyncio = { version = "0.20", features = ["tokio-runtime"] }

temporalio/bridge/src/runtime.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,12 @@ pub fn init_runtime(telemetry_config: TelemetryConfig) -> PyResult<RuntimeRef> {
118118
}
119119
}
120120

121-
let task_locals = Python::with_gil(|py| {
122-
// Event loop is assumed to be running at this point
123-
let locals = pyo3_asyncio::TaskLocals::with_running_loop(py)?.copy_context(py)?;
124-
PyResult::Ok(locals)
125-
})?;
126-
127121
// Create core runtime which starts tokio multi-thread runtime
128122
let mut core = CoreRuntime::new(
129123
telemetry_build
130124
.build()
131125
.map_err(|err| PyValueError::new_err(format!("Invalid telemetry config: {}", err)))?,
132-
TokioRuntimeBuilder {
133-
inner: tokio::runtime::Builder::new_multi_thread(),
134-
lang_on_thread_start: Some(move || {
135-
// Set task locals for each thread
136-
Python::with_gil(|_| {
137-
THREAD_TASK_LOCAL.with(|r| {
138-
std::cell::OnceCell::set(r, task_locals.clone())
139-
.expect("TaskLocals are only set once");
140-
});
141-
PyResult::Ok(())
142-
})
143-
.expect("Setting per-thread python TaskLocals must work");
144-
}),
145-
},
126+
TokioRuntimeBuilder::default(),
146127
)
147128
.map_err(|err| PyRuntimeError::new_err(format!("Failed initializing telemetry: {}", err)))?;
148129

@@ -386,12 +367,7 @@ impl TryFrom<MetricsConfig> for Arc<dyn CoreMeter> {
386367
pub(crate) struct TokioRuntime;
387368

388369
tokio::task_local! {
389-
static TASK_LOCALS: once_cell::unsync::OnceCell<pyo3_asyncio::TaskLocals>;
390-
}
391-
392-
thread_local! {
393-
pub(crate) static THREAD_TASK_LOCAL: std::cell::OnceCell<pyo3_asyncio::TaskLocals> =
394-
const { std::cell::OnceCell::new() };
370+
static TASK_LOCALS: std::cell::OnceCell<pyo3_asyncio::TaskLocals>;
395371
}
396372

397373
impl pyo3_asyncio::generic::Runtime for TokioRuntime {
@@ -414,7 +390,7 @@ impl pyo3_asyncio::generic::ContextExt for TokioRuntime {
414390
where
415391
F: Future<Output = R> + Send + 'static,
416392
{
417-
let cell = once_cell::unsync::OnceCell::new();
393+
let cell = std::cell::OnceCell::new();
418394
cell.set(locals).unwrap();
419395

420396
Box::pin(TASK_LOCALS.scope(cell, fut))

0 commit comments

Comments
 (0)