1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use crate::{
    connection::Connection,
    errors::JupyterResult,
    jupyter_message::{JupyterMessage, JupyterMessageType},
    ExecutionRequest, JupyterConnection, JupyterKernelProtocol, JupyterKernelSockets,
};

use crate::{
    commands::start::KernelControl,
    jupyter_message::{CommonInfoRequest, KernelInfoReply},
};
use serde_json::Value;
use std::{sync::Arc, time::SystemTime};
use tokio::{sync::Mutex, task::JoinHandle};
use zeromq::{PubSocket, RepSocket, RouterSocket, Socket, SocketRecv, SocketSend, ZmqMessage};

// Note, to avoid potential deadlocks, each thread should lock at most one mutex at a time.
#[derive(Clone)]
#[allow(unused)]
pub(crate) struct SealedServer {
    heartbeat: Arc<Mutex<Connection<RepSocket>>>,
    iopub: Arc<Mutex<Connection<PubSocket>>>,
    stdin: Arc<Mutex<Connection<RouterSocket>>>,
    control: Arc<Mutex<Connection<RouterSocket>>>,
    shell_socket: Arc<Mutex<Connection<RouterSocket>>>,
    latest_execution_request: Arc<Mutex<Option<JupyterMessage>>>,
    // execution_request_receiver: Arc<Mutex<UnboundedReceiver<ExecutionResult>>>,
    shutdown_sender: Arc<Mutex<Option<crossbeam_channel::Sender<()>>>>,
    tokio_handle: tokio::runtime::Handle,
}

pub struct ExecuteProvider<T> {
    pub(crate) context: Arc<Mutex<T>>,
    pub(crate) sockets: JupyterKernelSockets,
}

impl<T> Clone for ExecuteProvider<T> {
    fn clone(&self) -> Self {
        Self { context: self.context.clone(), sockets: self.sockets.clone() }
    }
}

impl<T> ExecuteProvider<T> {
    pub fn new(context: T, sockets: JupyterKernelSockets) -> Self
    where
        T: JupyterKernelProtocol + 'static,
    {
        Self { context: Arc::new(Mutex::new(context)), sockets }
    }
}

struct ShutdownReceiver {
    // Note, this needs to be a crossbeam channel because
    // start_output_pass_through_thread selects on this and other crossbeam
    // channels.
    recv: crossbeam_channel::Receiver<()>,
}

impl SealedServer {
    pub(crate) fn run<T>(config: &KernelControl, server: T) -> JupyterResult<()>
    where
        T: JupyterKernelProtocol + 'static,
    {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            // We only technically need 1 thread. However we've observed that
            // when using vscode's jupyter extension, we can get requests on the
            // shell socket before we have any subscribers on iopub. The iopub
            // subscription then completes, but the execution_state="idle"
            // message(s) have already been sent to a channel that at the time
            // had no subscriptions. The vscode extension then waits
            // indefinitely for an execution_state="idle" message that will
            // never come. Having multiple threads at least reduces the chances
            // of this happening.
            .worker_threads(4)
            .enable_all()
            .build()
            .unwrap();
        let handle = runtime.handle().clone();
        runtime.block_on(async {
            let shutdown_receiver = Self::start(config, handle, server).await?;
            shutdown_receiver.wait_for_shutdown().await;
            let result: JupyterResult<()> = Ok(());
            result
        })?;
        Ok(())
    }

    async fn start<T>(
        config: &KernelControl,
        tokio_handle: tokio::runtime::Handle,
        mut server: T,
    ) -> JupyterResult<ShutdownReceiver>
    where
        T: JupyterKernelProtocol + 'static,
    {
        let heartbeat = bind_socket::<RepSocket>(config, config.hb_port).await?;
        let shell_socket = bind_socket::<RouterSocket>(config, config.shell_port).await?;
        let control_socket = bind_socket::<RouterSocket>(config, config.control_port).await?;
        let stdin_socket = bind_socket::<RouterSocket>(config, config.stdin_port).await?;
        let io_pub_socket = bind_socket::<PubSocket>(config, config.iopub_port).await?;
        let io_pub = Arc::new(Mutex::new(io_pub_socket));
        let (shutdown_sender, shutdown_receiver) = crossbeam_channel::unbounded();
        let latest_execution_request = Arc::new(Mutex::new(None));
        let sockets = JupyterKernelSockets {
            execute_count: Arc::new(Mutex::new(1)),
            io_channel: Some(io_pub.clone()),
            debugging: Arc::new(Mutex::new(false)),
        };
        let setup = JupyterConnection { boot_path: Default::default(), sockets: sockets.clone() };
        server.connected(setup);
        // server.bind_execution_socket(execution_result_sender).await;
        let here = SealedServer {
            iopub: io_pub,
            heartbeat: Arc::new(Mutex::new(heartbeat)),
            latest_execution_request,
            // execution_request_receiver: Arc::new(Mutex::new(execution_receiver)),
            stdin: Arc::new(Mutex::new(stdin_socket)),
            control: Arc::new(Mutex::new(control_socket)),
            shutdown_sender: Arc::new(Mutex::new(Some(shutdown_sender))),
            tokio_handle,
            shell_socket: Arc::new(Mutex::new(shell_socket)),
        };
        let context = ExecuteProvider::new(server, sockets);
        here.clone().spawn_heart_beat();
        here.clone().spawn_shell_execution(context.clone());
        // server.clone().spawn_execution_queue(context.clone());
        here.clone().spawn_control(context.clone());
        here.clone().spawn_std_in(context.clone());
        Ok(ShutdownReceiver { recv: shutdown_receiver })
    }

    async fn signal_shutdown(&self) {
        // let msg = ShutdownReply::new();
        self.shutdown_sender.lock().await.take();
    }

    fn spawn_heart_beat(self) -> JoinHandle<()> {
        tokio::spawn(async move {
            loop {
                if let Err(e) = self.clone().handle_heart_beat().await {
                    tracing::warn!("Error sending heartbeat: {:?}", e);
                }
            }
        })
    }

    async fn handle_heart_beat(self) -> JupyterResult<()> {
        let mut connection = match self.heartbeat.try_lock() {
            Ok(o) => o,
            Err(_) => return Ok(()),
        };
        let _ = connection.socket.recv().await?;
        connection.socket.send(ZmqMessage::from(b"ping".to_vec())).await?;
        Ok(())
    }
    fn spawn_shell_execution<T>(self, executor: ExecuteProvider<T>) -> JoinHandle<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        tokio::spawn(async move {
            tracing::info!("Shell Executor Spawned");
            loop {
                if let Err(e) = self.clone().handle_shell(executor.clone()).await {
                    tracing::error!("Error sending shell execution: {:?}", e);
                }
            }
        })
    }
    async fn handle_shell<'a, T>(self, executor: ExecuteProvider<T>) -> JupyterResult<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        // Processing of every message should be enclosed between "busy" and "idle"
        // see https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-shell-router-dealer-channel
        // Jupiter Lab doesn't use the kernel until it received "idle" for kernel_info_request
        let request = JupyterMessage::read(&mut &mut self.shell_socket.lock().await).await?;
        request.send_state(self.iopub.clone(), true).await?;
        match request.kind() {
            JupyterMessageType::KernelInfoRequest => {
                let info = executor.context.lock().await.language_info();
                let cont = KernelInfoReply::build(info);
                request.as_reply().with_content(cont)?.send_by(&mut &mut self.shell_socket.lock().await).await?
            }
            JupyterMessageType::ExecuteRequest => {
                let time = SystemTime::now();
                // *self.latest_execution_request.lock().await = Some(request);
                let mut task = request.recast::<ExecutionRequest>()?;
                task.header = request.clone();
                // reply busy event
                let mut runner = executor.context.lock().await;
                let count = executor.sockets.get_counter();
                let reply = runner.running(task.clone()).await.with_count(count);
                // Check elapsed time
                match time.elapsed() {
                    Ok(o) => {
                        let escape = runner.running_time(o.as_secs_f64());
                        if !escape.is_empty() {
                            let time = task.as_result("text/html".to_string(), Value::String(escape));
                            request
                                .as_reply()
                                .with_message_type(JupyterMessageType::ExecuteResult)
                                .with_content(time)?
                                .send_by(&mut &mut self.iopub.lock().await)
                                .await?;
                        }
                    }
                    Err(_) => {}
                }
                // reply finish event
                request.as_reply().with_content(reply)?.send_by(&mut &mut self.shell_socket.lock().await).await?;
            }
            JupyterMessageType::CommonInfoRequest => {
                let task = request.recast::<CommonInfoRequest>()?;
                request.as_reply().with_content(task.as_reply())?.send_by(&mut &mut self.shell_socket.lock().await).await?;
            }
            JupyterMessageType::Custom(v) => {
                tracing::error!("Got unknown shell message: {:?}", v);
            }
            _ => {
                tracing::warn!("Got custom shell message: {:?}", request);
            }
        }
        request.send_state(self.iopub, false).await?;
        Ok(())
    }
    #[allow(dead_code)]
    fn spawn_execution_queue<T>(self, executor: ExecuteProvider<T>) -> JoinHandle<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        let mut running_count = 0;
        tokio::spawn(async move {
            tracing::trace!("Queue Executor Spawned");
            loop {
                if let Err(e) = self.clone().handle_execution_queue(executor.clone(), running_count).await {
                    eprintln!("Error sending execution queue: {:?}", e);
                }
                running_count += 1;
            }
        })
    }
    #[allow(dead_code)]
    async fn handle_execution_queue<T>(self, _executor: ExecuteProvider<T>, _count: i32) -> JupyterResult<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        // let io = self.iopub.try_lock()?;
        // let exec = self.execution_request_receiver.try_lock()?;
        todo!()
    }

    fn spawn_control<T>(self, executor: ExecuteProvider<T>) -> JoinHandle<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        tokio::spawn(async move {
            tracing::info!("Control Executor Spawned");
            loop {
                if let Err(e) = self.clone().handle_control(executor.clone()).await {
                    tracing::error!("Error sending control execution: {:?}", e);
                }
            }
        })
    }
    async fn handle_control<'a, T>(self, executor: ExecuteProvider<T>) -> JupyterResult<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        let control = &mut self.control.lock().await;
        let request = JupyterMessage::read(control).await?;
        // FUCK_ME: Time wasted here = two weeks
        // Who the hell designed this process and it's not mentioned in the docs!!!!!!!
        request.send_state(self.iopub.clone(), true).await?;
        // main reply
        match request.kind() {
            JupyterMessageType::KernelInfoRequest => {
                let info = executor.context.lock().await;
                let cont = KernelInfoReply::build(info.language_info());
                request.as_reply().with_content(cont)?.send_by(control).await?
            }

            JupyterMessageType::DebugRequest => {
                let result = request.debug_response(executor).await?;
                request.as_reply().with_content(result)?.send_by(control).await?;
            }
            JupyterMessageType::InterruptRequest => {
                let runner = executor.context.lock().await;
                match runner.interrupt_kernel() {
                    Some(_) => {
                        request
                            .as_reply()
                            .create_message(JupyterMessageType::StatusReply)
                            .with_content("ok")?
                            .send_by(control)
                            .await?
                    }
                    None => {
                        request
                            .as_reply()
                            .create_message(JupyterMessageType::StatusReply)
                            .with_content("ok")?
                            .send_by(control)
                            .await?
                    }
                }
            }
            JupyterMessageType::ShutdownRequest => self.signal_shutdown().await,
            JupyterMessageType::Custom(v) => {
                tracing::error!("Got unknown control message: {:#?}", v);
            }
            _ => {
                tracing::warn!("Got custom control message: {:#?}", request);
            }
        }
        request.send_state(self.iopub.clone(), false).await?;
        Ok(())
    }
    fn spawn_std_in<T>(self, executor: ExecuteProvider<T>) -> JoinHandle<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        tokio::spawn(async move {
            tracing::info!("IO Executor Spawned");
            loop {
                if let Err(e) = self.clone().handle_std_in(executor.clone()).await {
                    tracing::error!("Error sending io execution: {:?}", e);
                }
            }
        })
    }
    async fn handle_std_in<'a, T>(self, _: ExecuteProvider<T>) -> JupyterResult<()>
    where
        T: JupyterKernelProtocol + Send + 'static,
    {
        let io = &mut self.stdin.lock().await;
        let request = JupyterMessage::read(io).await?;
        match request.kind() {
            JupyterMessageType::Custom(v) => {
                tracing::error!("Got unknown io message: {:?}", v);
            }
            _ => {
                tracing::warn!("Got custom io message: {:?}", request);
            }
        }
        Ok(())
    }
}

impl ShutdownReceiver {
    async fn wait_for_shutdown(self) {
        let _ = tokio::task::spawn_blocking(move || self.recv.recv()).await;
    }
}

async fn bind_socket<S: Socket>(config: &KernelControl, port: u16) -> JupyterResult<Connection<S>> {
    let endpoint = format!("{}://{}:{}", config.transport, config.ip, port);
    let mut socket = S::new();
    socket.bind(&endpoint).await?;
    Connection::new(socket, &config.key)
}