@@ -350,23 +350,33 @@ fn handle_session(
350350 }
351351 } ;
352352 let identity = IdentityPayload :: try_from ( & snapshot) ?;
353- let backend = Arc :: new ( with_worker_autorelease_pool ( || {
354- RealHostBackend :: new ( snapshot, identity)
355- } ) ?) ;
356- handle_session_with_backend (
357- listeners,
358- control,
359- SessionStart {
360- session_id,
361- session_trace_id,
362- guest_runtime_overrides,
363- hello_request_id : hello_frame. header . id ,
364- guest_pid : hello. guest_pid ,
365- } ,
366- app_state,
367- backend,
368- stop,
369- )
353+ let session = SessionStart {
354+ session_id,
355+ session_trace_id,
356+ guest_runtime_overrides,
357+ hello_request_id : hello_frame. header . id ,
358+ guest_pid : hello. guest_pid ,
359+ } ;
360+ app_state. set ( HostRuntimeState :: PuckConnected {
361+ serial : identity. serial . clone ( ) ,
362+ } ) ;
363+ let backend = match with_worker_autorelease_pool ( || RealHostBackend :: new ( snapshot) ) {
364+ Ok ( backend) => Arc :: new ( backend) ,
365+ Err ( error) => {
366+ let hello_ok = hello_ok_with_status (
367+ StatusCode :: DeviceDisconnected ,
368+ session_id,
369+ session_trace_id,
370+ identity. default_input_report_len ( ) ,
371+ ) ;
372+ write_payload ( control, hello_frame. header . id , & hello_ok) ?;
373+ return Err ( error. into ( ) ) ;
374+ }
375+ } ;
376+ write_session_preamble ( control, & session, & identity) ?;
377+ let input =
378+ attach_input_for_session ( listeners, stop, INPUT_ATTACH_TIMEOUT , & session, & identity) ?;
379+ run_attached_session ( control, input, session, app_state, backend, stop, identity)
370380}
371381
372382#[ derive( Clone , Debug ) ]
@@ -378,11 +388,13 @@ struct SessionStart {
378388 guest_pid : u32 ,
379389}
380390
391+ #[ cfg( test) ]
381392fn handle_session_with_backend (
382393 listeners : & TransportListeners ,
383394 control : & mut ChannelStream ,
384395 session : SessionStart ,
385396 app_state : & AppState ,
397+ identity : IdentityPayload ,
386398 backend : Arc < dyn HostBackend > ,
387399 stop : & Arc < AtomicBool > ,
388400) -> Result < ( ) , RuntimeError > {
@@ -391,26 +403,39 @@ fn handle_session_with_backend(
391403 control,
392404 session,
393405 app_state,
406+ identity,
394407 backend,
395408 stop,
396409 INPUT_ATTACH_TIMEOUT ,
397410 )
398411}
399412
413+ #[ cfg( test) ]
400414fn handle_session_with_backend_timeout (
401415 listeners : & TransportListeners ,
402416 control : & mut ChannelStream ,
403417 session : SessionStart ,
404418 app_state : & AppState ,
419+ identity : IdentityPayload ,
405420 backend : Arc < dyn HostBackend > ,
406421 stop : & Arc < AtomicBool > ,
407422 input_attach_timeout : Duration ,
408423) -> Result < ( ) , RuntimeError > {
409- let identity = backend. identity ( ) . clone ( ) ;
410424 app_state. set ( HostRuntimeState :: PuckConnected {
411425 serial : identity. serial . clone ( ) ,
412426 } ) ;
413427
428+ write_session_preamble ( control, & session, & identity) ?;
429+ let input =
430+ attach_input_for_session ( listeners, stop, input_attach_timeout, & session, & identity) ?;
431+ run_attached_session ( control, input, session, app_state, backend, stop, identity)
432+ }
433+
434+ fn write_session_preamble (
435+ control : & mut ChannelStream ,
436+ session : & SessionStart ,
437+ identity : & IdentityPayload ,
438+ ) -> Result < ( ) , RuntimeError > {
414439 write_payload (
415440 control,
416441 session. hello_request_id ,
@@ -428,8 +453,17 @@ fn handle_session_with_backend_timeout(
428453 log_level. as_str( )
429454 ) ;
430455 }
431- write_payload ( control, 0 , & identity) ?;
456+ write_payload ( control, 0 , identity) ?;
457+ Ok ( ( ) )
458+ }
432459
460+ fn attach_input_for_session (
461+ listeners : & TransportListeners ,
462+ stop : & Arc < AtomicBool > ,
463+ input_attach_timeout : Duration ,
464+ session : & SessionStart ,
465+ identity : & IdentityPayload ,
466+ ) -> Result < ChannelStream , RuntimeError > {
433467 let mut input = accept_input_for_session ( listeners, stop, input_attach_timeout) ?;
434468 input. set_write_timeout ( Some ( Duration :: from_millis ( 250 ) ) ) ?;
435469 input. set_read_timeout ( Some ( Duration :: from_millis ( 250 ) ) ) ?;
@@ -461,7 +495,18 @@ fn handle_session_with_backend_timeout(
461495 actual : attach. session_id ,
462496 } ) ;
463497 }
498+ Ok ( input)
499+ }
464500
501+ fn run_attached_session (
502+ control : & mut ChannelStream ,
503+ input : ChannelStream ,
504+ session : SessionStart ,
505+ app_state : & AppState ,
506+ backend : Arc < dyn HostBackend > ,
507+ stop : & Arc < AtomicBool > ,
508+ identity : IdentityPayload ,
509+ ) -> Result < ( ) , RuntimeError > {
465510 app_state. set ( HostRuntimeState :: GuestConnected {
466511 session_id : session. session_id ,
467512 session_trace_id : session. session_trace_id ,
@@ -812,7 +857,6 @@ mod tests {
812857
813858 #[ derive( Clone ) ]
814859 struct FakeBackend {
815- identity : IdentityPayload ,
816860 reports : Arc < Mutex < VecDeque < Vec < u8 > > > > ,
817861 operations : Arc < Mutex < Vec < String > > > ,
818862 cleanup_called : Arc < AtomicBool > ,
@@ -821,7 +865,6 @@ mod tests {
821865 impl FakeBackend {
822866 fn new ( reports : Vec < Vec < u8 > > ) -> Self {
823867 Self {
824- identity : test_identity ( ) ,
825868 reports : Arc :: new ( Mutex :: new ( reports. into ( ) ) ) ,
826869 operations : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
827870 cleanup_called : Arc :: new ( AtomicBool :: new ( false ) ) ,
@@ -842,10 +885,6 @@ mod tests {
842885 }
843886
844887 impl HostBackend for FakeBackend {
845- fn identity ( & self ) -> & IdentityPayload {
846- & self . identity
847- }
848-
849888 fn open_input_reader ( & self ) -> Result < Box < dyn InputReportReader > , HostHidError > {
850889 Ok ( Box :: new ( FakeInputReader {
851890 reports : Arc :: clone ( & self . reports ) ,
@@ -949,6 +988,7 @@ mod tests {
949988 guest_pid : 1234 ,
950989 } ,
951990 & AppState :: new ( ) ,
991+ test_identity ( ) ,
952992 Arc :: new ( backend_for_server) ,
953993 & server_stop,
954994 )
@@ -1034,6 +1074,7 @@ mod tests {
10341074 guest_pid : 1234 ,
10351075 } ,
10361076 & AppState :: new ( ) ,
1077+ test_identity ( ) ,
10371078 Arc :: new ( backend) ,
10381079 & server_stop,
10391080 )
@@ -1081,6 +1122,7 @@ mod tests {
10811122 guest_pid : 1234 ,
10821123 } ,
10831124 & AppState :: new ( ) ,
1125+ test_identity ( ) ,
10841126 Arc :: new ( backend) ,
10851127 & server_stop,
10861128 ) ;
@@ -1142,6 +1184,7 @@ mod tests {
11421184 guest_pid : 1234 ,
11431185 } ,
11441186 & AppState :: new ( ) ,
1187+ test_identity ( ) ,
11451188 Arc :: new ( backend) ,
11461189 & server_stop,
11471190 Duration :: from_millis ( 100 ) ,
0 commit comments