handlers.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. """Tornado handlers for kernels.
  2. Preliminary documentation at https://github.com/ipython/ipython/wiki/IPEP-16%3A-Notebook-multi-directory-dashboard-and-URL-mapping#kernels-api
  3. """
  4. # Copyright (c) Jupyter Development Team.
  5. # Distributed under the terms of the Modified BSD License.
  6. import json
  7. import logging
  8. from textwrap import dedent
  9. from tornado import gen, web
  10. from tornado.concurrent import Future
  11. from tornado.ioloop import IOLoop
  12. from jupyter_client.jsonutil import date_default
  13. from ipython_genutils.py3compat import cast_unicode
  14. from notebook.utils import url_path_join, url_escape
  15. from ...base.handlers import APIHandler
  16. from ...base.zmqhandlers import AuthenticatedZMQStreamHandler, deserialize_binary_message
  17. from jupyter_client import protocol_version as client_protocol_version
  18. class MainKernelHandler(APIHandler):
  19. @web.authenticated
  20. @gen.coroutine
  21. def get(self):
  22. km = self.kernel_manager
  23. kernels = yield gen.maybe_future(km.list_kernels())
  24. self.finish(json.dumps(kernels, default=date_default))
  25. @web.authenticated
  26. @gen.coroutine
  27. def post(self):
  28. km = self.kernel_manager
  29. model = self.get_json_body()
  30. if model is None:
  31. model = {
  32. 'name': km.default_kernel_name
  33. }
  34. else:
  35. model.setdefault('name', km.default_kernel_name)
  36. kernel_id = yield gen.maybe_future(km.start_kernel(kernel_name=model['name']))
  37. model = km.kernel_model(kernel_id)
  38. location = url_path_join(self.base_url, 'api', 'kernels', url_escape(kernel_id))
  39. self.set_header('Location', location)
  40. self.set_status(201)
  41. self.finish(json.dumps(model, default=date_default))
  42. class KernelHandler(APIHandler):
  43. @web.authenticated
  44. def get(self, kernel_id):
  45. km = self.kernel_manager
  46. km._check_kernel_id(kernel_id)
  47. model = km.kernel_model(kernel_id)
  48. self.finish(json.dumps(model, default=date_default))
  49. @web.authenticated
  50. @gen.coroutine
  51. def delete(self, kernel_id):
  52. km = self.kernel_manager
  53. yield gen.maybe_future(km.shutdown_kernel(kernel_id))
  54. self.set_status(204)
  55. self.finish()
  56. class KernelActionHandler(APIHandler):
  57. @web.authenticated
  58. @gen.coroutine
  59. def post(self, kernel_id, action):
  60. km = self.kernel_manager
  61. if action == 'interrupt':
  62. km.interrupt_kernel(kernel_id)
  63. self.set_status(204)
  64. if action == 'restart':
  65. try:
  66. yield gen.maybe_future(km.restart_kernel(kernel_id))
  67. except Exception as e:
  68. self.log.error("Exception restarting kernel", exc_info=True)
  69. self.set_status(500)
  70. else:
  71. model = km.kernel_model(kernel_id)
  72. self.write(json.dumps(model, default=date_default))
  73. self.finish()
  74. class ZMQChannelsHandler(AuthenticatedZMQStreamHandler):
  75. '''There is one ZMQChannelsHandler per running kernel and it oversees all
  76. the sessions.
  77. '''
  78. # class-level registry of open sessions
  79. # allows checking for conflict on session-id,
  80. # which is used as a zmq identity and must be unique.
  81. _open_sessions = {}
  82. @property
  83. def kernel_info_timeout(self):
  84. km_default = self.kernel_manager.kernel_info_timeout
  85. return self.settings.get('kernel_info_timeout', km_default)
  86. @property
  87. def iopub_msg_rate_limit(self):
  88. return self.settings.get('iopub_msg_rate_limit', 0)
  89. @property
  90. def iopub_data_rate_limit(self):
  91. return self.settings.get('iopub_data_rate_limit', 0)
  92. @property
  93. def rate_limit_window(self):
  94. return self.settings.get('rate_limit_window', 1.0)
  95. def __repr__(self):
  96. return "%s(%s)" % (self.__class__.__name__, getattr(self, 'kernel_id', 'uninitialized'))
  97. def create_stream(self):
  98. km = self.kernel_manager
  99. identity = self.session.bsession
  100. for channel in ('shell', 'iopub', 'stdin'):
  101. meth = getattr(km, 'connect_' + channel)
  102. self.channels[channel] = stream = meth(self.kernel_id, identity=identity)
  103. stream.channel = channel
  104. def request_kernel_info(self):
  105. """send a request for kernel_info"""
  106. km = self.kernel_manager
  107. kernel = km.get_kernel(self.kernel_id)
  108. try:
  109. # check for previous request
  110. future = kernel._kernel_info_future
  111. except AttributeError:
  112. self.log.debug("Requesting kernel info from %s", self.kernel_id)
  113. # Create a kernel_info channel to query the kernel protocol version.
  114. # This channel will be closed after the kernel_info reply is received.
  115. if self.kernel_info_channel is None:
  116. self.kernel_info_channel = km.connect_shell(self.kernel_id)
  117. self.kernel_info_channel.on_recv(self._handle_kernel_info_reply)
  118. self.session.send(self.kernel_info_channel, "kernel_info_request")
  119. # store the future on the kernel, so only one request is sent
  120. kernel._kernel_info_future = self._kernel_info_future
  121. else:
  122. if not future.done():
  123. self.log.debug("Waiting for pending kernel_info request")
  124. future.add_done_callback(lambda f: self._finish_kernel_info(f.result()))
  125. return self._kernel_info_future
  126. def _handle_kernel_info_reply(self, msg):
  127. """process the kernel_info_reply
  128. enabling msg spec adaptation, if necessary
  129. """
  130. idents,msg = self.session.feed_identities(msg)
  131. try:
  132. msg = self.session.deserialize(msg)
  133. except:
  134. self.log.error("Bad kernel_info reply", exc_info=True)
  135. self._kernel_info_future.set_result({})
  136. return
  137. else:
  138. info = msg['content']
  139. self.log.debug("Received kernel info: %s", info)
  140. if msg['msg_type'] != 'kernel_info_reply' or 'protocol_version' not in info:
  141. self.log.error("Kernel info request failed, assuming current %s", info)
  142. info = {}
  143. self._finish_kernel_info(info)
  144. # close the kernel_info channel, we don't need it anymore
  145. if self.kernel_info_channel:
  146. self.kernel_info_channel.close()
  147. self.kernel_info_channel = None
  148. def _finish_kernel_info(self, info):
  149. """Finish handling kernel_info reply
  150. Set up protocol adaptation, if needed,
  151. and signal that connection can continue.
  152. """
  153. protocol_version = info.get('protocol_version', client_protocol_version)
  154. if protocol_version != client_protocol_version:
  155. self.session.adapt_version = int(protocol_version.split('.')[0])
  156. self.log.info("Adapting to protocol v%s for kernel %s", protocol_version, self.kernel_id)
  157. if not self._kernel_info_future.done():
  158. self._kernel_info_future.set_result(info)
  159. def initialize(self):
  160. super(ZMQChannelsHandler, self).initialize()
  161. self.zmq_stream = None
  162. self.channels = {}
  163. self.kernel_id = None
  164. self.kernel_info_channel = None
  165. self._kernel_info_future = Future()
  166. self._close_future = Future()
  167. self.session_key = ''
  168. # Rate limiting code
  169. self._iopub_window_msg_count = 0
  170. self._iopub_window_byte_count = 0
  171. self._iopub_msgs_exceeded = False
  172. self._iopub_data_exceeded = False
  173. # Queue of (time stamp, byte count)
  174. # Allows you to specify that the byte count should be lowered
  175. # by a delta amount at some point in the future.
  176. self._iopub_window_byte_queue = []
  177. @gen.coroutine
  178. def pre_get(self):
  179. # authenticate first
  180. super(ZMQChannelsHandler, self).pre_get()
  181. # check session collision:
  182. yield self._register_session()
  183. # then request kernel info, waiting up to a certain time before giving up.
  184. # We don't want to wait forever, because browsers don't take it well when
  185. # servers never respond to websocket connection requests.
  186. kernel = self.kernel_manager.get_kernel(self.kernel_id)
  187. self.session.key = kernel.session.key
  188. future = self.request_kernel_info()
  189. def give_up():
  190. """Don't wait forever for the kernel to reply"""
  191. if future.done():
  192. return
  193. self.log.warning("Timeout waiting for kernel_info reply from %s", self.kernel_id)
  194. future.set_result({})
  195. loop = IOLoop.current()
  196. loop.add_timeout(loop.time() + self.kernel_info_timeout, give_up)
  197. # actually wait for it
  198. yield future
  199. @gen.coroutine
  200. def get(self, kernel_id):
  201. self.kernel_id = cast_unicode(kernel_id, 'ascii')
  202. yield super(ZMQChannelsHandler, self).get(kernel_id=kernel_id)
  203. @gen.coroutine
  204. def _register_session(self):
  205. """Ensure we aren't creating a duplicate session.
  206. If a previous identical session is still open, close it to avoid collisions.
  207. This is likely due to a client reconnecting from a lost network connection,
  208. where the socket on our side has not been cleaned up yet.
  209. """
  210. self.session_key = '%s:%s' % (self.kernel_id, self.session.session)
  211. stale_handler = self._open_sessions.get(self.session_key)
  212. if stale_handler:
  213. self.log.warning("Replacing stale connection: %s", self.session_key)
  214. yield stale_handler.close()
  215. self._open_sessions[self.session_key] = self
  216. def open(self, kernel_id):
  217. super(ZMQChannelsHandler, self).open()
  218. km = self.kernel_manager
  219. km.notify_connect(kernel_id)
  220. # on new connections, flush the message buffer
  221. buffer_info = km.get_buffer(kernel_id, self.session_key)
  222. if buffer_info and buffer_info['session_key'] == self.session_key:
  223. self.log.info("Restoring connection for %s", self.session_key)
  224. self.channels = buffer_info['channels']
  225. replay_buffer = buffer_info['buffer']
  226. if replay_buffer:
  227. self.log.info("Replaying %s buffered messages", len(replay_buffer))
  228. for channel, msg_list in replay_buffer:
  229. stream = self.channels[channel]
  230. self._on_zmq_reply(stream, msg_list)
  231. else:
  232. try:
  233. self.create_stream()
  234. except web.HTTPError as e:
  235. self.log.error("Error opening stream: %s", e)
  236. # WebSockets don't response to traditional error codes so we
  237. # close the connection.
  238. for channel, stream in self.channels.items():
  239. if not stream.closed():
  240. stream.close()
  241. self.close()
  242. return
  243. km.add_restart_callback(self.kernel_id, self.on_kernel_restarted)
  244. km.add_restart_callback(self.kernel_id, self.on_restart_failed, 'dead')
  245. for channel, stream in self.channels.items():
  246. stream.on_recv_stream(self._on_zmq_reply)
  247. def on_message(self, msg):
  248. if not self.channels:
  249. # already closed, ignore the message
  250. self.log.debug("Received message on closed websocket %r", msg)
  251. return
  252. if isinstance(msg, bytes):
  253. msg = deserialize_binary_message(msg)
  254. else:
  255. msg = json.loads(msg)
  256. channel = msg.pop('channel', None)
  257. if channel is None:
  258. self.log.warning("No channel specified, assuming shell: %s", msg)
  259. channel = 'shell'
  260. if channel not in self.channels:
  261. self.log.warning("No such channel: %r", channel)
  262. return
  263. stream = self.channels[channel]
  264. self.session.send(stream, msg)
  265. def _on_zmq_reply(self, stream, msg_list):
  266. idents, fed_msg_list = self.session.feed_identities(msg_list)
  267. msg = self.session.deserialize(fed_msg_list)
  268. parent = msg['parent_header']
  269. def write_stderr(error_message):
  270. self.log.warning(error_message)
  271. msg = self.session.msg("stream",
  272. content={"text": error_message + '\n', "name": "stderr"},
  273. parent=parent
  274. )
  275. msg['channel'] = 'iopub'
  276. self.write_message(json.dumps(msg, default=date_default))
  277. channel = getattr(stream, 'channel', None)
  278. msg_type = msg['header']['msg_type']
  279. if channel == 'iopub' and msg_type == 'status' and msg['content'].get('execution_state') == 'idle':
  280. # reset rate limit counter on status=idle,
  281. # to avoid 'Run All' hitting limits prematurely.
  282. self._iopub_window_byte_queue = []
  283. self._iopub_window_msg_count = 0
  284. self._iopub_window_byte_count = 0
  285. self._iopub_msgs_exceeded = False
  286. self._iopub_data_exceeded = False
  287. if channel == 'iopub' and msg_type not in {'status', 'comm_open', 'execute_input'}:
  288. # Remove the counts queued for removal.
  289. now = IOLoop.current().time()
  290. while len(self._iopub_window_byte_queue) > 0:
  291. queued = self._iopub_window_byte_queue[0]
  292. if (now >= queued[0]):
  293. self._iopub_window_byte_count -= queued[1]
  294. self._iopub_window_msg_count -= 1
  295. del self._iopub_window_byte_queue[0]
  296. else:
  297. # This part of the queue hasn't be reached yet, so we can
  298. # abort the loop.
  299. break
  300. # Increment the bytes and message count
  301. self._iopub_window_msg_count += 1
  302. if msg_type == 'stream':
  303. byte_count = sum([len(x) for x in msg_list])
  304. else:
  305. byte_count = 0
  306. self._iopub_window_byte_count += byte_count
  307. # Queue a removal of the byte and message count for a time in the
  308. # future, when we are no longer interested in it.
  309. self._iopub_window_byte_queue.append((now + self.rate_limit_window, byte_count))
  310. # Check the limits, set the limit flags, and reset the
  311. # message and data counts.
  312. msg_rate = float(self._iopub_window_msg_count) / self.rate_limit_window
  313. data_rate = float(self._iopub_window_byte_count) / self.rate_limit_window
  314. # Check the msg rate
  315. if self.iopub_msg_rate_limit > 0 and msg_rate > self.iopub_msg_rate_limit:
  316. if not self._iopub_msgs_exceeded:
  317. self._iopub_msgs_exceeded = True
  318. write_stderr(dedent("""\
  319. IOPub message rate exceeded.
  320. The notebook server will temporarily stop sending output
  321. to the client in order to avoid crashing it.
  322. To change this limit, set the config variable
  323. `--NotebookApp.iopub_msg_rate_limit`.
  324. Current values:
  325. NotebookApp.iopub_msg_rate_limit={} (msgs/sec)
  326. NotebookApp.rate_limit_window={} (secs)
  327. """.format(self.iopub_msg_rate_limit, self.rate_limit_window)))
  328. else:
  329. # resume once we've got some headroom below the limit
  330. if self._iopub_msgs_exceeded and msg_rate < (0.8 * self.iopub_msg_rate_limit):
  331. self._iopub_msgs_exceeded = False
  332. if not self._iopub_data_exceeded:
  333. self.log.warning("iopub messages resumed")
  334. # Check the data rate
  335. if self.iopub_data_rate_limit > 0 and data_rate > self.iopub_data_rate_limit:
  336. if not self._iopub_data_exceeded:
  337. self._iopub_data_exceeded = True
  338. write_stderr(dedent("""\
  339. IOPub data rate exceeded.
  340. The notebook server will temporarily stop sending output
  341. to the client in order to avoid crashing it.
  342. To change this limit, set the config variable
  343. `--NotebookApp.iopub_data_rate_limit`.
  344. Current values:
  345. NotebookApp.iopub_data_rate_limit={} (bytes/sec)
  346. NotebookApp.rate_limit_window={} (secs)
  347. """.format(self.iopub_data_rate_limit, self.rate_limit_window)))
  348. else:
  349. # resume once we've got some headroom below the limit
  350. if self._iopub_data_exceeded and data_rate < (0.8 * self.iopub_data_rate_limit):
  351. self._iopub_data_exceeded = False
  352. if not self._iopub_msgs_exceeded:
  353. self.log.warning("iopub messages resumed")
  354. # If either of the limit flags are set, do not send the message.
  355. if self._iopub_msgs_exceeded or self._iopub_data_exceeded:
  356. # we didn't send it, remove the current message from the calculus
  357. self._iopub_window_msg_count -= 1
  358. self._iopub_window_byte_count -= byte_count
  359. self._iopub_window_byte_queue.pop(-1)
  360. return
  361. super(ZMQChannelsHandler, self)._on_zmq_reply(stream, msg)
  362. def close(self):
  363. super(ZMQChannelsHandler, self).close()
  364. return self._close_future
  365. def on_close(self):
  366. self.log.debug("Websocket closed %s", self.session_key)
  367. # unregister myself as an open session (only if it's really me)
  368. if self._open_sessions.get(self.session_key) is self:
  369. self._open_sessions.pop(self.session_key)
  370. km = self.kernel_manager
  371. if self.kernel_id in km:
  372. km.notify_disconnect(self.kernel_id)
  373. km.remove_restart_callback(
  374. self.kernel_id, self.on_kernel_restarted,
  375. )
  376. km.remove_restart_callback(
  377. self.kernel_id, self.on_restart_failed, 'dead',
  378. )
  379. # start buffering instead of closing if this was the last connection
  380. if km._kernel_connections[self.kernel_id] == 0:
  381. km.start_buffering(self.kernel_id, self.session_key, self.channels)
  382. self._close_future.set_result(None)
  383. return
  384. # This method can be called twice, once by self.kernel_died and once
  385. # from the WebSocket close event. If the WebSocket connection is
  386. # closed before the ZMQ streams are setup, they could be None.
  387. for channel, stream in self.channels.items():
  388. if stream is not None and not stream.closed():
  389. stream.on_recv(None)
  390. stream.close()
  391. self.channels = {}
  392. self._close_future.set_result(None)
  393. def _send_status_message(self, status):
  394. iopub = self.channels.get('iopub', None)
  395. if iopub and not iopub.closed():
  396. # flush IOPub before sending a restarting/dead status message
  397. # ensures proper ordering on the IOPub channel
  398. # that all messages from the stopped kernel have been delivered
  399. iopub.flush()
  400. msg = self.session.msg("status",
  401. {'execution_state': status}
  402. )
  403. msg['channel'] = 'iopub'
  404. self.write_message(json.dumps(msg, default=date_default))
  405. def on_kernel_restarted(self):
  406. logging.warn("kernel %s restarted", self.kernel_id)
  407. self._send_status_message('restarting')
  408. def on_restart_failed(self):
  409. logging.error("kernel %s restarted failed!", self.kernel_id)
  410. self._send_status_message('dead')
  411. #-----------------------------------------------------------------------------
  412. # URL to handler mappings
  413. #-----------------------------------------------------------------------------
  414. _kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"
  415. _kernel_action_regex = r"(?P<action>restart|interrupt)"
  416. default_handlers = [
  417. (r"/api/kernels", MainKernelHandler),
  418. (r"/api/kernels/%s" % _kernel_id_regex, KernelHandler),
  419. (r"/api/kernels/%s/%s" % (_kernel_id_regex, _kernel_action_regex), KernelActionHandler),
  420. (r"/api/kernels/%s/channels" % _kernel_id_regex, ZMQChannelsHandler),
  421. ]