@@ -232,18 +232,25 @@ async def _async_start_kernel( # type:ignore[override]
232232 kwargs ["kernel_id" ] = kernel_id
233233 kernel_id = await self .pinned_superclass ._async_start_kernel (self , ** kwargs )
234234 self ._kernel_connections [kernel_id ] = 0
235- task = asyncio .create_task (self ._finish_kernel_start (kernel_id ))
236- if not getattr (self , "use_pending_kernels" , None ):
237- await task
238- else :
239- self ._pending_kernel_tasks [kernel_id ] = task
235+
240236 # add busy/activity markers:
241237 kernel = self .get_kernel (kernel_id )
242238 kernel .execution_state = "starting" # type:ignore[attr-defined]
243239 kernel .reason = "" # type:ignore[attr-defined]
244240 kernel .last_activity = utcnow () # type:ignore[attr-defined]
245241 self .log .info ("Kernel started: %s" , kernel_id )
246- self .log .debug ("Kernel args: %r" , kwargs )
242+ self .log .debug (
243+ "Kernel args (excluding env): %r" , {k : v for k , v in kwargs .items () if k != "env" }
244+ )
245+ env = kwargs .get ("env" , None )
246+ if env and isinstance (env , dict ): # type:ignore[unreachable]
247+ self .log .debug ("Kernel argument 'env' passed with: %r" , list (env .keys ())) # type:ignore[unreachable]
248+
249+ task = asyncio .create_task (self ._finish_kernel_start (kernel_id ))
250+ if not getattr (self , "use_pending_kernels" , None ):
251+ await task
252+ else :
253+ self ._pending_kernel_tasks [kernel_id ] = task
247254
248255 # Increase the metric of number of kernels running
249256 # for the relevant kernel type by 1
@@ -532,6 +539,40 @@ def _check_kernel_id(self, kernel_id):
532539 raise web .HTTPError (404 , "Kernel does not exist: %s" % kernel_id )
533540
534541 # monitoring activity:
542+ untracked_message_types = List (
543+ trait = Unicode (),
544+ config = True ,
545+ default_value = [
546+ "comm_info_request" ,
547+ "comm_info_reply" ,
548+ "kernel_info_request" ,
549+ "kernel_info_reply" ,
550+ "shutdown_request" ,
551+ "shutdown_reply" ,
552+ "interrupt_request" ,
553+ "interrupt_reply" ,
554+ "debug_request" ,
555+ "debug_reply" ,
556+ "stream" ,
557+ "display_data" ,
558+ "update_display_data" ,
559+ "execute_input" ,
560+ "execute_result" ,
561+ "error" ,
562+ "status" ,
563+ "clear_output" ,
564+ "debug_event" ,
565+ "input_request" ,
566+ "input_reply" ,
567+ ],
568+ help = """List of kernel message types excluded from user activity tracking.
569+
570+ This should be a superset of the message types sent on any channel other
571+ than the shell channel.""" ,
572+ )
573+
574+ def track_message_type (self , message_type ):
575+ return message_type not in self .untracked_message_types
535576
536577 def start_watching_activity (self , kernel_id ):
537578 """Start watching IOPub messages on a kernel for activity.
@@ -552,15 +593,27 @@ def start_watching_activity(self, kernel_id):
552593
553594 def record_activity (msg_list ):
554595 """Record an IOPub message arriving from a kernel"""
555- self .last_kernel_activity = kernel .last_activity = utcnow ()
556-
557596 idents , fed_msg_list = session .feed_identities (msg_list )
558597 msg = session .deserialize (fed_msg_list , content = False )
559598
560599 msg_type = msg ["header" ]["msg_type" ]
600+ parent_msg_type = msg .get ("parent_header" , {}).get ("msg_type" , None )
601+ if (
602+ self .track_message_type (msg_type )
603+ or self .track_message_type (parent_msg_type )
604+ or kernel .execution_state == "busy"
605+ ):
606+ self .last_kernel_activity = kernel .last_activity = utcnow ()
561607 if msg_type == "status" :
562608 msg = session .deserialize (fed_msg_list )
563- kernel .execution_state = msg ["content" ]["execution_state" ]
609+ execution_state = msg ["content" ]["execution_state" ]
610+ if self .track_message_type (parent_msg_type ):
611+ kernel .execution_state = execution_state
612+ elif kernel .execution_state == "starting" and execution_state != "starting" :
613+ # We always normalize post-starting execution state to "idle"
614+ # unless we know that the status is in response to one of our
615+ # tracked message types.
616+ kernel .execution_state = "idle"
564617 self .log .debug (
565618 "activity on %s: %s (%s)" ,
566619 kernel_id ,
0 commit comments