sandbox.py 11.8 KB
Newer Older
dengjb's avatar
dengjb committed
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import argparse
import asyncio
import json
import logging
import os
import signal
import sys
from asyncio import Queue
from datetime import datetime, timezone
from typing import Annotated, List, Union

import tornado.escape
import tornado.ioloop
import tornado.web
from annotated_types import Gt
from jupyter_client.asynchronous.client import AsyncKernelClient
from jupyter_client.manager import AsyncKernelManager
from pydantic import BaseModel

# Shell Jupyter message types
JupyterMessageTypeExecuteRequest = "execute_request"
JupyterMessageTypeExecuteReply = "execute_reply"

# IOPub Jupyter message types
JupyterMessageTypeStream = "stream"
JupyterMessageTypeDisplayData = "display_data"
JupyterMessageTypeExecuteResult = "execute_result"
JupyterMessageTypeError = "error"
JupyterMessageTypeStatus = "status"

# Supported Jupyter message types (IOPub only)
JupyterSupportedMessageTypes = [
    JupyterMessageTypeStream,
    JupyterMessageTypeDisplayData,
    JupyterMessageTypeExecuteResult,
    JupyterMessageTypeError,
    JupyterMessageTypeStatus,
]

# Kernel execution states
JupyterExecutionStateBusy = "busy"
JupyterExecutionStateIdle = "idle"
JupyterExecutionStateStarting = "starting"

# Saturn execution event types
ExecutionEventTypeStream = "stream"
ExecutionEventTypeDisplayData = "display_data"
ExecutionEventTypeError = "error"

# Saturn execution statuses
ExecutionStatusOK = "ok"
ExecutionStatusTimeout = "timeout"


class ExecutionEventStream(BaseModel):
    stream: str
    text: str


class ExecutionEventDisplayData(BaseModel):
    variants: dict


class ExecutionEventError(BaseModel):
    ename: str
    evalue: str
    traceback: list[str]


class ExecutionEvent(BaseModel):
    type: str
    timestamp: str  # RFC3339
    data: Union[
        ExecutionEventStream,
        ExecutionEventDisplayData,
        ExecutionEventError,
    ]


class ExecuteRequest(BaseModel):
    code: str
    timeout_secs: Annotated[int, Gt(0)]


class ExecuteResponse(BaseModel):
    status: str
    events: List[ExecutionEvent]


class PingResponse(BaseModel):
    last_activity: str  # RFC3339


class Error(BaseModel):
    error: str


def datetime_to_rfc3339(dt: datetime) -> str:
    """Convert a datetime to an RFC3339 formatted string."""
    return dt.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")


def rfc3339_to_datetime(date_string: str) -> datetime:
    """Convert an RFC3339 formatted string to a datetime."""
    return datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%fZ").replace(
        tzinfo=timezone.utc
    )


logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")


async def async_create_kernel(kernel_name: str):
    logging.info(f"Starting kernel for spec '{kernel_name}'")

    km = AsyncKernelManager(kernel_name=kernel_name)
    await km.start_kernel()

    client: AsyncKernelClient = km.client()
    client.start_channels()

    await client.wait_for_ready()

    logging.info("Kernel started")

    return km, client


msg_id_to_queue: dict[str, Queue] = {}


async def async_msg_producer(km: AsyncKernelManager, kc: AsyncKernelClient):
    try:
        while True:
            logging.info("Waiting for message...")

            msg = await kc.get_iopub_msg()

            log_jupyter_kernel_message(msg)

            parent_msg_id = msg["parent_header"].get("msg_id")
            if parent_msg_id in msg_id_to_queue:
                await msg_id_to_queue[parent_msg_id].put(msg)

    except Exception as e:
        logging.error(f"Error in message producer: {e}")
        await async_shutdown(km)


async def async_shutdown(km: AsyncKernelManager):
    logging.info("Shutting down kernel...")
    await km.shutdown_kernel()
    logging.info("Kernel shut down")
    sys.exit(0)


class State:
    def __init__(self, kernel_client: AsyncKernelClient):
        self.last_activity = datetime.now()
        self.kernel_client = kernel_client

    def reset_last_activity(self):
        self.last_activity = datetime.now()


class MainHandler(tornado.web.RequestHandler):
    def initialize(self, state: State):
        self.state = state

    async def get(self):
        try:
            is_alive = await client.is_alive()
            if not is_alive:
                raise Exception("kernel is not alive")
            self.write(
                PingResponse(
                    last_activity=datetime_to_rfc3339(self.state.last_activity)
                ).model_dump_json()
            )
        except Exception as e:
            self.set_status(500)
            self.write(Error(error=str(e)).model_dump_json())
            return


def serializer(o):
    if isinstance(o, datetime):
        return o.isoformat()
    raise TypeError("Type not serializable")


def log_jupyter_kernel_message(msg):
    m = json.dumps(msg, default=serializer)
    logging.info(f"Jupyter: {m}")


class ExecuteHandler(tornado.web.RequestHandler):
    def initialize(self, state: State):
        self.state = state

    async def post(self):
        parent_msg_id = None
        res: ExecuteResponse = ExecuteResponse(status=ExecutionStatusOK, events=[])

        try:
            logging.info(f"Execute request: {self.request.body}")
            self.state.reset_last_activity()

            req = ExecuteRequest.model_validate_json(self.request.body)

            local_queue = Queue()
            parent_msg_id = self.state.kernel_client.execute(req.code)
            msg_id_to_queue[parent_msg_id] = local_queue

            # Use the timeout logic on message processing
            try:
                await asyncio.wait_for(
                    self.process_messages(parent_msg_id, local_queue, res),
                    timeout=req.timeout_secs,
                )
            except asyncio.TimeoutError:
                logging.info(f"Timeout after {req.timeout_secs}s")
                res.status = ExecutionStatusTimeout
                return self.write(res.model_dump_json())

            self.state.reset_last_activity()
            self.write(res.model_dump_json())

        except Exception as e:
            self.set_status(500)
            self.write(Error(error=str(e)).model_dump_json())

        finally:
            # Cleanup after processing all messages
            if parent_msg_id is not None and parent_msg_id in msg_id_to_queue:
                del msg_id_to_queue[parent_msg_id]

            logging.info(f"Execute response: {res.model_dump_json()}")

    async def process_messages(self, parent_msg_id, queue, res):
        while True:
            msg = await queue.get()

            if msg["msg_type"] not in JupyterSupportedMessageTypes:
                continue

            elif msg["msg_type"] == JupyterMessageTypeStatus:
                if msg["content"]["execution_state"] == JupyterExecutionStateIdle:
                    break

            elif msg["msg_type"] == JupyterMessageTypeStream:
                res.events.append(
                    ExecutionEvent(
                        type=ExecutionEventTypeStream,
                        timestamp=datetime_to_rfc3339(datetime.now()),
                        data=ExecutionEventStream(
                            stream=msg["content"]["name"],
                            text=msg["content"]["text"],
                        ),
                    )
                )

            elif msg["msg_type"] == JupyterMessageTypeDisplayData:
                res.events.append(
                    ExecutionEvent(
                        type=ExecutionEventTypeDisplayData,
                        timestamp=datetime_to_rfc3339(datetime.now()),
                        data=ExecutionEventDisplayData(variants=msg["content"]["data"]),
                    )
                )

            elif msg["msg_type"] == JupyterMessageTypeError:
                res.events.append(
                    ExecutionEvent(
                        type=ExecutionEventTypeError,
                        timestamp=datetime_to_rfc3339(datetime.now()),
                        data=ExecutionEventError(
                            ename=msg["content"]["ename"],
                            evalue=msg["content"]["evalue"],
                            traceback=msg["content"]["traceback"],
                        ),
                    )
                )

            elif msg["msg_type"] == JupyterMessageTypeExecuteResult:
                res.events.append(
                    ExecutionEvent(
                        type=ExecutionEventTypeDisplayData,
                        timestamp=datetime_to_rfc3339(datetime.now()),
                        data=ExecutionEventDisplayData(variants=msg["content"]["data"]),
                    )
                )


@tornado.web.stream_request_body
class FileUploadHandler(tornado.web.RequestHandler):
    def initialize(self, state: State):
        self.state = state
        self.file_obj = None

    async def prepare(self):
        if self.request.method != "POST":
            self.set_status(404)
            self.finish()
            return

        path = self.path_args[0]
        full_path = os.path.join("/", path)

        os.makedirs(os.path.dirname(full_path), exist_ok=True)

        self.file_obj = open(full_path, "wb")

        content_length = int(self.request.headers.get("Content-Length", 0))
        logging.info(f"File upload: '{path}' (Content-Length: {content_length})")

    def data_received(self, chunk):
        if self.file_obj:
            self.file_obj.write(chunk)

    async def post(self, path):
        self.state.reset_last_activity()
        if self.file_obj:
            self.file_obj.close()
        self.set_status(201)


class FileDownloadHandler(tornado.web.RequestHandler):
    def initialize(self, state: State):
        self.state = state

    async def get(self, path):
        self.state.reset_last_activity()

        full_path = os.path.join("/", path)

        if not os.path.exists(full_path):
            self.set_status(404)
            self.write(Error(error="file not found").model_dump_json())
            return

        content_length = os.path.getsize(full_path)
        logging.info(f"File download: '{path}' (Content-Length: {content_length})")

        # Set appropriate headers for file download
        self.set_header("Content-Length", content_length)
        self.set_header("Content-Type", "application/octet-stream")
        self.set_header(
            "Content-Disposition",
            f"attachment; filename*=UTF-8''{tornado.escape.url_escape(os.path.basename(full_path))}",
        )

        # Stream the file to the client
        with open(full_path, "rb") as f:
            while True:
                chunk = f.read(64 * 1024)
                if not chunk:
                    break
                try:
                    self.write(chunk)
                    await self.flush()
                except tornado.iostream.StreamClosedError:
                    return


def shutdown(ioloop: tornado.ioloop.IOLoop, km):
    logging.info("Shutting down server...")
    ioloop.add_callback_from_signal(lambda: async_shutdown(km))


if __name__ == "__main__":
    p = argparse.ArgumentParser()
    p.add_argument("--port", type=int, default=80)
    p.add_argument("--kernel-name", type=str, default="python3")
    args = p.parse_args()

    km, client = asyncio.run(async_create_kernel(args.kernel_name))

    state = State(client)

    application = tornado.web.Application(
        [
            (r"/", MainHandler, {"state": state}),
            (r"/execute", ExecuteHandler, {"state": state}),
            (r"/files/upload/-/(.*)", FileUploadHandler, {"state": state}),
            (r"/files/download/-/(.*)", FileDownloadHandler, {"state": state}),
        ]
    )

    application.listen(args.port)

    logging.info(f"Server started at http://localhost:{args.port}")

    ioloop = tornado.ioloop.IOLoop.current()

    signal.signal(signal.SIGINT, lambda sig, frame: shutdown(ioloop, km))
    signal.signal(signal.SIGTERM, lambda sig, frame: shutdown(ioloop, km))

    ioloop.add_callback(async_msg_producer, km, client)

    tornado.ioloop.IOLoop.current().start()