Skip to content

Commit

Permalink
chore: check if a thread is still alive after the block is dead.
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Nov 7, 2024
1 parent 357b5ec commit b043b17
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ use crate::storage::{PapyrusStorage, Storage, StorageConfig};
pub(crate) type RawTransactionExecutionResult = Vec<u8>;
pub(crate) type PyVisitedSegmentsMapping = Vec<(PyFelt, Vec<usize>)>;


use std::thread::{self, JoinHandle};
use std::sync::{Arc, Mutex};
#[cfg(test)]
#[path = "py_block_executor_test.rs"]
mod py_block_executor_test;
Expand Down Expand Up @@ -114,6 +117,10 @@ impl ThinTransactionExecutionInfo {
}
}


#[pyclass]


#[pyclass]
pub struct PyBlockExecutor {
pub bouncer_config: BouncerConfig,
Expand All @@ -124,6 +131,7 @@ pub struct PyBlockExecutor {
/// `Send` trait is required for `pyclass` compatibility as Python objects must be threadsafe.
pub storage: Box<dyn Storage + Send>,
pub global_contract_cache: GlobalContractCache,
thread_handle: Option<Arc<Mutex<Option<JoinHandle<()>>>>>,
}

#[pymethods]
Expand All @@ -144,6 +152,13 @@ impl PyBlockExecutor {
let versioned_constants =
VersionedConstants::get_versioned_constants(py_versioned_constants_overrides.into());
log::debug!("Initialized Block Executor.");
// Spawn a thread and store its handle
let thread_handle = Arc::new(Mutex::new(Some(thread::spawn(|| {
loop {
// Thread work here...
thread::sleep(std::time::Duration::from_secs(1));
}
}))));

Self {
bouncer_config: bouncer_config.try_into().expect("Failed to parse bouncer config."),
Expand All @@ -155,6 +170,7 @@ impl PyBlockExecutor {
tx_executor: None,
storage: Box::new(storage),
global_contract_cache: GlobalContractCache::new(global_contract_cache_size),
thread_handle: Some(thread_handle),
}
}

Expand Down Expand Up @@ -245,6 +261,17 @@ impl PyBlockExecutor {
})
.collect();

let is_thread_alive = {
let handle = self.thread_handle.lock().unwrap();
handle.as_ref().map(|h| h.is_running()).unwrap_or(false)
};

if is_thread_alive {
println!("Thread is still alive");
} else {
println!("Thread is not alive");
}

// Convert to Py types and allocate it on Python's heap, to be visible for Python's
// garbage collector.
Python::with_gil(|py| {
Expand Down Expand Up @@ -391,6 +418,7 @@ impl PyBlockExecutor {
versioned_constants,
tx_executor: None,
global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
thread_handle: None,
}
}
}
Expand Down Expand Up @@ -421,6 +449,7 @@ impl PyBlockExecutor {
versioned_constants: VersionedConstants::latest_constants().clone(),
tx_executor: None,
global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
thread_handle: None,
}
}

Expand Down

0 comments on commit b043b17

Please sign in to comment.