mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-16 04:55:41 +00:00
PVF: Vote invalid on panics in execution thread (after a retry) (#7155)
* PVF: Remove `rayon` and some uses of `tokio` 1. We were using `rayon` to spawn a superfluous thread to do execution, so it was removed. 2. We were using `rayon` to set a threadpool-specific thread stack size, and AFAIK we couldn't do that with `tokio` (it's possible [per-runtime](https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.thread_stack_size) but not per-thread). Since we want to remove `tokio` from the workers [anyway](https://github.com/paritytech/polkadot/issues/7117), I changed it to spawn threads with the `std::thread` API instead of `tokio`.[^1] [^1]: NOTE: This PR does not totally remove the `tokio` dependency just yet. 3. Since `std::thread` API is not async, we could no longer `select!` on the threads as futures, so the `select!` was changed to a naive loop. 4. The order of thread selection was flipped to make (3) sound (see note in code). I left some TODO's related to panics which I'm going to address soon as part of https://github.com/paritytech/polkadot/issues/7045. * PVF: Vote invalid on panics in execution thread (after a retry) Also make sure we kill the worker process on panic errors and internal errors to potentially clear any error states independent of the candidate. * Address a couple of TODOs Addresses a couple of follow-up TODOs from https://github.com/paritytech/polkadot/pull/7153. * Add some documentation to implementer's guide * Fix compile error * Fix compile errors * Fix compile error * Update roadmap/implementers-guide/src/node/utility/candidate-validation.md Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com> * Address comments + couple other changes (see message) - Measure the CPU time in the prepare thread, so the observed time is not affected by any delays in joining on the thread. - Measure the full CPU time in the execute thread. * Implement proper thread synchronization Use condvars i.e. `Arc::new((Mutex::new(true), Condvar::new()))` as per the std docs. Considered also using a condvar to signal the CPU thread to end, in place of an mpsc channel. This was not done because `Condvar::wait_timeout_while` is documented as being imprecise, and `mpsc::Receiver::recv_timeout` is not documented as such. Also, we would need a separate condvar, to avoid this case: the worker thread finishes its job, notifies the condvar, the CPU thread returns first, and we join on it and not the worker thread. So it was simpler to leave this part as is. * Catch panics in threads so we always notify condvar * Use `WaitOutcome` enum instead of bool condition variable * Fix retry timeouts to depend on exec timeout kind * Address review comments * Make the API for condvars in workers nicer * Add a doc * Use condvar for memory stats thread * Small refactor * Enumerate internal validation errors in an enum * Fix comment * Add a log * Fix test * Update variant naming * Address a missed TODO --------- Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com>
This commit is contained in:
@@ -27,6 +27,7 @@ use cpu_time::ProcessTime;
|
||||
use parity_scale_codec::{Decode, Encode};
|
||||
use polkadot_node_core_pvf::{
|
||||
framed_recv, framed_send, ExecuteHandshake as Handshake, ExecuteResponse as Response,
|
||||
InternalValidationError,
|
||||
};
|
||||
use polkadot_parachain::primitives::ValidationResult;
|
||||
use std::{
|
||||
@@ -127,13 +128,9 @@ pub fn worker_entrypoint(socket_path: &str, node_version: Option<&str>) {
|
||||
let response = match outcome {
|
||||
WaitOutcome::Finished => {
|
||||
let _ = cpu_time_monitor_tx.send(());
|
||||
execute_thread.join().unwrap_or_else(|e| {
|
||||
// TODO: Use `Panic` error once that is implemented.
|
||||
Response::format_internal(
|
||||
"execute thread error",
|
||||
&stringify_panic_payload(e),
|
||||
)
|
||||
})
|
||||
execute_thread
|
||||
.join()
|
||||
.unwrap_or_else(|e| Response::Panic(stringify_panic_payload(e)))
|
||||
},
|
||||
// If the CPU thread is not selected, we signal it to end, the join handle is
|
||||
// dropped and the thread will finish in the background.
|
||||
@@ -150,16 +147,14 @@ pub fn worker_entrypoint(socket_path: &str, node_version: Option<&str>) {
|
||||
);
|
||||
Response::TimedOut
|
||||
},
|
||||
Ok(None) => Response::format_internal(
|
||||
"cpu time monitor thread error",
|
||||
"error communicating over closed channel".into(),
|
||||
),
|
||||
// We can use an internal error here because errors in this thread are
|
||||
// independent of the candidate.
|
||||
Err(e) => Response::format_internal(
|
||||
"cpu time monitor thread error",
|
||||
&stringify_panic_payload(e),
|
||||
),
|
||||
Ok(None) =>
|
||||
Response::InternalError(InternalValidationError::CpuTimeMonitorThread(
|
||||
"error communicating over finished channel".into(),
|
||||
)),
|
||||
Err(e) =>
|
||||
Response::InternalError(InternalValidationError::CpuTimeMonitorThread(
|
||||
stringify_panic_payload(e),
|
||||
)),
|
||||
}
|
||||
},
|
||||
WaitOutcome::Pending =>
|
||||
@@ -181,7 +176,7 @@ fn validate_using_artifact(
|
||||
// TODO: Re-evaluate after <https://github.com/paritytech/substrate/issues/13860>.
|
||||
let file_metadata = std::fs::metadata(artifact_path);
|
||||
if let Err(err) = file_metadata {
|
||||
return Response::format_internal("execute: could not find or open file", &err.to_string())
|
||||
return Response::InternalError(InternalValidationError::CouldNotOpenFile(err.to_string()))
|
||||
}
|
||||
|
||||
let descriptor_bytes = match unsafe {
|
||||
|
||||
@@ -205,7 +205,7 @@ pub fn worker_entrypoint(socket_path: &str, node_version: Option<&str>) {
|
||||
Ok(None) => Err(PrepareError::IoErr(
|
||||
"error communicating over closed channel".into(),
|
||||
)),
|
||||
// Errors in this thread are independent of the candidate.
|
||||
// Errors in this thread are independent of the PVF.
|
||||
Err(err) => Err(PrepareError::IoErr(stringify_panic_payload(err))),
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user