From 643957472169685c497b761ae914c44778a863e3 Mon Sep 17 00:00:00 2001 From: meship-starkware Date: Thu, 7 Nov 2024 16:25:55 +0200 Subject: [PATCH] chore: check if a thread is still alive after the block is dead --- .../src/py_block_executor.rs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/crates/native_blockifier/src/py_block_executor.rs b/crates/native_blockifier/src/py_block_executor.rs index 7bf28040a1..9b2ae0d58f 100644 --- a/crates/native_blockifier/src/py_block_executor.rs +++ b/crates/native_blockifier/src/py_block_executor.rs @@ -33,6 +33,8 @@ use crate::storage::{PapyrusStorage, Storage, StorageConfig}; pub(crate) type RawTransactionExecutionResult = Vec; pub(crate) type PyVisitedSegmentsMapping = Vec<(PyFelt, Vec)>; +use std::sync::{Arc, Mutex}; +use std::thread::{self, JoinHandle}; #[cfg(test)] #[path = "py_block_executor_test.rs"] mod py_block_executor_test; @@ -124,6 +126,7 @@ pub struct PyBlockExecutor { /// `Send` trait is required for `pyclass` compatibility as Python objects must be threadsafe. pub storage: Box, pub global_contract_cache: GlobalContractCache, + thread_handle: Arc>>>, } #[pymethods] @@ -144,6 +147,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."), @@ -155,6 +165,7 @@ impl PyBlockExecutor { tx_executor: None, storage: Box::new(storage), global_contract_cache: GlobalContractCache::new(global_contract_cache_size), + thread_handle, } } @@ -245,6 +256,22 @@ impl PyBlockExecutor { }) .collect(); + let is_thread_alive = { + let handle = self.thread_handle.lock().unwrap(); + if let Some(handle) = handle.as_ref() { + handle.thread().unpark(); + true + } else { + 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| { @@ -376,6 +403,12 @@ impl PyBlockExecutor { // contracts. let mut versioned_constants = VersionedConstants::latest_constants().clone(); versioned_constants.disable_cairo0_redeclaration = false; + 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: BouncerConfig { block_max_capacity: BouncerWeights { @@ -386,11 +419,15 @@ impl PyBlockExecutor { tx_executor_config: TransactionExecutorConfig { concurrency_config: concurrency_config.into(), }, + + + storage: Box::new(PapyrusStorage::new_for_testing(path, &os_config.chain_id)), chain_info: os_config.into_chain_info(), versioned_constants, tx_executor: None, global_contract_cache: GlobalContractCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST), + thread_handle, } } } @@ -410,9 +447,17 @@ impl PyBlockExecutor { ) } + + #[cfg(any(feature = "testing", test))] pub fn create_for_testing_with_storage(storage: impl Storage + Send + 'static) -> Self { use blockifier::state::global_cache::GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST; + 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: BouncerConfig::max(), tx_executor_config: TransactionExecutorConfig::create_for_testing(true), @@ -421,6 +466,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, } }