Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 51 additions & 38 deletions src/execution/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod websocket;
pub mod ydoc;
pub mod ydoc_notebook_ops;

use crate::execution::types::{ExecutionConfig, ExecutionError, ExecutionResult};
use crate::execution::types::{ExecutionConfig, ExecutionResult};
use crate::execution::ExecutionBackend;
use anyhow::{Context, Result};
use client::{JupyterClient, SessionInfo};
Expand Down Expand Up @@ -227,25 +227,7 @@ impl ExecutionBackend for RemoteExecutor {
}

if idle_received {
let has_error = outputs
.iter()
.any(|o| matches!(o, nbformat::v4::Output::Error(_)));
let error_info = outputs.iter().find_map(|o| {
if let nbformat::v4::Output::Error(err) = o {
Some(ExecutionError {
ename: err.ename.clone(),
evalue: err.evalue.clone(),
traceback: err.traceback.clone(),
})
} else {
None
}
});
return if has_error {
Ok(ExecutionResult::error(outputs, ec, error_info.unwrap()))
} else {
Ok(ExecutionResult::success(outputs, ec))
};
return Ok(ExecutionResult::from_outputs(outputs, ec));
}
}

Expand All @@ -259,37 +241,68 @@ impl ExecutionBackend for RemoteExecutor {
} else {
tokio::select! {
kernel_msg = ws.recv_message() => {
if let Some(msg) = kernel_msg? {
let is_ours = msg.parent_header.as_ref()
.map(|h| h.msg_id == msg_id).unwrap_or(false);
if is_ours {
match &msg.content {
JupyterMessageContent::ExecuteInput(input) => {
expected_ec = Some(input.execution_count.0 as i64);
}
JupyterMessageContent::Status(status) => {
if matches!(status.execution_state,
jupyter_protocol::ExecutionState::Idle) {
idle_received = true;
match kernel_msg? {
Some(msg) => {
let is_ours = msg.parent_header.as_ref()
.map(|h| h.msg_id == msg_id).unwrap_or(false);
if is_ours {
match &msg.content {
JupyterMessageContent::ExecuteInput(input) => {
expected_ec = Some(input.execution_count.0 as i64);
}
JupyterMessageContent::Status(status) => {
if matches!(status.execution_state,
jupyter_protocol::ExecutionState::Idle) {
idle_received = true;
}
}
_ => {}
}
_ => {}
}
}
None => break,
}
}
ydoc_result = ydoc.recv_update() => {
ydoc_result.context("Y.js update error")?;
}
_ = tokio::time::sleep_until(deadline) => {
break;
}
}
}
}

let ec = ydoc
.read_cell_outputs(cell_idx)
.ok()
.and_then(|c| c.execution_count);
Ok(ExecutionResult::success(outputs, ec))
// Fallback: loop exited via timeout or WS close.
// Collect any outputs we haven't seen yet.
let ec = if let Ok(cell_data) = ydoc.read_cell_outputs(cell_idx) {
for (idx, url_path) in &cell_data.externalized_urls {
if fetched_urls.insert(url_path.clone()) {
seen_indices.insert(*idx);
if let Some(output) =
Self::fetch_output(&http, &self.server_url, &self.token, url_path).await
{
if let Some(cb) = &on_output {
cb(&output);
}
outputs.push(output);
}
}
}
for (idx, output) in &cell_data.inline_outputs {
if seen_indices.insert(*idx) {
if let Some(cb) = &on_output {
cb(output);
}
outputs.push(output.clone());
}
}
cell_data.execution_count
} else {
None
};

Ok(ExecutionResult::from_outputs(outputs, ec))
}

async fn stop(&mut self) -> Result<()> {
Expand Down
19 changes: 19 additions & 0 deletions src/execution/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ impl ExecutionResult {
error: Some(error),
}
}

/// Build a result by inspecting outputs for errors in a single pass.
pub fn from_outputs(outputs: Vec<nbformat::v4::Output>, execution_count: Option<i64>) -> Self {
let error_info = outputs.iter().find_map(|o| {
if let nbformat::v4::Output::Error(err) = o {
Some(ExecutionError {
ename: err.ename.clone(),
evalue: err.evalue.clone(),
traceback: err.traceback.clone(),
})
} else {
None
}
});
match error_info {
Some(err) => Self::error(outputs, execution_count, err),
None => Self::success(outputs, execution_count),
}
}
}

/// Output from a single execution message
Expand Down
Loading